Python 有條件迴圈遍歷

2022-09-18 12:42:12 字數 1343 閱讀 5838

有個場景需要通過傳入的列表遍歷乙個列表裡每個字典並判斷特定鍵值是否在允許的列表裡.

通過迭代和計數來實現節點判斷和遍歷.

以下是實現方法

info = [

]},,

]}]},,,

]},]},

,]def get_allow(info_in, allow_in):

result_to_return =

success_status, allow_count = false, 0

for i in info_in:

if not i.get('children'):

'進入根節點'

print(f"root --- ")

if i.get('title') not in allow_in:

continue

else:

allow_count += 1

i2 =

if i.get('children'):

'不是根節點, 迭代檢查, 並獲取子節點返回回來的狀態'

children, success_status = get_allow(i.get('children'), allow_in)

if success_status:

'存在任一子節點在允許清單'

result_to_return[result_to_return.index(i2)]['children'] = children

elif allow_count:

'是根節點, 並且在允許清單裡!'

if success_status:

pass

elif allow_count > 0 and not success_status:

'根節點在清單中, 修改狀態'

success_status = true

return result_to_return, success_status

allow_list = ['d-1-2', 'e']

print(get_allow(info, allow_list))

結果:

root --- a-1

root --- b-1-1

root --- b-1-2

root --- c

root --- d-1-1

root --- d-1-2

root --- d-2

root --- e

(, 'title': 'd-1'}],

'title': 'd'},

],true)

myslq有條件插入資料

要求是這樣的 我有乙個表存著基礎資料有乙個欄位是 管理號 我的目的是先查詢最新的管理號按照規則生成新管理號 然後insert到表裡新資料 由於查詢並不鎖表 所以在查詢到insert這步中間會有可能兩線程查詢到相同的管理號?參考了關鍵是如下的mysql語句,插入多條記錄 insert into cli...

有條件的表聯接

表1和表2在聯接時,希望顯示表1的全部記錄以及表2的部分記錄。嘗試使用下面的 sql 語句 select table1.table2as.from table1 left outer join select from table2 where rightname 計畫編制員 as table2as ...

RAILS有條件的校驗

rails中所有的驗證宣告都可以接受 if 選項,可以指定一段在校驗之前執行的 比如 只有在郵箱位址不為空的時候才驗證郵箱位址的格式 使用proc物件,呼叫時,傳入當前的模型物件作為引數,返回false時,不做校驗 validates format of email,with a za z0 9 a...