報錯原因:對 None 進行迭代
d = {"aa": "1", "bb": "2", "cc": "345"} d2 = {"aa": "12", "bb": "02"} l1 = [d, d2] for item in l1: x = item.get("cc") if "3" not in x: print(x)
報錯:TypeError: 'NoneType' object is not iterable
解決:
d2 沒有 key 為 “cc” 所以 x 可能為 None 對None 進行迭代 會報錯
d = {"aa": "1", "bb": "2", "cc": "345"} d2 = {"aa": "12", "bb": "02"} l1 = [d, d2] for item in l1: x = item.get("cc") if x and "3" not in x: print(x)