如果你想获得幻想,试试这个:
def parse_file(f):
competitors = []
for line in f:
last, first, year, medals, _ = line.strip(), next(f).strip(), next(f).strip(), next(f).strip(), next(f)
competitors.append({"last":last, "first":first,
"year":year, "medals":medals})
return competitors
def count_by_year(year, competitors_dict):
# year must be a string
year = str(year)
return sum(competitor['medals'] for competitor in competitors_dict if competitor['year'] == year)
def years_by_athlete(firstname, lastname, year, competitors_dict):
for competitor in competitors_dict:
if competitor['first'] == firstname and \
competitor['last'] == lastname and \
competitor['year'] == year:
return competitor['medals']
return None
if __name__ == "__main__":
with open('path/to/your/file', 'r') as f:
competitors = parse_file(f)
num_medals_by_year = count_by_year(1900, competitors)
num_medals_by_athlete = years_by_athlete("DARIA", "PRATT", "1900", competitors)
记住,文件对象在Python迭代器,所以如果你正在读文件描述如下:
This is the first line
This is the second line
This is the third line
This is the fourth line
This is a blank line
那么你可以做
for line in file:
firstline = line
secondline = next(file)
thirdline = next(file)
fourthline = next(file)
_ = next(file)
内置的next
推进迭代器。代码以5行块的形式贯穿整个文件,将最后一行分配给_
(这是“我们不使用这个”的常见Python成语,例如for _ in range(10)
做了10次)。然后我们建立一个字典列表,以便稍后参考。这比我们每次查看文件时读取文件要快得多。我们使用strip
(除了我们扔掉的线以外,因为......为什么要打扰?)来删除尾随的空格。这包括在每行末尾的换行符(如匹配的文件读取This is the first line
准确,你就必须寻找"This is the first line\n"
)
然后使用内置sum
功能,我们使用一台发电机表达式如果'year'
值与我们给出的值相符,则给予每个'medals'
值。这将展开到:
s = 0
for competitor in competitors_dict:
if competitors_dict['year'] == year:
s += competitors_dict['medals']
return s
至于你的第二个功能,你需要的是一系列if
条件。通过competitors_dict
迭代并确保'first'
,'last'
和'year'
字段都与您的参数匹配,然后返回'medals'
字段。就这么简单:)
第一个'break'执行后的代码能执行吗? – 101 2014-09-19 00:04:02
为什么不'如果行==年? – 2014-09-19 00:08:29
我认为行是必要的计数器,我将不得不增加去下一行? – darksoulsfan 2014-09-19 00:09:54