像这样的东西
from collections import defaultdict
# collect all visitors in a dictionary where the key is the date, and
# the value is a set of badge numbers
visitorsPerDay = defaultdict(set)
# store the last read date value
currentDate = None
with open('filename') as f:
for line in f:
# if the line is 10 characters long, it’s a date line
if len(line.strip()) == 10:
# store the date value
currentDate = line.strip()
elif currentDate:
# extract the badge number; if the file is tab
# separated, even better: split by \t
time, badge, _ = (part.strip() for part in line.split(' ', 2))
# add the badge number to the set within the dictionary
visitorsPerDay[currentDate].add(badge)
# now for every date, count the number of (unique) visitors
for date, visitors in visitorsPerDay.items():
print(date, len(visitors))