Python习题(统计单词):'统计如下list单词及其出现的次数 alist = ['apple','banana','apple','orange','orange','apple','apple','grape','banana','apple']

时间:2022-06-07 14:44:44

方法一:使用for循环判断。

 1 alist = ['apple','banana','apple','orange','orange','apple','apple','grape','banana','apple']
 2 
 3 new_count = {}
 4 for n in alist:
 5     if n not in new_count:
 6         new_count[n] = 1
 7     else:
 8         new_count[n] += 1
 9         
10 #print(new_count)
11 for key,value in new_count.items():
12     print(str(key) + ":" + str(value))