【问题标题】:Understanding control flow of nested for loops sans break statement理解无中断语句的嵌套 for 循环的控制流程
【发布时间】:2016-01-12 17:34:00
【问题描述】:

我现在正试图了解这里发生了什么,但是为什么 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 语句,这些循环在控制流方面(或在麻袋上)如何运作?为什么在没有break 语句的情况下,嵌套循环会不断打印出相同的id 对象,其次数等于列表中的项目数?

【问题讨论】:

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


    【解决方案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)
    

    【讨论】:

      【解决方案2】:

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

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

      也许您应该从keyvalue 获得id

      【讨论】:

        猜你喜欢
        • 2020-08-23
        • 1970-01-01
        • 2015-01-18
        • 1970-01-01
        • 1970-01-01
        • 2019-06-05
        • 1970-01-01
        • 2016-07-24
        • 1970-01-01
        相关资源
        最近更新 更多