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
Post a Comment