修复python中的基本单词计数

问题描述 投票:0回答:8

所以我的代码有问题,我的字数总是等于“4”,每当我键入不同数量的单词时,这都是不准确的。

这是我的代码:

word=raw_input("Enter your string please: ")
count=0
for i in "word":
    count += 1
    if word == " ":
        print(count) 
print "Your word count:", count
print "Your character count:", (len(word))

样本输出:

Enter your string please: ched hcdbe checbj
Your word count: 4
Your character count: 17

我的角色计数完全正常,这只是我的字数。我对我需要解决的问题感到困惑。非常感谢帮助!

python string count word-count
8个回答
0
投票

问题是你在“word”上迭代为一个包含4个字符的字符串。

计算单词还有另一个问题,计数单词和计数字符的输出将是相同的。

这是一个较短的固定代码:

word=raw_input("Enter your string please: ")
print ("Your word count:", len(word.split()))
print ("Your character count:", len(word))

输出:

Enter your string please: This is a Test
Your word count: 4
Your character count: 14

2
投票

调试:

以下行无效,因为您正在迭代字符串字中的每个元素而不是用户输入:

for i in "word":

本来应该:

for i in word:

完全修复(使用两个单独的变量进行单词和字符计数):

word= input("Enter your string please: ")
charCount = 0
wordCount = 0
for i in word:
    charCount += 1
    if i == ' ':
        wordCount += 2
print("Your character count:", charCount)
print("Your word count:", wordCount)

现在,更短的方式:

使用str.format()len()

word = input("Enter your string please: ")

print("Total words: {}".format(len(word.split())))
print("Total Characters: {}".format(len(word)))

OUTPUT:

Enter your string please: hey, how're you?
Total words: 3
Total Characters: 16

1
投票

“word”是可迭代的字符串对象,您正在遍历字符串“word”的每个符号,尝试更改word.split()的“word”并通过len()方法获取计数:

word = input("Enter your string please: ")
print("Your word count:", len(word.split()))
print("Your character count:", len(word))

1
投票

for循环的语句是迭代字符串“word”而不是你保存为输入的变量字。另外,你的if语句是单词,而不是迭代器。

word=input("Enter your string please: ")
new_word = word.lstrip(" ").rstrip(" ")
new_word += ' '
count=0
for i in new_word:
    if i == " ":
        count += 1
print(count) 
print("Your word count: ", count)
print("Your character count: ", (len(new_word.rstrip(' '))))
print("Your character count: ", (len(word)))

0
投票

你实际上在迭代单词“word”

如果你想得到一个单词列表,你应该使用split关键字。

>>> words = 'this is a test sentence'
>>> word_list = words.split()
>>> print(len(word_list))
5

0
投票
inp = raw_input("Enter your string: ")

word_count = len(inp.split())
chr_count = len(inp)

print(word_count, chr_count)

例:

>>> Enter your string: foo bar
>>> 2 7

0
投票

您将遍历每个“单词”字母,这将导致长度为4.您希望在修剪后将输入字符串拆分为空格,然后获取结果列表的长度。

word=raw_input("Enter your string please: ")
print("Your word count: %s", len(word.strip().split(" ")))
print("Your character count: %s", (len(word)))

0
投票

您还可以从集合中的计数器中获取一些价值。

from collections import Counter

my_string = "xxx yyy xxx"
c = Counter(my_string.split(' '))

print("number of words:", len(my_string.split(' ')))
print("number of characters:", len(my_string))
print("number of unique words:",len(c))
print("most common word:" , c.most_common()[0][0])
print("least common word:", c.most_common()[-1][0])

得到:

number of words 3
number of characters 11
number of unique words: 2
most common word: xxx
least common word: yyy
© www.soinside.com 2019 - 2024. All rights reserved.