For循环和嵌套列表问题

时间:2014-10-25 14:12:32

标签: python list nested-lists

我从网站XHR请求通过JSON.Loads方法返回了一组嵌套列表:

[[[13, u'Arsenal', [[[[0, 1], [1, 18], [7, 1], [8, 1], [[[u'fk_foul_lost', [82]], 
[u'total_red_card', [0]], [u'total_yel_card', [21]]]]]]]], 
    ...
    ...
    ...
[184, u'Burnley', [[[[1, 11], [9, 1], [[[u'fk_foul_lost', [78]], [u'total_red_card', [0]], 
[u'total_yel_card', [12]]]]]]]], [259, u'Swansea', [[[[0, 3], [1, 14], [[[u'fk_foul_lost', [99]], 
[u'total_red_card', [2]], [u'total_yel_card', [13]]]]]]]]]]

将上面的嵌套列表分配给变量responser我使用以下代码:

for match in responser: 
    for num_events, team, events in match:

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

这会返回如下结果:

[0, 1]
[1, 18]
[7, 1]
[8, 1]
[[[u'fk_foul_lost', [82]], [u'total_red_card', [0]], [u'total_yel_card', [21]]]]
...
...
...
[1, 11]
[9, 1]
[[[u'fk_foul_lost', [78]], [u'total_red_card', [0]], [u'total_yel_card', [12]]]]
[0, 3]
[1, 14]
[[[u'fk_foul_lost', [99]], [u'total_red_card', [2]], [u'total_yel_card', [13]]]]

然而我想要的只是其中的数值:

[[[u'fk_foul_lost', [99]], [u'total_red_card', [2]], [u'total_yel_card', [13]]]]

有人能告诉我完成此代码所需的语法吗?

由于

1 个答案:

答案 0 :(得分:2)

试试这个:

for match in responser: 
    for num_events, team, events in match:

        for y in events[0]:
            for sub in y:
                if isinstance(sub[0], list):
                    print sub