python - Zip() an already initialised list -


i'm trying write function writes numpy arrays file. use pythonic method of using zip(). following code not work; trying convert **data_lists named arguments (of values 1d numpy arrays , keys labels) list of arrays required zip function.

def write_data_file(filename,**data_lists):     open("{}.dat".format(filename), "w") f:         header = "#"                 lst in data_lists:             header = "{} {} \t".format(header,lst)         header = "{} \n".format(header)         f.write(header)         # following line of code not work         row in zip(data_lists[lst] lst in data_lists):              f.write("\t".join([str(item) item in row])+ "\n") 

i can't assign list first below:

trial = [data_lists[lst] lst in data_lists] zip(trial)  

this nothing trial treated single argument. how can zip() treat items in trial individually or otherwise complete this?

change:

zip(data_lists[lst] lst in data_lists) 

to:

zip(*data_lists.values()) 

that unpacks values of data_lists dict sequential positional arguments. * known "splat" or iterable unpacking operator.

it's little odd though, since order of values not defined; you'd zip in different order depending on python version (and within version, you'd zip in different orders run run). you're using iteration order define header, columns match headers every time, order of columns differ run run. might want try receiving headers separate argument, followed values positionally, e.g.:

def write_data_file(filename, headers, *data_lists):     if len(headers) != len(data_lists): ... raise exception mismatched lengths ... 

then you'd doing zip(*data_lists) (no .values() call), , getting consistent header ordering.


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -