簡體   English   中英

嘗試添加到字典時,Python輸出類型“ Nonetype”

[英]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.

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