[英]Python outputs type 'Nonetype' when try to add to dictionary
這是代碼(運行Python 2.7.6):
currency_pairs = {'PPC': 10}
print currency_pairs
currency_pairs = currency_pairs.update({'NMC': 50})
print type (currency_pairs)
輸出:
{'PPC': 10}
<type 'NoneType'>
為什么Python不會添加到字典中? 我不明白 有什么想法嗎?
該方法update
僅更新字典,但不返回任何內容(換句話說,返回None
)
currency_pairs = currency_pairs.update({'NMC': 50})
您將None
分配給currecy_pairs
。 該方法本身將修改字典,因此您應該這樣調用它:
currency_pairs = {'PPC': 10}
print currency_pairs
currency_pairs.update({'NMC': 50})
print currency_pairs
輸出:
{'PPC': 10}
{'PPC': 10, 'NMC': 50}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.