Python,如何检查一个项目在数组中出现的次数
我正在制作一个程序,找到文本块中的每个单词,并输出每个单词以及单词的使用次数 我目前的代码如下:Python,如何检查一个项目在数组中出现的次数,python,arrays,text,char,words,Python,Arrays,Text,Char,Words,我正在制作一个程序,找到文本块中的每个单词,并输出每个单词以及单词的使用次数 我目前的代码如下: text = input("Please enter some text ") terminator = len(text) n = 0 word = "" wordlist = [] while len(text) > 0: if word != "": wordlist.append(word) tex
text = input("Please enter some text ")
terminator = len(text)
n = 0
word = ""
wordlist = []
while len(text) > 0:
if word != "":
wordlist.append(word)
text = text[n:]
word = ""
n = 0
for char in text:
if char != " ":
word = word + char
n = n + 1
else:
text = text[1:]
break
for item in wordlist:
print(item)
谢谢:)我会这样做:
import re
from collections import Counter
text = input("Please enter some text ")
text = re.sub(' +', ' ', text)
text = text.split(' ')
counter = Counter(text)
行
text=re.sub('+','',text)
处理用户输入多个连续空格的情况。似乎是集合的工作。计数器
。