我有清单: lst [a,b,c,d,e]
然后输入: food
我想要输出: there is 1 d
另一个输入示例: aachen
所以输出是: there is 2 a
there is 1 e
大小写无关。
您可以collections.Counter()
用来计算每个字母的出现次数,然后遍历您的字母lst
并输出每个数字出现的次数。
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}")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句