我是python新手,我在读字典。从我以前使用javascript等语言的经验来看,它们对我来说就像是对象。字典可以存储列表,并与javascript中的对象共享许多相似的内容。
前python代码:
menu = {}
menu['Chicken Alfredo'] = 14.50
menu['Italian Pasta'] = 15.89
menu['Shrimp Soup'] = 12.43
menu['Persian Rice'] = 21.99
ex javascript代码:
var menu = new Object();
menu['Chicken Alfredo'] = 14.50;
menu['Italian Pasta'] = 15.89;
menu['Shrimp Soup'] = 12.43;
menu['Persian Rice'] = 21.99;
有什么区别,他们都做同样的工作,但有不同的概念?
Python字典中的键必须是可散列的(例如,字符串、数字、浮点等)。
以下是JavaScript中的有效对象:
但是,它作为Python字典是无效的:
一个快速的解决方案是将Python字典的键转换为字符串:
通过dicts,您可以在JavaScript中创建对象。它们不仅保存数据,还具有许多其他强大的功能,如构造函数。
From :
如果您对两种语言之间的差异感兴趣,请看ans
相关问题 更多 >
编程相关推荐