0

我有一个这种类型的字符串:

s = "//name#1  un2 //name2#1  u13 //name3#3"

我怎样才能得到这些字符串数组:

s1 [] = "//name, //name2, name3"
s2 [] = "1, 2, 3"
4

2 回答 2

2

只需一个正则表达式即可:

>>> s = "//name#1  un2 //name2#1  u13 //name3#3"
>>> import re
>>> x = re.findall(r"(\S+)#(\S+)", s)
>>> [s1, s2] = [', '.join(y) for y in zip(*x)]
>>> s1
'//name, //name2, //name3'
>>> s2
'1, 1, 3'
于 2012-04-19T18:55:33.770 回答
0

可能你正在寻找

>>> s1=", ".join(re.findall("(//.*?)#\d+",s))
>>> s2=", ".join(re.findall("#(\d+)",s))
>>> s1
'//name, //name2, //name3'
>>> s2
'1, 1, 3'
>>> 
于 2012-04-19T19:02:34.453 回答