【问题标题】:Python: Iterating through a listPython:遍历列表
【发布时间】:2013-02-18 04:54:08
【问题描述】:

给我一​​个文本文件,其中包含许多行,如下所示...许多随机信息

Spiaks 餐厅|42.74|-73.70|2 Archibald St+Watervliet, NY 12189|http://www.yelp.com/biz/spiaks-restaurant-watervliet|Italian|4|5|4|3|3|4|4

例如,Spiaks Restaurant 位于位置 0,42.74 位于位置 1,-73.70 位于位置 2.... Italian 位于位置 5... 4|5|4|3|3|4|4 是另一个列表...所以基本上是列表中的一个列表,数字 4 将位于位置 6,5 位于位置 7.. 等等

我要问用户,用户应该回复:

What type of restaurant would you like => Italian
What is the minimum rating => 3.6

结果应该是:

Name: Spiaks Restaurant; Rating 3.86
Name: Lo Portos; Rating 4.00
Name: Verdiles Restaurant; Rating 4.00
Found 3 restaurants.

这是我的代码:

rest_type = raw_input("What type of restaurant would you like => ")
min_rate = float(raw_input("What is the minimum rating => "))

def parse_line(text_file):
count = 0.0
a_strip = text_file.strip('\n')
b_split = a_strip.split('|')

for i in range(6, len(b_split)):
    b_split[i] = int(b_split[i]) # Takes the current indices in the list and converts it to integers
    count += b_split[i] # Add up all of the integers values to get the total ratings
    avg_rate = count/len(b_split[6:len(b_split)]) # Takes the total ratings & divides it by the length

#The above basically calculates the average of the numbers like 4|5|4|3|3|4|4

    if (rest_type == b_split[5] and avg_rate >= min_rate):
        print b_split[0], avg_rate

结果的问题是..我明白了:

None

我知道这是一个非常非常长的问题,但如果有人能给我一些见解,我将不胜感激!

【问题讨论】:

    标签: list python-2.7 iterator


    【解决方案1】:

    您是否尝试打印出您汇总的所有信息?

    找出错误发生的位置,无论是在您尝试解析特定餐厅时,还是直接什么都不解析并最终得到一个空白列表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-08
      • 2015-10-07
      • 2013-07-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多