擅长:python、mysql、java
<p>您可以使用<a href="https://www.python.org/dev/peps/pep-0274/" rel="nofollow noreferrer">Dict Comprehensions</a>和<a href="https://docs.python.org/3/library/stdtypes.html#str.count" rel="nofollow noreferrer">str.count</a>:</p>
<pre><code>def count_letters(text, letters):
lower_text = text.lower()
return {c: lower_text.count(c) for c in letters}
print(count_letters("Sample String", "sao"))
result: {'s': 2, 'a': 1, 'o': 0}
</code></pre>