Python 获取字符串中的第 x 个单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22767509/
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:37:37  来源:igfitidea点击:

Python get the x first words in a string

pythonstring

提问by Guillaume

I'm looking for a code that takes the 4 (or 5) first words in a script. I tried this:

我正在寻找在脚本中使用 4(或 5)个第一个单词的代码。我试过这个:

import re    
my_string = "the cat and this dog are in the garden"    
a = my_string.split(' ', 1)[0]
b = my_string.split(' ', 1)[1]

But I can't take more than 2 strings:

但我不能带超过 2 个字符串:

a = the
b = cat and this dog are in the garden

I would like to have:

我想拥有:

a = the
b = cat
c = and
d = this
...

采纳答案by bosnjak

The second argument of the split()method is the limit. Don't use it and you will get all words. Use it like this:

split()方法的第二个参数是限制。不要使用它,你会得到所有的话。像这样使用它:

my_string = "the cat and this dog are in the garden"    
splitted = my_string.split()

first = splitted[0]
second = splitted[1]

...

Also, don't call split()every time when you want a word, it is expensive. Do it once and then just use the results later, like in my example.
As you can see, there is no need to add the ' 'delimiter since the default delimiter for the split()function (None) matches all whitespace. You can use it however if you don't want to split on Tabfor example.

另外,不要split()每次都想说一句话就打电话,这很贵。做一次,然后再使用结果,就像在我的例子中一样。
如您所见,无需添加' '分隔符,因为split()函数 ( None)的默认分隔符与所有空格匹配。但是,如果您不想拆分,则可以使用它Tab

回答by Two-Bit Alchemist

You can use slice notationon the list created by split:

您可以在 split 创建的列表上使用切片符号

my_string.split()[:4] # first 4 words
my_string.split()[:5] # first 5 words

N.B. these are example commands. You should use one or the other, not both in a row.

注意这些是示例命令。您应该使用其中一个,而不是连续使用。

回答by kojiro

You can split a string on whitespace easily enough, but if your string doesn't happen to have enough words in it, the assignment will fail where the list is empty.

您可以很容易地在空格上拆分字符串,但是如果您的字符串中没有足够的单词,则分配将在列表为空时失败。

a, b, c, d, e = my_string.split()[:5] # May fail

You'd be better off keeping the list as is instead of assigning each member to an individual name.

您最好按原样保留列表,而不是为每个成员分配一个单独的名称。

words = my_string.split()
at_most_five_words = words[:5] # terrible variable name

That's a terrible variable name, but I used it to illustrate the fact that you're not guaranteed to get five words – you're only guaranteed to get at mostfive words.

这是一个糟糕的变量名,但我用它来说明这样一个事实,即您不能保证得到五个词——您只能保证最多得到五个词。