将列表元素组合到python中的一个元素中

时间:2014-04-24 17:27:19

标签: python list merge elements

我在Python中有一个如下所示的列表:

Temp = ['H,test,0,04/09/2014,15:19:35,15:28:30,0.07,0,0,avo,1', 'N,2,1,240.00,30.00,1,3,1,,,,,,,', 'K,0.18,,,,', 'D,,,,,,,,,15,', 'U,35P019,000000,ST,5,U,00,Y,0000,,', 'T,1,Disk,WH,All Element,FL,72,0.062,0.079,0.070,PASS']

我希望它看起来像这样:

Temp = ['H','test','0','04/09/2014','15:19:35','15:28:30','0.07','0','0','avo','1','N','2','1','240.00','30.00','1','3','1','','','','','','','', 'K','0.18','','','','','D','','','','','','','','','15','','U','35P019','000000','ST','5','U','00','Y','0000','','','T','1','Disk','WH','All Element','FL','72','0.062','0.079','0.070','PASS']

如何做到这一点?如何将所有元素组合成一个元素?

由于

1 个答案:

答案 0 :(得分:2)

根据您的最新修改,您需要根据,拆分列表,以便您可以

print [item for items in Temp for item in items.split(",")]

<强>输出

['H',
 'test',
 '0',
 '04/09/2014',
 '15:19:35',
 '15:28:30',
 '0.07',
 '0',
 '0',
 'avo',
 '1',
 'N',
 '2',
 '1',
 '240.00',
 '30.00',
 '1',
 '3',
 '1',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 'K',
 '0.18',
 '',
 '',
 '',
 '',
 'D',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '15',
 '',
 'U',
 '35P019',
 '000000',
 'ST',
 '5',
 'U',
 '00',
 'Y',
 '0000',
 '',
 '',
 'T',
 '1',
 'Disk',
 'WH',
 'All Element',
 'FL',
 '72',
 '0.062',
 '0.079',
 '0.070',
 'PASS']

注意:根据PEP-0008,Python样式圣经,不鼓励使用带有首字母大写字母的变量名称。