def processStrings(userPhrase):
""" This function will accept the phrase as string and will count every
instance of vowel then return a dictionary with key:values of each
vowel and amount of occurrences in string.
"""
vowelCount = {i:userPhrase.count(i) for i in 'AEIOU'}
return (vowelCount)
我是新的堆栈溢出和编程。我写这个函数是作为课堂小程序的一部分,现在很想知道什么是最有效的解决方案。我们被告知在开发一个有效的解决方案时要对适用的概念有很强的理解,据我所知这已经非常接近了,因为它使用了一个字典,count()应该是好的。我想知道我是否遗漏了这样的东西,它必须在整个短语上迭代5次才能得到值,但我不知道是否有更好的解决方案,这意味着更少的处理时间或内存使用,比如说,如果我对一个更大的字符串使用函数,搜索的不仅仅是元音或其他东西。你知道吗
使用
Counter
,然后提取元音的值并复述它们可能会更有效:作为一个班轮:
(正如@stevenRumbalski在评论中所建议的)
相关问题 更多 >
编程相关推荐