Saving python list of numpy arrays of different sizes to .txt-file -


i looking nice way save multiple 1d numpy arrays of different lengths in .txt-file (each array being 1 column). have of them placed in python list. np.columnstack , np.savetxt don't work arrays of different sizes , far way got work filling arrays zeros make them equally long.

i found suggested solution uses dataframes pandas , saves cvs put messes formatting in rows don't have entry in each column.

direct file write might easiest

in [233]: alist=[[1,2,3,4],[.2,.4,1.2],['a','b','c','d','e']] in [234]: import itertools in [235]: list(itertools.zip_longest(*alist)) out[235]:  [(1, 0.2, 'a'),  (2, 0.4, 'b'),  (3, 1.2, 'c'),  (4, none, 'd'),  (none, none, 'e')] in [236]:  in [236]: list(itertools.zip_longest(*alist,fillvalue='')) out[236]: [(1, 0.2, 'a'), (2, 0.4, 'b'), (3, 1.2, 'c'), (4, '', 'd'), ('', '', 'e')] 

now write formatted lines (substitute file.write print):

in [238]: line in itertools.zip_longest(*alist,fillvalue=''):      ...:     print('%5s, %5s, %5s'%tuple(line))      ...:          1,   0.2,         2,   0.4,     b     3,   1.2,     c     4,      ,     d      ,      ,     e 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -