我需要打印用户指定的句子中有多少个字符,打印出一个句子中有多少单词用户指定并打印每个单词,单词中的字母数以及单词中的第一个和最后一个字母。这可以做到吗?我如何获得一个程序来打印句子中的单词数量和每个单词的顺序
-4
A
回答
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
变量
要获得字符串中的字符的数量,你需要明白一个字符串实际上只是一个列表!所以如果你想知道你可以使用的字符数量:
len(s)
,...返回一个对象的长度(项目数)。 参数可能是一个序列(字符串,元组或列表)或映射 (字典)。 http://docs.python.org/3/library/functions.html#len
我会让你弄清楚如何使用它。
最后你将需要使用一个内置的函数的字符串:
str.split([sep[, maxsplit]])
...返回在 字符串中的单词的列表,使用月作为分隔符串。如果给出maxsplit,则在 处完成大多数maxsplit分割(因此,列表将最多有 maxsplit + 1个元素)。如果未指定maxsplit或-1,那么 对分割数量没有限制(所有可能的分割)。 http://docs.python.org/2/library/stdtypes.html#str.split
相关问题
- 1. 我怎样才能让程序打印一个句子中的单词数量?
- 2. 打印每个单词的第一个字母一个句子
- 3. 如何每秒打印一个单词?
- 4. 如何在PHP中打印每个单词的单词?
- 5. 每行打印一个单词的程序
- 6. 如何打印句子中每个单词的第一个字母?
- 7. 如何获取此代码从每个数组中选择一个随机单词来打印一个句子?
- 8. c中一个句子中的单词反向排列顺序
- 9. 查找MIPS程序集中的句子中的每个单词
- 10. Python如何打印一个单词所在的句子?
- 11. java程序找到一个句子中每个单词的字母数
- 12. 如何在单词中搜索句子的第一个单词
- 13. 如何用Haskell中的每个单词长度打印一组单词
- 14. 我有一个单词文档。我想获得单词doc每页的字数?
- 15. MySQL:搜索一个句子并忽略单词的顺序
- 16. 获取句子中每个单词的第一个字母?
- 17. 如何获得我列表中每个单词的计数?
- 18. 如何计算一个句子中的单词数量?
- 19. 如何获得scala中每个单词的第一个字母?
- 20. 如何用Perl匹配一个句子中的顺序词?
- 21. 程序打印一个号码的英文单词
- 22. 如何制作程序来计算每个单词的值
- 23. 如何添加(追加)一个单词到同一个句子,每个单词使用一个按钮(单击)
- 24. REGEX以反向打印每个单词
- 25. 如何输入句子以便程序将句子识别为单个单词?
- 26. 如何获得这个词出现的每个单词的行数(不是ammount)
- 27. 在句子的某个单词的任一侧获取n个单词
- 28. Python如何计算每个词汇单词在句子中显示的次数?
- 29. 我写了一个函数,可以在保持单词顺序的同时,翻转句子中每个单词的字符。我不确定时间复杂度
- 30. 如何为输入的句子中的每个单词分配一个数字?
当然是可以做到的。你到目前为止的尝试是什么? – msvalkon
高清count_letter(字): \t打印(LEN(字)) \t列表= word.split() \t 高清的main(): \t句子=输入( “请输入你的句子。”) \t count_letter (句子) \t main() – user3285386