如何让我的代码区分单词和单数字符? (Python)

How do I make my code differentiate between words and singular characters? (Python)

(Python) 我的任务是创建一个收集 input() 并将其放入字典的程序。对于文本中的每个单词,它都会计算它之前出现的次数。我的代码:

text = input()

words = {}

for word in text:
    if word not in words:
        words[word] = 0
        print(words[word])

    elif word in words:
        words[word] = words[word] + 1
        print(words[word])

示例输入可以是:

one two one two three two four three

正确的输出应该是:

0
0
1
1
0
2
0
1

然而,我的代码计算每个字符的出现次数,而不是每个单词的出现次数,这使得输出太长。 如何让它区分单词和字符?

那是因为 text 是一个字符串,遍历字符串就是遍历字符。您可以使用 for word in text.split(),这会将字符串拆分为一个列表。默认情况下,它会根据空格进行拆分,因此会在此处将其拆分为单词列表。

鉴于您的示例输入,您需要在空格上拆分 text 才能获得单词。通常,将任意文本拆分为 words/tokens 的问题并不简单;有很多专门为此而构建的自然语言处理库。

此外,对于计数,内置集合模块中的 Counter class 非常有用。

from collections import Counter

text = input()
word_counts = Counter(w for w in text.split())
print(word_counts.most_common())

输出

[('two', 3), ('one', 2), ('three', 2), ('four', 1)]

您正在寻找从字符串类型拆分出来的函数:https://docs.python.org/3/library/stdtypes.html?highlight=str%20split#str.split

用它来创建单词数组:

splitted_text = text.split()

完整示例如下所示:

text = 'this is an example and this is nice'

splitted_text = text.split()

words = {}

for word in splitted_text:
   if word not in words:
      words[word] = 0
    
   elif word in words:
      words[word] = words[word] + 1
print(words)

将输出:

{'this': 1, 'is': 1, 'an': 0, 'example': 0, 'and': 0, 'nice': 0}