group element by two python list -


i want write elements group of 2 list txt file using list comprehension.

datacolumn =    ['a1', -86, 'a2', 1839, 'a3', 2035, 'a4', 1849, 'a5', 1714, ...] 

so filename.txt =

a1 -86 a2 1839 a3 2035 a4 1849 a5 1714 ... 

i found solution writing element 1 column :

with  open('filename.txt','w') f:      f.writelines( "%s\n" % item item in datacolumn)       

but can't figure out how 2 elements @ time. did loop :

with  open('filename.txt','w') f:      in range(0,size(datacolumn),2):             f.write(str(datacolumn[i])+"\t"+str(datacolumn[i+1])+"\n")  

but prefer use comprehension list. i'm using python 2.7.

res = [ "{}\t{}\n".format(x,y)            (x,y) in zip(datacolumn[0::2], datacolumn[1::2])] 

...will give list of rows, formatted seem require.

note assumes there pairs format.


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -