a = "i love love you you" from collections import Counter dict( Counter(list(a.split())) ) Out[103]: {'i': 1, 'love': 2, 'you': 2}
或者:
s = '11AAAdfdfBB' sli = list(s.upper()) sli Out[107]: ['1', '1', 'A', 'A', 'A', 'D', 'F', 'D', 'F', 'B', 'B'] [(i, sli.count(i)) for i in sli] Out[108]: [('1', 2), ('1', 2), ('A', 3), ('A', 3), ('A', 3), ('D', 2), ('F', 2), ('D', 2), ('F', 2), ('B', 2), ('B', 2)] sorted(sliset, key=lambda x:(-x[1])) Out[116]: [('A', 3), ('1', 2), ('D', 2), ('F', 2), ('B', 2)]
来源:https://www.cnblogs.com/douzujun/p/12369416.html