Python String Double Splitting?

时间:2015-07-28 13:58:39

标签: python list sorting numbers

所以继续我的问题我得到一个像这样的字符串

01000200030004020511050006000702051108020511090205111002051111020511120205111300140205111500160017001800190020002100

然后我必须将它分成最终看起来像这样的东西

[['01', 00], ['02', 00], ['03', 00], ['04', 020511], ['05', 00], ['06', 00], ['07', 020511, ['08', 020511], ['09', 020511], ['10', 020511,], ['11', 020511], ['12', 020511], ['13', 00], ['14', 020511], ['15', 00], ['16', 00], ['17', 00], ['18', 00], ['19', 00], ['20', 00], ['21', 00]]

所以首先我想让我们尝试使用拆分,也许这会有效,所以这是我的第一次尝试,它就像这样出来了

['01', '02', '03', '0402051105', '06', '0702051108020511090205111', '20511110205111202051113', '1402051115', '16', '17', '18', '19', '2', '021', '']

在那次失败之后,我虽然对自己说我将不得不分裂两次以摆脱'00'和'020511',所以我使用了方法

re.split('020511|00', theStr)

做那个方法我得到了回来......

['01', '02', '03', '04', '05', '06', '07', '08', '09', '1', '2051111', '12', '13', '14', '15', '16', '17', '18', '19', '2', '021', '']

但它不是我想要它的格式而且它没有正确出现,当我拆分时它将摆脱我想说的'10'将出现为1的值因为程序分裂了0和我不完全确定如何为此提出解决方案所以任何帮助都是apreciated ..谢谢。

1 个答案:

答案 0 :(得分:5)

您可以使用re.findall()查找长度为2的数字,后跟00020511

>>> re.findall('(\d{2})(020511|00)', theStr)
[('01', '00'), ('02', '00'), ('03', '00'), ('04', '020511'), ('05', '00'), ('06', '00'), ('07', '020511'), ('08', '020511'), ('09', '020511'), ('10', '020511'), ('11', '020511'), ('12', '020511'), ('13', '00'), ('14', '020511'), ('15', '00'), ('16', '00'), ('17', '00'), ('18', '00'), ('19', '00'), ('20', '00'), ('21', '00')]
>>> 

如果你想在列表中得到结果,你可以使用re.finditer返回迭代器和列表推导来将相关组转换为列表:

>>> [list(i.group(1,2)) for i in re.finditer('(\d{2})(020511|00)', theStr)]
[['01', '00'], ['02', '00'], ['03', '00'], ['04', '020511'], ['05', '00'], ['06', '00'], ['07', '020511'], ['08', '020511'], ['09', '020511'], ['10', '020511'], ['11', '020511'], ['12', '020511'], ['13', '00'], ['14', '020511'], ['15', '00'], ['16', '00'], ['17', '00'], ['18', '00'], ['19', '00'], ['20', '00'], ['21', '00']]
>>>