簡體   English   中英

將空列表添加到字典值

[英]Adding an empty list to a dictionary value

當我編碼時:

for key, value in d.items():
    d[key].append([])

它說,

AttributeError: 'int' object has no attribute 'append'

我不明白這個錯誤信息。 這里的int對象是什么以及如何編碼將空列表添加到字典值?

value( d[key] ) 的類型是整數, .append()方法不適用於整數。

If Questions is Append a empty list as the value if value( d[key] ) is empty to a dictionary,

d = {'a':2, 'b': None, 'c':3}
for key, value in d.items():
    if not d[key]:
       d[key] = []
print(d)

輸出: {'a': 2, 'b': [], 'c': 3}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM