简体   繁体   English

如何在一个句子中获得单词的长度?

[英]How to get the length of words in a sentence?

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. 这是每个单词的实际长度。

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 comprehension for 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]

Use map 1 and str.split : 使用map 1str.split

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

1 Note : map returns an iterator in Python 3. If you are using that version, you may want to place it in list to get a list of integers like the Python 2 map returns: 1 注意map在Python 3中返回一个迭代器。如果您使用的是该版本,您可能希望将其放在list以获取Python 2 map返回的整数列表:

>>> # 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]
>>>

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. 然后我们取每个单词的len()来得到单词的长度。 After that, we take each length and append it to a list so that it can be conveniently returned as the result. 之后,我们获取每个长度并将其附加到列表中,以便可以方便地返回结果。

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

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM