python如何统计子列表中某个词出现的次数

2024-12-27 18:59:17
推荐回答(1个)
回答1:

persondic={1:'a',2:'b',3:'c'}
counts={}
lst=[[1,2,3],[3,2,1],[1,3,2],[1,2,3]]
for i in lst:
ckey=persondic[i[0]]
counts[ckey]=counts.get(ckey,0)+1

for v in persondic.values():
print v,'=',counts.get(v,0)

python2.7