前言
嗨喽~大家好呀,这里是魔王呐 ❤ ~!
方式一:
原理:创建一个新的空字典,用循环的方式来获取列表中的每一个元素,判断获取的元素是否存在字典中的key,如果不存在的话,将元素作为key,值为列表中元素的count
# 字典方法
words = [
'my', 'skills', 'are', 'poor', 'I', 'am', 'poor', 'I',
'need', 'skills', 'more', 'my', 'ability', 'are',
'so', 'poor'
]
dict1 = {
}
for i in words:
if i not in dict1.keys():
dict1[i] = words.count(i)
print(dict1)
运行结果:
{
'my': 2, 'skills': 2, 'are': 2, 'poor': 3, 'I': 2,