简单操作:

import collections
A=['a','b','b','c','d','b','a']
count=collections.Counter(A)
print(count)
Counter({'b': 3, 'a': 2, 'c': 1, 'd': 1})
count.items()
Out[6]: dict_items([('a', 2), ('b', 3), ('c', 1), ('d', 1)])
count.keys()
Out[7]: dict_keys(['a', 'b', 'c', 'd'])
count.values()
Out[8]: dict_values([2, 3, 1, 1])
count.elements()
Out[9]: <itertools.chain at 0x7f08050d66a0>
list(count.elements())
Out[10]: ['a', 'a', 'b', 'b', 'b', 'c', 'd']

排序:

Out[20]: [1, 1, 2, 3]
sorted(count.items())
Out[21]: [('a', 2), ('b', 3), ('c', 1), ('d', 1)]
sorted(count.items(),key=lambda x:x[1])
Out[22]: [('c', 1), ('d', 1), ('a', 2), ('b', 3)]

 

相关文章:

  • 2022-12-23
  • 2021-12-30
  • 2022-12-23
  • 2022-01-14
  • 2021-10-14
  • 2021-04-07
  • 2022-12-23
  • 2021-09-26
猜你喜欢
  • 2022-12-23
  • 2022-01-26
  • 2022-12-23
  • 2021-11-29
  • 2022-12-23
  • 2021-10-01
相关资源
相似解决方案