繁体   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