在字符串列表上拆分算法

时间:2018-01-14 15:45:28

标签: python

我正在寻找一种方法来生成具有任意随机字符串列表和预期输出字符串的拆分算法。预期的输出字符串每次都会有一个“:”分隔符。

foo@mail.com
bar@mail.com
foooo@3robosa.com
foooodf@sdfrobosa.com
foooo@dsdfrobosa.com
foooo@ferobosa.com
foooo@rodbosa.com
foooo@rodsfa.com
lalsdfaslsad
lasdfl1241aslsad
lalsdfsdfas241lsad
ladsfsdflas241lsad
lalas241lsad
lalas241lsad
lalas241lsad
laldsfsdfas241lsad

当前输出:

foo@mail.com:lalsdfaslsad
bar@mail.com:lasdfl1241aslsad
foooo@3robosa.com:lalsdfsdfas241lsad
foooodf@sdfrobosa.com:ladsfsdflas241lsad
...

预期产出:

!(n<0 && n%2==0) || !(n>0 && n%3==0)

提前谢谢你们

2 个答案:

答案 0 :(得分:0)

a = 'email = "foo@mail.com, info = "lalsdfaslsad"))'
a = a.split(", ")
print ":".join([a[0].split("=")[-1],  a[1].split("=")[-1]]).replace("))", "").replace('"', "")

<强>结果:

"foo@mail.com: lalsdfaslsad

答案 1 :(得分:0)

您可以使用re.findall捕获=后显示的所有文字,并加入所需的:

import re
to_split = """
 'email = "foo@mail.com, info = "lalsdfaslsad"))'
 'email = "bar@mail.com, info = "lasdfl1241aslsad"))'
 'email = "foooo@3robosa.com, info = "lalsdfsdfas241lsad"))'
 'email = "foooodf@sdfrobosa.com, info = "ladsfsdflas241lsad"))'
 'email = "foooo@dsdfrobosa.com, info = "lalas241lsad"))'
 'email = "foooo@ferobosa.com, info = "lalas241lsad"))'
 'email = "foooo@rodbosa.com, info = "lalas241lsad"))'
 'email = "foooo@rodsfa.com, info = "laldsfsdfas241lsad"))'
 """
new_l = '\n'.join(filter(None, [':'.join(re.findall('(?<=\s\=\s")[a-zA-Z@\.]+', i[:-2])) for i in filter(None, to_split.split('\n'))]))
print(new_l)

输出:

foo@mail.com:lalsdfaslsad
bar@mail.com:lasdfl
foooo@:lalsdfsdfas
foooodf@sdfrobosa.com:ladsfsdflas
foooo@dsdfrobosa.com:lalas
foooo@ferobosa.com:lalas
foooo@rodbosa.com:lalas
foooo@rodsfa.com:laldsfsdfas