擅长:python、mysql、java
<p>我会写一个函数,当给定一个字符串时,它返回一个3元组的计数。在</p>
<pre><code>import string
def count_helper(s) -> ("vowel count", "consonant count", "punctuation count"):
vowels = set('aeiou')
consonants = set(string.ascii_lowercase).difference(vowels)
# you could also do set('bcdfghjklmnpqrstvwxyz'), but I recommend this approach
# because it's more obviously correct (you can't possibly typo and miss a letter)
c_vowel = c_consonant = c_punctuation = 0
for ch in s:
if ch in vowels: c_vowel += 1
elif ch in consonants: c_consonant += 1
else: c_punctuation += 1
return (c_vowel, c_consonant, c_punctuation)
</code></pre>
<p>然后在遍历文件时,将每一行传递给<code>count_helper</code>。在</p>
^{pr2}$