我目前有一個字典串列
list_of_dicts = [{"name": 'stfdahbancssc',"status":'running'},
{"name": 'stfdahbancssc',"status":'running'}]
我想檢查如何匹配前綴'stf'并列印 stf 存在,否則 stf 不存在。
uj5u.com熱心網友回復:
迭代字典,獲取“名稱”值,檢查前綴,列印訊息:
list_of_dicts = [{"name": 'stfdahbancssc',"status":'running'},
{"name": 'stfdahbancssc',"status":'running'},
{"name": 'none'}]
for idx,d in enumerate(list_of_dicts):
if d.get("name","").startswith("stf"):
v = d["name"]
s = d["status"]
print(f"The {idx} dictionary has {v} as {s}")
print("a")
else:
print(f"The {idx}. dictionary has no name starting with 'stf'.")
輸出:
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/360856.html
