Python 如何获取句子中单词的长度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22749706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:35:27  来源:igfitidea点击:

How to get the length of words in a sentence?

python

提问by Reymy

I am trying to get the length of each word in a sentence. I know you can use the "len" function, I just don't know how to get the length of each word.

我正在尝试获取句子中每个单词的长度。我知道你可以使用“len”函数,我只是不知道如何获得每个单词的长度。

Instead of this

而不是这个

>>> s = "python is pretty fun to use"
>>> len(s)
27
>>>

I'd like to get this

我想得到这个

6, 2, 6, 3, 2, 3

which is the actual length of every word.

这是每个单词的实际长度。

采纳答案by óscar López

Try this, using map()for applying len()over each word in the sentence, understanding that split()creates a list with each word in the sentence:

试试这个,map()用于应用len()句子中的每个单词,理解为句子中的每个单词split()创建一个列表:

s = "python is pretty fun to use"
map(len, s.split())       # assuming Python 2.x
list(map(len, s.split())) # assuming Python 3.x

Or alternatively, you can use a list comprehensionfor the same effect:

或者,您可以使用列表理解来达到相同的效果:

[len(x) for x in s.split()]

In both cases the result is a list with the length of each word in the sentence:

在这两种情况下,结果都是一个包含句子中每个单词长度的列表:

[6, 2, 6, 3, 2, 3]

回答by óscar López

Use map1and str.split:

使用1和:mapstr.split

>>> s = "python is pretty fun to use"
>>> map(len, s.split())
[6, 2, 6, 3, 2, 3]
>>>


1Note: mapreturns an iterator in Python 3. If you are using that version, you may want to place it in listto get a list of integers like the Python 2 mapreturns:

1注意map在 Python 3 中返回一个迭代器。如果您使用该版本,您可能希望将其放入list以获取整数列表,如 Python 2map返回:

>>> # Python 3 interpreter
>>> s = "python is pretty fun to use"
>>> map(len, s.split())
<map object at 0x02364ED0>
>>> list(map(len, s.split()))
[6, 2, 6, 3, 2, 3]
>>>

回答by agconti

Use this:

用这个:

s = "python is pretty fun to use"
[len(x) for x in s.split()]

example output:

示例输出:

>>> [len(x) for x in s.split()]
[6, 2, 6, 3, 2, 3]

What's going on in the background?

后台发生了什么?

s.split()breaks on the white space in the string and returns each word in the sentence in a list:

s.split()在字符串中的空白处中断并以列表形式返回句子中的每个单词:

>>> s.split()
['python', 'is', 'pretty', 'fun', 'to', 'use']

Then we take the len()of each of those words to get the word's length. After that, we take each length and append it to a list so that it can be conveniently returned as the result.

然后我们取len()每个单词的 得到单词的长度。之后,我们获取每个长度并将其附加到列表中,以便可以方便地将其作为结果返回。

That all happens in this list comprehension:

这一切都发生在这个列表理解中

[len(x) for x in s.split()]

Still a little confused? This is conceptually the same thing just broken down more explicitly:

还是有点迷茫?这在概念上是同一件事,只是更明确地分解了:

results = []
for x in s.split():
    word_length = len(x)
    results.append(word_length)
print results 

If you'd like them printed out separately, like in your question, use:

如果您希望将它们分开打印,就像在您的问题中一样,请使用:

for x in [len(x) for x in s.split()]: 
    print x

回答by JerryPlayz101

iCodez is right - but it requires a few minor adjustments from script form.

iCodez 是对的 - 但它需要对脚本形式进行一些小的调整。

s = 'python is pretty fun to use'
li = list(map(len, s.split()))

then type into the shell or print statement:

然后在 shell 或 print 语句中输入:

print(li)