当退出innerloop(Python)时,继续outerloop几次

2024-06-28 11:13:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个内部循环和一个外部循环(数据库中的记录)。外部循环(json)基于几个字段(例如:begin/end=>;0:0.5>;>;ids114到159)生成一个过滤器。你知道吗

表格:

ID    begin     end  status  FK1_field  FK2_field FK3_field FK4_field  FK5_field    report_id

114    0        0.5    1      NULL      13          8         142       44            1
115    0        0.5    1      NULL      13          8         61        45            1
158    0        0.5    1      NULL      13          8         142       45            1
159    0        0.5    1      NULL      13          8         61        44            1
116    0.5      1.5    1      NULL      13          8         142       45            1
117    0.5      1.5    1      NULL      13          8         131       45            1
118    1.5      2.5    1      NULL      13          8         142       45            1

结果是在内环上循环(例如:4条记录/行(*))。你知道吗

但是,当我退出内部循环时,我必须在外部循环上循环4次(*),以获得ID 116(和filter begin/end)=>;0.5:1.5。你知道吗

实际上,我只能在内部循环上循环,并用“if”测试同样的次数。但我觉得过滤更优雅。 目前,我认为退出内部循环时循环是愚蠢的。你知道吗

代码:

highways = tools_cronjob.getAPI(API_URL + action, headers)
for highway in highways:
    print('>>>>> Highway : %s ' % highway['name'])

    # all the prpk, same values like the table
    prpk_by_highway = tools_cronjob.getAPI(API_URL + actions_api['prpk_by_highway'] + "%s" % (highway['id']), headers)

    # get all prpk by highway
    for prpk in prpk_by_highway:
        filtered_by_begin_end = [pr_pk for pr_pk in prpk_by_highway if pr_pk['begin'] == prpk['begin']]

        # each couple of prpk 'begin' and 'end'
        for filter_prpk in filtered_prpk_by_begin_end:
            # some stuff
            print (filter_prpk)
            # when I exit, for eg, I must continue looping (prpk in prpk_by_highway) starting the couple begin/end : 0.5 : 1.5 

所以第一个循环是4条记录,第二个是2条,第三个只有1条。 Json:

[
   {
      'end':0.5,
      'begin':0.0,
      'status':'1',
      'department':{
         'number':'13',
         'id':13,
         'name':'Bouches-du-Rhône'
      },
      'meteozone':{
         'id':44,
         'name':'137'
      },
      'commune':None,
      'massif':{
         'meteozones':[
            {
               'id':44,
               'name':'137'
            },
            {
               'id':45,
               'name':'138'
            },
            {
               'id':104,
               'name':'831'
            },
            {
               'id':105,
               'name':'832'
            }
         ],
         'id':142,
         'name':'Sainte-baume'
      }
   },
   {
      'end':0.5,
      'begin':0.0,
      'status':'1',
      'department':{
         'number':'13',
         'id':13,
         'name':'Bouches-du-Rhône'
      },
      'meteozone':{
         'id':45,
         'name':'138'
      },
      'commune':None,
      'massif':{
         'meteozones':[
            {
               'id':45,
               'name':'138'
            }
         ],
         'id':61,
         'name':'Garlaban'
      }
   },
   {
      'end':0.5,
      'begin':0.0,
      'status':'1',
      'department':{
         'number':'13',
         'id':13,
         'name':'Bouches-du-Rhône'
      },
      'meteozone':{
         'id':45,
         'name':'138'
      },
      'commune':None,
      'massif':{
         'meteozones':[
            {
               'id':44,
               'name':'137'
            },
            {
               'id':45,
               'name':'138'
            },
            {
               'id':104,
               'name':'831'
            },
            {
               'id':105,
               'name':'832'
            }
         ],
         'id':142,
         'name':'Sainte-baume'
      }
   },
   {
      'end':0.5,
      'begin':0.0,
      'status':'1',
      'department':{
         'number':'13',
         'id':13,
         'name':'Bouches-du-Rhône'
      },
      'meteozone':{
         'id':44,
         'name':'137'
      },
      'commune':None,
      'massif':{
         'meteozones':[
            {
               'id':45,
               'name':'138'
            }
         ],
         'id':61,
         'name':'Garlaban'
      }
   },
   {
      'end':1.5,
      'begin':0.5,
      'status':'1',
      'department':{
         'number':'13',
         'id':13,
         'name':'Bouches-du-Rhône'
      },
      'meteozone':{
         'id':45,
         'name':'138'
      },
      'commune':None,
      'massif':{
         'meteozones':[
            {
               'id':44,
               'name':'137'
            },
            {
               'id':45,
               'name':'138'
            },
            {
               'id':104,
               'name':'831'
            },
            {
               'id':105,
               'name':'832'
            }
         ],
         'id':142,
         'name':'Sainte-baume'
      }
   },
   {
      'end':1.5,
      'begin':0.5,
      'status':'1',
      'department':{
         'number':'13',
         'id':13,
         'name':'Bouches-du-Rhône'
      },
      'meteozone':{
         'id':45,
         'name':'138'
      },
      'commune':None,
      'massif':{
         'meteozones':[
            {
               'id':43,
               'name':'136'
            },
            {
               'id':45,
               'name':'138'
            }
         ],
         'id':131,
         'name':'Régagnas'
      }
   },
   {
      'end':2.5,
      'begin':1.5,
      'status':'1',
      'department':{
         'number':'13',
         'id':13,
         'name':'Bouches-du-Rhône'
      },
      'meteozone':{
         'id':45,
         'name':'138'
      },
      'commune':None,
      'massif':{
         'meteozones':[
            {
               'id':44,
               'name':'137'
            },
            {
               'id':45,
               'name':'138'
            },
            {
               'id':104,
               'name':'831'
            },
            {
               'id':105,
               'name':'832'
            }
         ],
         'id':142,
         'name':'Sainte-baume'
      }
   }
]

