Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python定义的字符串分割分隔符?_Python_Regex_String_Split - Fatal编程技术网

Python定义的字符串分割分隔符?

Python定义的字符串分割分隔符?,python,regex,string,split,Python,Regex,String,Split,如何以最有效的方式定义用于拆分的字符串分隔符?我的意思是不需要使用很多if等 我有一些字符串需要严格地拆分为两个元素列表。问题是这些字符串有不同的符号,我可以用它们来分割。例如: 'Hello:test1'。这个有拆分分隔符:'。另一个例子是: 'Hello-test1'。所以这个应该是'-'。分割分隔符也可以是'-'或'-'。所以,如果我知道分隔符的所有变体,如何才能最有效地定义它们 首先我做了这样的事情: strings = ['Hello - test', 'Hello- test', '

如何以最有效的方式定义用于拆分的字符串分隔符?我的意思是不需要使用很多if等

我有一些字符串需要严格地拆分为两个元素列表。问题是这些字符串有不同的符号,我可以用它们来分割。例如:

'Hello:test1'
。这个有拆分分隔符
:'
。另一个例子是:
'Hello-test1'
。所以这个应该是
'-'
。分割分隔符也可以是
'-'
'-'
。所以,如果我知道分隔符的所有变体,如何才能最有效地定义它们

首先我做了这样的事情:

strings = ['Hello - test', 'Hello- test', 'Hello -test']
for s in strings:
    delim = ' - '
    if len(s.split('- ', 1)) == 2:
        delim = '- '
    elif len(s.split(' -', 1)) == 2:
        delim = ' -'
    print s.split(delim, 1)[1])

但后来我得到了一个新字符串,它有另一个意外的分隔符。这样做,我应该添加更多的if来检查其他分隔符,如
:'
。但后来我想知道是否有更好的方法来定义它们(如果我以后需要在某种列表中包含新的分隔符,那么就没有问题了)。也许正则表达式或其他工具会有所帮助?

将所有分隔符放入
re.split
函数中,如下所示,使用逻辑或
运算符

re.split(r': | - | -|- ', string)
如果要进行一次性拆分,请添加
maxplit=1

re.split(r': | - | -|- ', string, maxsplit=1)

将所有分隔符放入
re.split
函数中,如下所示,使用逻辑或
|
运算符

re.split(r': | - | -|- ', string)
如果要进行一次性拆分,请添加
maxplit=1

re.split(r': | - | -|- ', string, maxsplit=1)
您可以按此进行拆分。使用
re.split(r“\s*[:-]\s*”,string)
。请参阅演示

如果可以使用诸如
-
-
-
之类的分隔符,则应该使用此分隔符。其中可以有多个空格

您可以按此进行拆分。使用
re.split(r“\s*[:-]\s*”,string)
。请参阅演示

如果您可以使用分隔符,如
-
-
-
。其中可以有多个空格。

您可以使用re模块的功能

>>> strings = ['Hello1 - test1', 'Hello2- test2', 'Hello3 -test3', 'Hello4 :test4', 'Hello5 : test5']
>>> for s in strings:
...   re.split(" *[:-] *",s)
...
['Hello1', 'test1']
['Hello2', 'test2']
['Hello3', 'test3']
['Hello4', 'test4']
['Hello5', 'test5']
[]
之间放置所有可能的分隔符。
*
表示一些空格可以放在前面或后面。

您可以使用re模块的功能

>>> strings = ['Hello1 - test1', 'Hello2- test2', 'Hello3 -test3', 'Hello4 :test4', 'Hello5 : test5']
>>> for s in strings:
...   re.split(" *[:-] *",s)
...
['Hello1', 'test1']
['Hello2', 'test2']
['Hello3', 'test3']
['Hello4', 'test4']
['Hello5', 'test5']

[]
之间放置所有可能的分隔符。
*
表示一些空格可以放在前面或后面。

这不是最好的方法,但是如果您想避免出于某种(或没有)原因使用
re
,我会这样做:

