【问题标题】:How do I get a program to print the number of words in a sentence and each word in order如何让程序按顺序打印句子中的单词数和每个单词
【发布时间】:2014-03-28 22:53:31
【问题描述】:

我需要打印用户指定的句子中有多少个字符,打印用户指定的句子中有多少个单词,并打印每个单词,单词中的字母数以及第一个和最后一个字母在这个词中。这个可以吗?

【问题讨论】:

  • 当然可以。到目前为止你的尝试是什么?
  • def count_letter(word): print (len(word)) list = word.split() def main(): sentence = input("请输入你的句子。") count_letter(sentence) main ()

标签: python string printing character


【解决方案1】:

我希望您花点时间了解下面的代码中发生了什么,我建议您阅读这些资源。

http://docs.python.org/3/library/re.html

http://docs.python.org/3/library/functions.html#len

http://docs.python.org/3/library/functions.html

http://docs.python.org/3/library/stdtypes.html#str.split

import re


def count_letter(word):
    """(str) -> int

    Return the number of letters in a word.

    >>> count_letter('cat')
    3
    >>> count_letter('cat1')
    3

    """
    return len(re.findall('[a-zA-Z]', word))


if __name__ == '__main__':
    sentence = input('Please enter your sentence: ')
    words = re.sub("[^\w]", " ",  sentence).split()

    # The number of characters in the sentence.
    print(len(sentence))
    # The number of words in the sentence.
    print(len(words))
    # Print all the words in the sentence, the number of letters, the first
    # and last letter.
    for i in words:
        print(i, count_letter(i), i[0], i[-1])

Please enter your sentence: hello user
10
2
hello 5 h o
user 4 u r

【讨论】:

    【解决方案2】:

    请阅读 Python 的字符串文档,它是不言自明的。以下是一些 cmets 不同部分的简短说明。

    我们知道一个句子是由单词组成的,每个单词都是由字母组成的。我们首先要做的就是将split的句子变成单词。这个列表中的每个条目都是一个单词,每个单词都以连续字符的形式存储,我们可以得到它们中的每一个。

    sentence = "This is my sentence"
    
    # split the sentence
    words = sentence.split()
    # use len() to obtain the number of elements (words) in the list words
    print('There are {} words in the given sentence'.format(len(words)))
    # go through each word
    for word in words:
        # len() counts the number of elements again,
        # but this time it's the chars in the string 
        print('There are {} characters in the word "{}"'.format(len(word), word))
    
        # python is a 0-based language, in the sense that the first element is indexed at 0
        # you can go backward in an array too using negative indices.
        #
        # However, notice that the last element is at -1 and second to last is -2,
        # it can be a little bit confusing at the beginning when we know that the second
        # element from the start is indexed at 1 and not 2.
        print('The first being "{}" and the last "{}"'.format(word[0], word[-1]))
    

    【讨论】:

      【解决方案3】:

      我们不会在堆栈溢出时为您做作业......但我会帮助您开始。

      您需要的最重要的方法是以下两种方法之一(取决于 python 的版本):

      • Python3.X - input([prompt]),.. 如果提示参数存在,则写入 到标准输出,没有尾随换行符。那么函数 从输入中读取一行,将其转换为字符串(剥离一个 尾随换行符),并返回。读取 EOF 时,EOFError 为 提高。 http://docs.python.org/3/library/functions.html#input
      • Python2.X raw_input([prompt]),... 如果提示参数是 目前,它被写入标准输出,没有尾随换行符。 然后该函数从输入中读取一行,将其转换为字符串 (剥离尾随换行符),并返回。读取 EOF 时, 引发了 EOFError。 http://docs.python.org/2.7/library/functions.html#raw_input

      你可以像这样使用它们

      >>> my_sentance = raw_input("Do you want us to do your homework?\n")
      Do you want us to do your homework?
      yes
      >>> my_sentance
      'yes'
      

      如您所见,所写的文本在my_sentance 变量中

      要获取字符串中的字符数量,您需要了解字符串实际上只是一个列表!因此,如果您想知道可以使用的字符数:

      我会让你弄清楚如何使用它。

      最后你需要使用一个内置函数来处理字符串:

      • str.split([sep[, maxsplit]]),...返回单词列表 字符串,使用 sep 作为分隔符字符串。如果给出了 maxsplit,则在 大多数 maxsplit 拆分已完成(因此,列表最多有 maxsplit+1 个元素)。如果未指定 maxsplit 或 -1,则 对拆分的数量没有限制(所有可能的拆分都进行了)。 http://docs.python.org/2/library/stdtypes.html#str.split

      【讨论】:

        猜你喜欢
        • 2019-10-31
        • 1970-01-01
        • 1970-01-01
        • 2020-02-18
        • 1970-01-01
        • 2016-05-17
        • 1970-01-01
        • 1970-01-01
        • 2016-07-06
        相关资源
        最近更新 更多