gdogg371
gdogg371

Reputation: 4122

Converted nested list into dictionary key and value

I have a set of nested lists returned from a website via an XHR request. The technique used is json.loads on response.text. The nested list looks like this:

[[[[u'goal', u'openplay', u'leftfoot', [2]],
   [u'goal', u'openplay', u'rightfoot', [7]],
   [u'goal', u'owngoal', u'rightfoot', [1]],
   [u'miss', u'corner', u'header', [1]],
   [u'miss', u'corner', u'otherbodypart', [1]],
   [u'miss', u'corner', u'rightfoot', [2]],
   [u'miss', u'crossedfreekick', u'leftfoot', [1]],
   [u'miss', u'crossedfreekick', u'rightfoot', [6]],
   [u'miss', u'openplay', u'header', [2]],
   [u'miss', u'openplay', u'leftfoot', [11]],
   [u'miss', u'openplay', u'rightfoot', [31]],
   [u'miss', u'throwin', u'rightfoot', [1]]]]]

Assuming that this nested list has been put into the variable events I am using the following code:

for y in events[0]:
    for sub in y:
        print sub

I get an output like this:

[u'goal', u'openplay', u'leftfoot', [2]]
[u'goal', u'openplay', u'rightfoot', [7]
...
...
...
[u'miss', u'openplay', u'leftfoot', [11]
[u'miss', u'openplay', u'rightfoot', [31]

However, what I want is to turn the first part of the nested list, i.e. u'goal', u'openplay', u'leftfoot' into the key of a dictionary and the second part of the value, i.e [1]. This would make the above appear like this:

{"'goal', u'openplay', u'leftfoot'":2}
{"'goal', u'openplay', u'rightfoot'":7}
...
...
...
{"'miss', u'openplay', u'leftfoot'":11}
{"'miss', u'openplay', u'rightfoot'":31}

I'm not really sure how to achieve this last step though.

Upvotes: 0

Views: 54

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121296

You can turn the first three elements into a tuple for the key. This can be done with a dictionary comprehension:

{tuple(sub[:3]): sub[3][0] for y in events[0] for sub in y}

So the first 3 elements are made into a tuple key, and the integer from the 4th element is unwrapped for the dictionary value.

Demo:

>>> from pprint import pprint
>>> events = [[[[u'goal', u'openplay', u'leftfoot', [2]], [u'goal', u'openplay', u'rightfoot', [7]], [u'goal', 
... u'owngoal', u'rightfoot', [1]], [u'miss', u'corner', u'header', [1]], [u'miss', u'corner',
...  u'otherbodypart', [1]], [u'miss', u'corner', u'rightfoot', [2]], [u'miss', u'crossedfreekick',
...  u'leftfoot', [1]], [u'miss', u'crossedfreekick', u'rightfoot', [6]], [u'miss', u'openplay', u'header',
...  [2]], [u'miss', u'openplay', u'leftfoot', [11]], [u'miss', u'openplay', u'rightfoot', [31]], [u'miss',
...  u'throwin', u'rightfoot', [1]]]]]
>>> {tuple(sub[:3]): sub[3][0] for y in events[0] for sub in y}
{(u'goal', u'owngoal', u'rightfoot'): 1, (u'miss', u'corner', u'header'): 1, (u'miss', u'corner', u'otherbodypart'): 1, (u'goal', u'openplay', u'leftfoot'): 2, (u'miss', u'openplay', u'rightfoot'): 31, (u'miss', u'corner', u'rightfoot'): 2, (u'miss', u'crossedfreekick', u'rightfoot'): 6, (u'miss', u'throwin', u'rightfoot'): 1, (u'miss', u'openplay', u'header'): 2, (u'goal', u'openplay', u'rightfoot'): 7, (u'miss', u'openplay', u'leftfoot'): 11, (u'miss', u'crossedfreekick', u'leftfoot'): 1}
>>> pprint(_)
{(u'goal', u'openplay', u'leftfoot'): 2,
 (u'goal', u'openplay', u'rightfoot'): 7,
 (u'goal', u'owngoal', u'rightfoot'): 1,
 (u'miss', u'corner', u'header'): 1,
 (u'miss', u'corner', u'otherbodypart'): 1,
 (u'miss', u'corner', u'rightfoot'): 2,
 (u'miss', u'crossedfreekick', u'leftfoot'): 1,
 (u'miss', u'crossedfreekick', u'rightfoot'): 6,
 (u'miss', u'openplay', u'header'): 2,
 (u'miss', u'openplay', u'leftfoot'): 11,
 (u'miss', u'openplay', u'rightfoot'): 31,
 (u'miss', u'throwin', u'rightfoot'): 1}

Note that requests gives you direct access to decoding a JSON response; use response.json() rather than using json.loads() on response.content or response.text.

Upvotes: 1

Related Questions