python - Transforming an array of lists -


consider array of 2 lists:

in [98]: toks out[98]: [['1.423',   '0.046',   '98.521',   '0.010',   '0.000',   '0.000',   '5814251520.0',   '769945600.0',   '18775908352.0',   '2.45024350208e+11',   '8131.903',   '168485.073',   '0.0',   '0.0',   '0.022',   '372.162',   '1123.041',   '1448.424'],  ['71.765',   '0.478',   '27.757',   '0.0',   '0.0',   '0.0',   '5839618048.0',   '769945600.0',   '18776162304.0',   '2.44998729728e+11',   '0.0',   '0.0',   '1640.0',   '1608.0',   '0.0',   '3775.0',   '12858.0',   '6723.0']] 

so convert list point

point = namedtuple('point', 'usr sys idl wai hiq siq  used  buff  cach  free       read  writ recv  send majpf minpf alloc  vmfree') 

doing conversion directly does work:

  in [99]: point(*toks[0]) out[99]: point(usr='1.423', sys='0.046', idl='98.521', wai='0.010', hiq='0.000', siq='0.000', used='5814251520.0',  buff='769945600.0', cach='18775908352.0', free='2.45024350208e+11', read='8131.903', writ='168485.073', recv='0.0',   send='0.0', majpf='0.022', minpf='372.162', alloc='1123.041', vmfree='1448.424') 

but attempting create point via iteration not:

pts = [map(lambda x: point(*x), tokarr) tokarr in toks]   in [90]: pts = [map(lambda x: point(*x), tokarr) tokarr in toks0] --------------------------------------------------------------------------- typeerror                                 traceback (most recent call last) <ipython-input-90-a30764943aa1> in <module>() ----> 1 pts = [map(lambda x: point(*x), tokarr) tokarr in toks0]  <ipython-input-90-a30764943aa1> in <lambda>(x) ----> 1 pts = [map(lambda x: point(*x), tokarr) tokarr in toks0]  typeerror: __new__() takes 19 arguments (2 given) 

i need latter construction because intention iterate on collection of lists , convert each entry point. how this?

i think way tried use map isn't quite right. want lambda applied each element of root list, not each element of each child list, right?

is looking for?

pts = map(lambda x: point(*x), toks) 

Comments