理解没有 break 语句的嵌套 for 循环的控制流

Understanding control flow of nested for loops sans break statement

我现在正试图了解这里发生了什么,但是为什么 Python 中嵌套循环的控制流在这种特定情况下以它们的方式工作:

# let's say that `res` is a list of dictionaries where len(res) == 20

for index, item in enumerate(res):
        print index,item
        for key, value in item.iteritems():
            id = item.get('id')
            print id
            video_asset_ids.append(id)
           # break        

上述嵌套循环的输出如下所示:

0 {u'updated_at': u'2012-09-18T22:07:37.027Z', u'account_id': u'2001', u'duration': 232410, u'text_tracks': [], u'images': {u'poster': {u'asset_id': u'34343615001', u'width': None, u'height': None}]}, u'thumbnail': {u'asset_id': u'34343614001', u'width': None, u'height': None}]}}, u'digital_master_id': None, u'custom_fields': {}, u'schedule': {u'starts_at': u'2009-08-18T00:53:17.569Z', u'ends_at': None}, u'id': u'34351747001', u'state': u'ACTIVE', u'cue_points': [], u'sharing': {u'source_id': None, u'to_external_acct': True, u'by_id': None, u'by_reference': False, u'by_external_acct': False}, u'complete': True, u'tags': [u'football', u'nfl network nfl films football', u'tv', u'sports', u'pro', u'entertainment'], u'link': None, u'reference_id': u'1578699', u'geo': None, u'name': u'Video: Inside NFL Films', u'created_at': u'2009-08-18T00:53:17.569Z', u'economics': u'AD_SUPPORTED', u'original_filename': None, u'folder_id': None}
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
1 {u'updated_at': u'2012-09-18T22:07:37.027Z', u'account_id': u'2001', u'duration': 232410, u'text_tracks': [], u'images': {u'poster': {u'asset_id': u'34343615001', u'width': None, u'height': None}]}, u'thumbnail': {u'asset_id': u'34343614001', u'width': None, u'height': None}]}}, u'digital_master_id': None, u'custom_fields': {}, u'schedule': {u'starts_at': u'2009-08-18T00:53:17.569Z', u'ends_at': None}, u'id': u'34351747001', u'state': u'ACTIVE', u'cue_points': [], u'sharing': {u'source_id': None, u'to_external_acct': True, u'by_id': None, u'by_reference': False, u'by_external_acct': False}, u'complete': True, u'tags': [u'football', u'nfl network nfl films football', u'tv', u'sports', u'pro', u'entertainment'], u'link': None, u'reference_id': u'1578699', u'geo': None, u'name': u'Video: Inside NFL Films', u'created_at': u'2009-08-18T00:53:17.569Z', u'economics': u'AD_SUPPORTED', u'original_filename': None, u'folder_id': None}
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001

等等,等等

显然当我们在嵌套循环中添加一个break语句时,id对象只打印一次,输出结果如下:

0 {u'updated_at': u'2012-09-18T22:07:37.027Z', u'account_id': u'2001', u'duration': 232410, u'text_tracks': [], u'images': {u'poster': {u'asset_id': u'34343615001', u'width': None, u'height': None}]}, u'thumbnail': {u'asset_id': u'34343614001', u'width': None, u'height': None}]}}, u'digital_master_id': None, u'custom_fields': {}, u'schedule': {u'starts_at': u'2009-08-18T00:53:17.569Z', u'ends_at': None}, u'id': u'34351747001', u'state': u'ACTIVE', u'cue_points': [], u'sharing': {u'source_id': None, u'to_external_acct': True, u'by_id': None, u'by_reference': False, u'by_external_acct': False}, u'complete': True, u'tags': [u'football', u'nfl network nfl films football', u'tv', u'sports', u'pro', u'entertainment'], u'link': None, u'reference_id': u'1578699', u'geo': None, u'name': u'Video: Inside NFL Films', u'created_at': u'2009-08-18T00:53:17.569Z', u'economics': u'AD_SUPPORTED', u'original_filename': None, u'folder_id': None}
4416825569001
1 {u'updated_at': u'2012-09-18T22:07:37.027Z', u'account_id': u'2001', u'duration': 232410, u'text_tracks': [], u'images': {u'poster': {u'asset_id': u'34343615001', u'width': None, u'height': None}]}, u'thumbnail': {u'asset_id': u'34343614001', u'width': None, u'height': None}]}}, u'digital_master_id': None, u'custom_fields': {}, u'schedule': {u'starts_at': u'2009-08-18T00:53:17.569Z', u'ends_at': None}, u'id': u'34351747001', u'state': u'ACTIVE', u'cue_points': [], u'sharing': {u'source_id': None, u'to_external_acct': True, u'by_id': None, u'by_reference': False, u'by_external_acct': False}, u'complete': True, u'tags': [u'football', u'nfl network nfl films football', u'tv', u'sports', u'pro', u'entertainment'], u'link': None, u'reference_id': u'1578699', u'geo': None, u'name': u'Video: Inside NFL Films', u'created_at': u'2009-08-18T00:53:17.569Z', u'economics': u'AD_SUPPORTED', u'original_filename': None, u'folder_id': None}
34351747001

等等,等等

所以如果没有 break 语句,这些循环在控制流(或在 sack 上)方面如何运作?为什么在没有 break 语句的情况下,嵌套循环会不断打印出相同的 id 对象,次数等于列表中的项目数?

在内循环的每次迭代中,keyvalue 应该不同,但 item 是相同的。 item 随着 outer 循环的每次迭代而变化。

由于您从 item 获得 id,因此 id 不会在每次内部迭代中更改。

也许您应该从 keyvalue 获得 id

遍历 item.iteritems() 会遍历字典中的每个 key, value 对,因此您为每一对打印相同的 ID。您根本不需要内部循环。试试这个:

for index, item in enumerate(res):
    print index,item
    id = item.get('id')
    print id
    video_asset_ids.append(id)