这是我的函数的代码:
def calcVowelProportion(wordList):
"""
Calculates the proportion of vowels in each word in wordList.
"""
VOWELS = 'aeiou'
ratios = []
for word in wordList:
numVowels = 0
for char in word:
if char in VOWELS:
numVowels += 1
ratios.append(numVowels/float(len(word)))
Run Code Online (Sandbox Code Playgroud)
现在,我正在使用超过87,000个单词的列表,这个算法显然非常慢.
有一个更好的方法吗?
编辑:
我测试了以下类提供的算法@ExP:
import time
class vowelProportions(object):
"""
A series of methods that all calculate the vowel/word length ratio
in a list of words.
"""
WORDLIST_FILENAME = "words_short.txt"
def __init__(self):
self.wordList = self.buildWordList()
print "Original: " + str(self.calcMeanTime(10000, self.cvpOriginal, …Run Code Online (Sandbox Code Playgroud) 0 == false并且'0' == false都'真'
然而,(true && 0)是'假',而(true && '0')'是'.
为什么?