TypeError: 'NoneType' object is not iterable


報錯原因:對 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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM