我有一個內部循環和一個外部循環(數據庫中的記錄)。外循環 (json) 基于幾個字段創建過濾器(例如:begin/end => 0 : 0.5 >>> ID 114 到 159)。桌子 :ID begin end status FK1_field FK2_field FK3_field FK4_field FK5_field report_id114 0 0.5 1 NULL 13 8 142 44 1115 0 0.5 1 NULL 13 8 61 45 1158 0 0.5 1 NULL 13 8 142 45 1159 0 0.5 1 NULL 13 8 61 44 1116 0.5 1.5 1 NULL 13 8 142 45 1117 0.5 1.5 1 NULL 13 8 131 45 1118 1.5 2.5 1 NULL 13 8 142 45 1結果在內部循環上循環(例如:4條記錄/行(*))。但是,當我退出內部循環時,必須在外部循環上循環4次(*),以獲得ID 116(以及過濾器的開始/結束)=> 0.5:1.5。事實上,我只能循環內部循環并使用“if”進行相同的幾次測試。但是我認為過濾更加優雅?,F在,我認為退出內循環時循環是愚蠢的。編碼 :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 個。最好的解決方案是什么?
添加回答
舉報
0/150
提交
取消