Python中count()方法应用汇总
#1---------------- sentence="This is is an example cutting." for word in sentence.split(): print(word,sentence.count(word)) #2---------------- sentence="This is is an example cutting." setSentence=set(sentence.split()) listSentence=list(setSentence) for word in listSentence: print(word,sentence.count(word)) #3-------------------- sentence="This is is an example cutting." listSentence=sentence.split() dictSentence={} for word in listSentence: dictSentence[word]=listSentence.count(word) print(dictSentence)
切记:列表(或元组)的count()与字符串的count()的意思类似,但又不同。