2014-03-28 51 views
-4

我需要打印用户指定的句子中有多少个字符,打印出一个句子中有多少单词用户指定并打印每个单词,单词中的字母数以及单词中的第一个和最后一个字母。这可以做到吗?我如何获得一个程序来打印句子中的单词数量和每个单词的顺序

+3

当然是可以做到的。你到目前为止的尝试是什么? – msvalkon

+0

高清count_letter(字): \t打印(LEN(字)) \t列表= word.split() \t 高清的main(): \t句子=输入( “请输入你的句子。”) \t count_letter (句子) \t main() – user3285386

回答

2

我要你把你的时间,并了解在下面的代码怎么回事,我建议你阅读这些资源。

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 
1

请阅读Python的字符串文档,这是不言自明的。这里有一些评论的不同部分的简短解释。

我们知道一个句子是由单词组成的,每个单词都由字母组成。我们首先要做的就是把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])) 
1

我们不会为你堆栈溢出做你的功课......但我会让你开始。

你需要的最重要的方法是这两个(取决于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' 

,你可以看到,文中写道在被stroed的my_sentance变量

要获得字符串中的字符的数量,你需要明白一个字符串实际上只是一个列表!所以如果你想知道你可以使用的字符数量:

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

最后你将需要使用一个内置的函数的字符串:

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