所以这是我正在使用的文本文件:
AK,F,1910,Mary,14
AK,F,1910,Annie,12
AK,F,1910,Anna,10
AK,F,1910,Margaret,8
AK,F,1910,Helen,7
AK,F,1910,Elsie,6
文本文件包含每个州。它一直持续到2000年代。每年都列出性别M和F,每年年初的名字是最受欢迎的一年。例如,玛丽是1910年最受欢迎的婴儿名字。 我的代码应该返回这样的内容:
Enter state: ny
Enter gender: f
Enter start year: 2004
Enter end year: 2007
Top female names for NY between 2004-2007:
In 2004 Emily occurred the most at 1590 times
In 2005 Emily occurred the most at 1444 times
In 2006 Emily occurred the most at 1317 times
In 2007 Isabella occurred the most at 1425 times
Emily occurred consecutively the most in this range at 3 time/s
我已经写了很多这个程序。我只需要一些关于如何返回表示每年顶级名称的Name对象列表的建议 范围。
答案 0 :(得分:-1)
以下是你可以做的(这里是2.7.8,我在这台机器上没有3.x):
from collections import defaultdict, Counter
data = '''-,-,1970,John,-
-,-,1970,John,-
-,-,1970,Paul,-
-,-,2014,Bob,-
-,-,2014,Mary,-
-,-,2014,Mary,-'''
temp = defaultdict(list)
for record in (line.split(',') for line in data.splitlines()):
y = record[2]
n = record[3]
temp[y].append(n)
results = [(k, Counter(v).most_common(1)) for k,v in temp.items()]
[('2014',[('Mary',2)]),('1970',[('John',2)])]
for year,r in results:
if int(year) in valid:
print('In {0} the name {1} occured the most ({2} times)'.format(year, r[0][0], r[0][1]))
2014年,Mary的名字发生次数最多(2次)
1970年,约翰这个名字出现次数最多(2次)