编写一个循环,它总结wordList中单词出现在列表中的次数

时间:2014-10-10 23:40:17

标签: python loops for-loop

count = 0
for word in newWordList:
    newWordList= word.count('theBells')
    count += 1
print(newWordList)

1 个答案:

答案 0 :(得分:0)

根据我对标题中的问题的解释,并没有太多细节,这是一种方法。 wordList是你要查找的源列表,inList是输入。

count = 0
wordList=["hello","hi","is","and", "are", "you"]

#'are' appears twice and 'you' once - total should be 3
inList=["how", "are","you", "where", "are", "your", "friends" ]
count = 0
#count number of times the words in wordList appear in the input list 'inList'
for word in inList:
    if word in wordList:
        count += 1
print(count)

<强>输出:

3