-1

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

3
  • 1
    what did you try so far? Commented Oct 15, 2014 at 15:37
  • dimension of the array varies depending on the number of elements (in the example of the number itemid) Commented Oct 15, 2014 at 15:39
  • 1
    Please post your code... Commented Oct 15, 2014 at 15:41

1 Answer 1

0

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']]
1
  • NameError: global name 'sub_list' is not defined Commented Oct 15, 2014 at 15:50

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.