使用多个分隔符分割python字符串

时间:2021-03-23 22:08:31

I want to split a python string line = '1 -1 2 3 -1 4' so that my output is a python list['1','2','3','4']. I tried solution given here and here. However, some strange output is coming. My code:

我想分割一个python字符串行= '1 - 2 3 -1 4',这样我的输出就是一个python列表['1','2','3','4']。我试过这里和这里的解。然而,一些奇怪的输出即将到来。我的代码:

line = '1 -1 2 3 -1 4'
import re    
t=re.split("-1| ", line)

output:

输出:

['1', '', '', '2', '3', '', '', '4']

Any help is appreciated!

任何帮助都是赞赏!

6 个解决方案

#1


3  

That was a tricky one :)

这是个棘手的问题

re.split(r"(?:\s-1)?\s",line)
#['1', '2', '3', '4']

And the fastest solution (runs about 4.5 times faster than the regex):

以及最快的解决方案(比regex快4.5倍):

line.replace("-1 ", "").split()

#2


1  

Try:

试一试:

t=re.split("\s+",line.replace("-1"," "))

#3


0  

You can try this too with lookahead:

你也可以试着这样做:

print re.findall('(?<=[^-])\d+', ' ' +line)
# ['1', '2', '3', '4']

This is more generic since this will filter out all negative numbers.

这是更一般的,因为这将过滤掉所有的负数。

line = '-10 12 -10 20 3 -2 40 -5 5 -1 1' 
print re.findall('(?<=[^-\d])\d+', ' ' +line)
# ['12', '20', '3', '40', '5', '1']

#4


0  

I'm not using a regex but a conditional list comprehension:

我没有使用正则表达式,而是有条件的列表理解:

>>> line = '1 -1 2 3 -1 4'
>>> [substr for substr in line.split() if not substr.startswith('-')]
['1', '2', '3', '4']

#5


0  

line.replace(' -1','').split(' ')

线。替换(' 1 ',”)。分割(' ')

#6


-1  

I can't say whether this would be more efficient than some of the other solutions, but a list comprehension is very readable and could certainly do the trick:

我不能说这是否比其他一些解决方案更有效,但是一个列表的理解是很容易理解的,并且可以做到这一点:

line = '1 -1 2 3 -1 4'
t = [str(item) for item in line.split(" ") if int(item) >= 0]
>>> ['1', '2', '3', '4']

#1


3  

That was a tricky one :)

这是个棘手的问题

re.split(r"(?:\s-1)?\s",line)
#['1', '2', '3', '4']

And the fastest solution (runs about 4.5 times faster than the regex):

以及最快的解决方案(比regex快4.5倍):

line.replace("-1 ", "").split()

#2


1  

Try:

试一试:

t=re.split("\s+",line.replace("-1"," "))

#3


0  

You can try this too with lookahead:

你也可以试着这样做:

print re.findall('(?<=[^-])\d+', ' ' +line)
# ['1', '2', '3', '4']

This is more generic since this will filter out all negative numbers.

这是更一般的,因为这将过滤掉所有的负数。

line = '-10 12 -10 20 3 -2 40 -5 5 -1 1' 
print re.findall('(?<=[^-\d])\d+', ' ' +line)
# ['12', '20', '3', '40', '5', '1']

#4


0  

I'm not using a regex but a conditional list comprehension:

我没有使用正则表达式,而是有条件的列表理解:

>>> line = '1 -1 2 3 -1 4'
>>> [substr for substr in line.split() if not substr.startswith('-')]
['1', '2', '3', '4']

#5


0  

line.replace(' -1','').split(' ')

线。替换(' 1 ',”)。分割(' ')

#6


-1  

I can't say whether this would be more efficient than some of the other solutions, but a list comprehension is very readable and could certainly do the trick:

我不能说这是否比其他一些解决方案更有效,但是一个列表的理解是很容易理解的,并且可以做到这一点:

line = '1 -1 2 3 -1 4'
t = [str(item) for item in line.split(" ") if int(item) >= 0]
>>> ['1', '2', '3', '4']