exesoft

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

分类:

技术点:

相关文章:

  • 2021-11-23
  • 2021-11-23
  • 2021-05-23
  • 2021-11-15
  • 2021-11-23
  • 2021-10-23
  • 2021-12-05
  • 2021-11-03
猜你喜欢
  • 2021-12-11
  • 2021-03-31
  • 2021-11-23
  • 2021-12-05
  • 2020-01-13
  • 2021-12-11
相关资源
相似解决方案