我有名单,名字和姓氏列表.
如何获得大于3的姓氏数.
import collections
names = [['Fiona','Murray'],
['Diane','Springer'],
['Dylan','Oliver'],
['Alison','McDonald'],
['Sophie','Peters'],
['Kevin','Davidson'],
['David','Marshall'],
['Jake','Lewis'],
['Thomas','Burgess'],
['Fiona','Murray'],
['Fiona','Murray'],
['Fiona','Murray'],
['Fiona','Murray'],
['Dylan','Oliver'],
['Dylan','Oliver'] ]
lst1 = [(a,b) for (a,b) in collections.Counter(map(tuple,names)).iteritems()]
我尝试了列表理解但无法获得大于3的姓氏.
最佳答案 更具功能性的编程方法.
from collections import Counter
from operator import itemgetter
from itertools import imap
[(name, count) for name, count in Counter(imap(itemgetter(1), names)).iteritems() if count > 3]