【问题标题】:Fixing a basic word count in python修复python中的基本字数
【发布时间】:2019-08-12 08:26:45
【问题描述】:

所以我的代码有问题,我的字数总是等于“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

我的字数非常好,这只是我的字数。我很困惑我需要解决什么。非常感谢您的帮助!

【问题讨论】:

  • for i in "word"?您正在遍历字符串 'word',它有 4 个字符。请参阅变量 word
  • 哦,所以我把引号去掉了?
  • 但是当我只有 3 个单词时它告诉我我有 17 个单词 :(
  • 是的,没错。请注意,您没有计算单词。您必须为此拆分字符串。所以更像len(word.split())
  • 那么我该如何合并它呢?

标签: python string count word-count


【解决方案1】:

调试:

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

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)))

输出:

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

【讨论】:

    【解决方案2】:

    "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))
    

    【讨论】:

      【解决方案3】:

      您的 for 循环语句正在遍历字符串“word”,而不是您保存为输入的变量 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)))
      

      【讨论】:

      • 您通过在末尾附加一个额外的空格使逻辑复杂化,然后您将其剥离以打印其长度。它还确保计数始终至少为 1,即使字符串为空且字符计数为 0。
      【解决方案4】:

      问题是您将“word”作为字符串进行迭代,其中包含 4 个字符。

      计算你的单词还有一个问题,count words和count characters的输出会是一样的。

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

      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
      

      【讨论】:

        【解决方案5】:

        你实际上是在遍历单词“word”

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

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

        【讨论】:

          【解决方案6】:
          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
          

          【讨论】:

            【解决方案7】:

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

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

            【讨论】:

              【解决方案8】:

              您还可以从集合中的 Counter 中获取一些值。

              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
              

              【讨论】:

                猜你喜欢
                • 2022-01-23
                • 1970-01-01
                • 2017-07-28
                • 2011-10-22
                • 2011-04-16
                • 1970-01-01
                • 2019-06-17
                • 1970-01-01
                • 2017-03-05
                相关资源
                最近更新 更多