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

时间:2016-01-12 17:34:00

标签: python python-2.7 loops nested-loops break

我现在试图了解这里发生了什么,但为什么Python中嵌套循环的控制流程在这个特定场景中的工作方式如下:

    payload = {
        'main': {
            "title": "Jim",
            "description": "I am cool"
        }
    }

    return JsonResponse(payload, content_type='application/json')

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

# 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

等等

因此,如果没有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 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对象多次等于列表中的项目数?

2 个答案:

答案 0 :(得分:1)

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

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

也许您应该从idkey获取value

答案 1 :(得分:1)

循环遍历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)