1

I have a list in Python that looks like this:

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']

I'd like it to look like this:

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']

How can this be done? How can all elements be combined to one element?

Thanks

3
  • Why do you want the end result to be a 1-element list rather than just a string? There are valid uses for a 1-element list, but here, it seems better to just use the string. Commented Apr 24, 2014 at 17:34
  • You're right...I made a mistake in my original post! Please see the update. Sorry to those who answered what was a rather foolish question! Commented Apr 24, 2014 at 17:36
  • So you don't actually want to merge the strings; you want to split them on commas and merge the resulting lists. (I suspect it'd actually be more useful to split on commas and then not merge, so you have a list of lists of strings.) Commented Apr 24, 2014 at 17:53

1 Answer 1

2

As per your latest edit, you need to split the list based on ,, so you can do

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

Output

['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']

Note: According to PEP-0008, Python Style Bible, using a variable name with an initial capital letter is not encouraged.

Sign up to request clarification or add additional context in comments.

6 Comments

Please see my comment @mhlester
have at it @thefourtheye :)
@mhlester Anyway, OPs edit completely changed the meaning of the question :(
Almost...Notice how, for instance, the second element 'test' is split into 4 elements 't' 'e' 's' 't' ['H', '', '', 't', 'e', 's', 't', '', '', '0', '', '', '0', '4', '/', '0', '9', '/', '2', '0', '1', '4', '', '', '1', '5', ':', '1', '9', ':', '3', '5', '', '', '1', '5', ':', '2', '8', ':', '3', '0', '', '', '0', '.', '0', '7', '', '', '0', '', '', '0', '', '', 'a', 'v', 'o', '', '', '1', '', '', 'N', '', '', '2', '', '', '1', '', '', '2', '4', '0', '.', '0', '0', '', '', '3', '0', '.', '0', '0', '', '', '1', '', '', '3', '', '', '1', '', '', '', '', '', '', '',
@BrocolliRob Please check the output I got
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.