最好的解决方案是什么?你知道吗

谢谢 F


Tags: nameidnumberbystatusnullenddepartment
1条回答
网友
1楼 · 发布于 2024-06-28 11:13:19

我理解您的问题,您希望按begin字段对实体进行分组。您可以尝试通过循环所有条目,然后再次循环所有条目(在列表理解中)来找到具有相同begin的条目,但是外部循环在它停止的地方继续。你知道吗

相反,我建议使用itertools.groupby按该属性对条目进行分组。请注意,这假设条目是按同一属性排序的,因此如果不是,请先对其排序。你知道吗

import csv, itertools, operator
with open("data.csv") as data:
    prpk_by_highway = csv.DictReader(data)
    for key, group in itertools.groupby(prpk_by_highway, key=operator.itemgetter("begin")):
        print("BEGIN", key)
        for prpk in group:
            print(prpk)

这里,data.csv是一个保存数据的CSV文件,也就是说,使用tools_cronjob.getAPI调用应该可以实现同样的效果。输出:

BEGIN 0
OrderedDict([('ID', '114'), ('begin', '0'), ('end', '0.5'), ('status', '1'), ('FK', '44'), ('report_id', '1')])
OrderedDict([('ID', '115'), ('begin', '0'), ('end', '0.5'), ('status', '1'), ('FK', '45'), ('report_id', '1')])
OrderedDict([('ID', '158'), ('begin', '0'), ('end', '0.5'), ('status', '1'), ('FK', '45'), ('report_id', '1')])
OrderedDict([('ID', '159'), ('begin', '0'), ('end', '0.5'), ('status', '1'), ('FK', '44'), ('report_id', '1')])
BEGIN 0.5
OrderedDict([('ID', '116'), ('begin', '0.5'), ('end', '1.5'), ('status', '1'), ('FK', '45'), ('report_id', '1')])
OrderedDict([('ID', '117'), ('begin', '0.5'), ('end', '1.5'), ('status', '1'), ('FK', '45'), ('report_id', '1')])
BEGIN 1.5
OrderedDict([('ID', '118'), ('begin', '1.5'), ('end', '2.5'), ('status', '1'), ('FK', '45'), ('report_id', '1')])

或者,您可以使用dict将匹配的条目放入bucket。你知道吗

相关问题 更多 >