给定以下字符串:
'hello0192239world0912903spam209394'
我希望能够将上面的字符串拆分成这个
hello, 0192239, world, 0912903, spam, 209394
最好以列表结尾:
[hello, 0192239], [world, 0912903], [spam, 209394]
但我只是不知道如何进行第一步,按单词 x 数拆分。我知道有 split 方法和一种叫做正则表达式的东西,但我不知道如何使用它,即使它是正确的使用方法
尝试这个:
>>> lst = re.split('(\d+)','hello0192239world0912903spam209394')
>>> list(zip(lst[::2],lst[1::2]))
[('hello', '0192239'), ('world', '0912903'), ('spam', '209394')]
>>> lst = re.split('(\d+)','09182hello2349283world892')
>>> list(zip(lst[::2],lst[1::2]))
[('', '09182'), ('hello', '2349283'), ('world', '892')]
# as a list
>>> list(map(list,zip(lst[::2],lst[1::2])))
[['', '09182'], ['hello', '2349283'], ['world', '892']]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句