Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

please help me to parsing data in two-dimensional array in python. The value massive and dictionary inside example change every time when script run, so length massive don't do constanta.

example data:

[[{u'itemid': u'23296', u'ns': u'733098943', u'value': u'0.0000', u'clock': u'1413386116'}, 
{u'itemid': u'23296', u'ns': u'774481389', u'value': u'0.0000', u'clock': u'1413386176'}], 
[{u'itemid': u'23297', u'ns': u'735958009', u'value': u'0.0100', u'clock': u'1413386117'}, 
{u'itemid': u'23297', u'ns': u'776151521', u'value': u'0.0100', u'clock': u'1413386177'}], 
[{u'itemid': u'23295', u'ns': u'731054106', u'value': u'0.0500', u'clock': u'1413386115'}, 
{u'itemid': u'23295', u'ns': u'772822468', u'value': u'0.0500', u'clock': u'1413386175'}]]

example needed output:

[[0.0000, 0.0000], [0.0100, 0.0100], [0.0500, 0.0500]]

Please help

share|improve this question
1  
what did you try so far? –  unixer 20 hours ago
    
dimension of the array varies depending on the number of elements (in the example of the number itemid) –  somethings went wrong 20 hours ago
1  
Please post your code... –  bruno desthuilliers 20 hours ago

2 Answers 2

up vote 0 down vote accepted

you could use list comprehensions for that

>>> els = [[{u'itemid': u'23296', u'ns': u'733098943', u'value': u'0.0000', u'clock': u'1413386116'}, 
{u'itemid': u'23296', u'ns': u'774481389', u'value': u'0.0000', u'clock': u'1413386176'}], 
[{u'itemid': u'23297', u'ns': u'735958009', u'value': u'0.0100', u'clock': u'1413386117'}, 
{u'itemid': u'23297', u'ns': u'776151521', u'value': u'0.0100', u'clock': u'1413386177'}], 
[{u'itemid': u'23295', u'ns': u'731054106', u'value': u'0.0500', u'clock': u'1413386115'}, 
{u'itemid': u'23295', u'ns': u'772822468', u'value': u'0.0500', u'clock': u'1413386175'}]]

>>> ls = [[dict_["value"] for dict_ in sub_lists] for sub_lists in els]
>>> print ls
[[u'0.0000', u'0.0000'], [u'0.0100', u'0.0100'], [u'0.0500', u'0.0500']]
share|improve this answer
    
NameError: global name 'sub_list' is not defined –  somethings went wrong 20 hours ago
    
I fixed the typo –  greole 20 hours ago
    
Thanks!!! really you help me!! thanks!! –  somethings went wrong 20 hours ago

try this:

a = [[{u'itemid': u'23296', u'ns': u'733098943', u'valk': u'1413386116'}, 
{u'itemid': u'23296', u'ns': u'774481389', u'value': u1413386176'}], 
[{u'itemid': u'23297', u'ns': u'735958009', u'value': '1413386117'}, 
{u'itemid': u'23297', u'ns': u'776151521', u'value': u1413386177'}], 
[{u'itemid': u'23295', u'ns': u'731054106', u'value': '1413386115'}, 
{u'itemid': u'23295', u'ns': u'772822468', u'value': u1413386175'}]]

print [[x['value'] for y in a] for x in y]
share|improve this answer
    
UnboundLocalError: local variable 'y' referenced before assignment –  somethings went wrong 20 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.