簡體   English   中英

對於循環和嵌套列表問題

[英]For loops and nested lists issue

我有一組嵌套列表從網站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]]]]

誰能告訴我完成此代碼所需的語法?

謝謝

嘗試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM