Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python,如何检查一个项目在数组中出现的次数_Python_Arrays_Text_Char_Words - Fatal编程技术网

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)
处理用户输入多个连续空格的情况。

似乎是
集合的工作。计数器