"""
python 计算列表内容出现次数
"""
#方法一:
   l = ['a','a','b','c','d','b','b','b']
   test_dict = {}
   for i in l:
        #通过key来计算元素个数
        test_dict[i] = test_dict.get(i,0) + 1
   print(test_dict)

 

python 计算列表内容出现次数

使用python中的内置模块

#方法二
l = ['a','a','b','c','d','b','b','b']
from collections import Counter
r = Counter(l)
print(r)

python 计算列表内容出现次数



#遍历循坏字典
for key,value in test_dict.items():
print("Key: " + key)
print("Value: " + str(value))

相关文章:

  • 2022-12-23
  • 2022-02-17
  • 2021-10-01
  • 2021-05-18
  • 2022-12-23
  • 2021-11-29
  • 2021-10-16
  • 2021-09-07
猜你喜欢
  • 2021-11-29
  • 2022-12-23
  • 2022-03-09
  • 2022-12-23
  • 2021-12-06
  • 2021-06-27
相关资源
相似解决方案