e媒网络

一切皆可能 e媒网络 http://www.eMay.net

博客园 首页 新随笔 联系 订阅 管理

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()的意思类似,但又不同。

posted on 2020-05-12 18:05  e媒网络技术团队  阅读(297)  评论(0编辑  收藏  举报