擅长:python、mysql、java
<p>这并不是对您的问题的回答(因为user2464424已经回答了这个问题),而是对未来的一些提示。您的函数可以更简洁地表达为:</p>
<pre><code>def calculation(text, alphabet):
total = float(sum(ch in alphabet for ch in text))
return [100 * sum(ch == letter for ch in text) / total for letter in alphabet]
</code></pre>
<p>这演示了Python的<a href="https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions" rel="nofollow">list comprehension</a>、<a href="https://docs.python.org/2/reference/expressions.html#generator-expressions" rel="nofollow">generator expressions</a>,以及booleans <a href="https://docs.python.org/2/library/stdtypes.html#truth-value-testing" rel="nofollow">are integers</a>的事实。你知道吗</p>
<p>它还修复了程序中的一个错误,即字符总数仅排除空格,而不排除句点,而此版本排除了给定字母表中未显式显示的所有字符。(当前函数返回的列表总和不是100。)</p>