l = ['a','b','c','d','e']
m = {}
s = input("Enter input string:")
for f in l:
for k in range(len(s)):
if f == s[k]:
if f in m:
m[f] = m[f] + 1
else:
m[f] = 1
for j in m.keys():
print(s,"has")
print(m[j],j)
import collections
lst = ['a','b','c','d','e']
word = 'food'
word_count = collections.Counter(word)
for letter in lst:
count = word_count.get(letter)
if count:
print(f"There is {count} {letter}")
Python本机方式:
您可以使用
collections.Counter()
计算每个字母的出现次数,然后在lst
上迭代并输出每个数字出现的次数相关问题 更多 >
编程相关推荐