a01:01-24-2011:s1
a03:01-24-2011:s2
a02:01-24-2011:s2
a03:02-02-2011:s2
a03:03-02-2011:s1
a02:04-19-2011:s2
a01:05-14-2011:s2
a02:06-11-2011:s2
a03:07-12-2011:s1
a01:08-19-2011:s1
a03:09-19-2011:s1
a03:10-19-2011:s2
a03:11-19-2011:s1
a03:12-19-2011:s2
格式为animalid:date:location
到目前为止,这是我的代码
def main():
fname = input("Enter name of file: ")
with open(fname) as inf:
animalnames, dates, locations = zip(*[line.strip().split(':') for line in inf])
d = {}
for animalname, loc in zip(animalnames, locations):
d.setdefault(animalname, []).append(loc)
print("Animal ID","Location1","Location2")
for k, v in d.items():
print(k, end='\t') #To print table of the data
print(v.count('s1'), end='\t')
print(v.count('s2'))
print("================================================================================================================")
我所拥有的表是
Animal ID Location1 Location2
a01 2 1
a02 0 3
有人可以帮助我如何使用上述所有内容来回答问题:
1)至少4次访问两个站的动物#answer是a03
2)每只动物的总访问次数
答案 0 :(得分:2)
您已经完成了大部分工作,这将得到您的答案:
1)
for k, v in d.items():
if v.count('s1')>=4 and v.count('s2')>=4:
print k
2)
for k, v in d.items():
print k, v.count('s1')+v.count('s2')