>>> strings = ['Hello - test', 'Hello- test', 'Hello -test', 'Hello : test']
>>> delims = [':', '-']  # all possible delimiters; don't worry about spaces.
>>>
>>> for string in strings:
...     delim = next((d for d in delims if d in string), None) # finds the first delimiter in delims that's present in the string (if there is one)
...     if not delim:
...         continue  # No delimiter! (I don't know how you want to handle this possibility; this code will simply skip the string all together.)
...     print [s.strip() for s in string.split(delim, 1)]  # assuming you want them in list form.
['Hello', 'test']
['Hello', 'test']
['Hello', 'test']
['Hello', 'test']
这将使用Python本机的
.split()
在分隔符处打断字符串,然后
.strip()
修剪结果中的空白(如果有)。我已经使用
next
找到了合适的分隔符,但是有很多东西可以替换掉(特别是如果你喜欢
块)

如果确定每个字符串将至少包含一个分隔符(最好正好包含一个),则可以将其简化为:

 ## with strings and delims defined...
>>> for string in strings:
...     delim = next(d for d in delims if d in string) # raises StopIteration at this line if there is no delimiter in the string.
...     print [s.strip() for s in string.split(delim, 1)]

我不确定这是否是最优雅的解决方案,但它使用更少的
if
块,并且您不必导入任何东西来执行此操作。

这不是最好的方法,但如果您想出于某种(或无)原因避免使用
re
,我会这样做:

>>> strings = ['Hello - test', 'Hello- test', 'Hello -test', 'Hello : test']
>>> delims = [':', '-']  # all possible delimiters; don't worry about spaces.
>>>
>>> for string in strings:
...     delim = next((d for d in delims if d in string), None) # finds the first delimiter in delims that's present in the string (if there is one)
...     if not delim:
...         continue  # No delimiter! (I don't know how you want to handle this possibility; this code will simply skip the string all together.)
...     print [s.strip() for s in string.split(delim, 1)]  # assuming you want them in list form.
['Hello', 'test']
['Hello', 'test']
['Hello', 'test']
['Hello', 'test']
这将使用Python本机的
.split()
在分隔符处打断字符串,然后
.strip()
修剪结果中的空白(如果有)。我已经使用
next
找到了合适的分隔符,但是有很多东西可以替换掉(特别是如果你喜欢
块)

如果确定每个字符串将至少包含一个分隔符(最好正好包含一个),则可以将其简化为:

 ## with strings and delims defined...
>>> for string in strings:
...     delim = next(d for d in delims if d in string) # raises StopIteration at this line if there is no delimiter in the string.
...     print [s.strip() for s in string.split(delim, 1)]

我不确定这是否是最优雅的解决方案,但它使用更少的
if
块,而且您不必导入任何东西来执行此操作。

这似乎是定义的最佳方式,因为您可以明确定义分隔符是什么。抱歉,我不知道您的意思。您还可以更新您的答案,添加
maxplit=1
,我询问如何将其拆分为两个元素列表。因此,在第一次拆分之后,它不应该再拆分该字符串。我的意思是,使用其他答案,分隔符可以变成例如
:-
,即使您不想这样,然后它会错误地拆分。这似乎是定义的最佳方式,因为您可以明确定义分隔符是什么。抱歉,我不知道您的意思。您也可以更新您的答案,添加
maxplit=1
,我询问如何将其拆分为两个元素列表。因此,在第一次拆分之后,它不应该再拆分该字符串。有了这个注释,我的意思是,使用其他答案,分隔符可以变成例如
:-
,即使你不想这样,它也会被错误地分割。如果你在
块示例中使用第一个
,并且不知何故被要求使用零作为分隔符,请确保使用字符串
'0'
,而不是整数
0
,由于整数为false,如果不是delim,则
将在delim==0时运行。如果delim不是None,您可能还想将
更改为
,如果delim是None:
(或者您将
下一个
的默认设置改为
);总的来说,这是一个更安全的选择。如果您对
块示例使用第一个
,并且不知何故被要求使用零作为分隔符,请确保使用字符串
'0'
,而不是整数
0
,因为整数是假的,如果delim==0,则运行
。如果delim不是None,您可能还想将
更改为
,如果delim是None:
(或者您将
下一个
的默认设置改为
);总的来说,这是一个更安全的选择。