Python字典和Javascript对象之间有什么区别? [英] What are the differences between Python Dictionaries vs Javascript Objects?

查看:348
本文介绍了Python字典和Javascript对象之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,正在阅读有关Dictionary的文章。从我以前对javascript之类的语言的经验来看,它们对我来说就像对象。字典可以存储列表并与javascript中的对象共享许多相似之处。

I'm new to python and I was reading about Dictionaries. And from my previous experience with langages like javascript they seemed like objects to me. Dictionaries can store lists and share many similaraties to objects in javascript.

ex python代码:

ex python code:

menu = {}
menu['Chicken Alfredo'] = 14.50
menu['Italian Pasta'] = 15.89
menu['Shrimp Soup'] = 12.43
menu['Persian Rice'] = 21.99

ex javascript代码:

ex javascript code:

var menu = new Object();
menu['Chicken Alfredo'] = 14.50;
menu['Italian Pasta'] = 15.89;
menu['Shrimp Soup'] = 12.43;
menu['Persian Rice'] = 21.99;

这里有什么区别,它们都做相同的工作,但是概念不同?

What's the difference here, they both do the same job, but there different concepts?

推荐答案

发件人:


在Python中,字典是一种映射类型。可以使用一系列逗号分隔的名称来初始化它们
:值对,用花括号括起来的
。使用包含方括号的数组符号
可以访问它们。键可以是任何可散列的键,包括
数字和字符串。

In Python, dictionaries are a form of mapping type. They can be initialized using a sequence of comma-separated name: value pairs, enclosed in curly braces. They are accessed using array notation involving square braces. The key can be any hashable, including numbers and strings.

在Javascript中,字典与对象相同。可以使用与Python相同的语法对
进行初始化。键可以是数字,
字符串或标识符。因为字典也是一个对象,所以
的元素可以使用数组表示法进行访问,例如b [i],
或使用属性符号,例如bi

In Javascript, a dictionary is the same as an object. It can be initialized using the same syntax as Python. The key can be a number, a string, or an identifier. Because the dictionary is also an object, the elements can be accessed either using array notation, e.g. b[i], or using property notation, e.g. b.i.

考虑初始化程序中使用的标识符,例如

Consider an identifier used in an initializer, such as

 b = {i:j} 

在Python中,i和j均被评估,但在Javascript中,仅j被评估。在Javascript中,您还具有
特权,可以使用标识符i来写点表示法。
因此在Python中,

In Python both i and j are evaluated, but in Javascript, only j is evaluated. In Javascript you also have the privilege of writing in the dot notation, using the identifier i. Hence in Python,

 i='k'
 j=1
 b = {i:j}
 b['k'] # -> 1 

在Javascript中,

In Javascript,

 i='k'
 j=1
 b = {i:j}
 b['i'] // -> 1
 b.i // -> 1
 // b[i], b['k'] and b.k are not defined 

在Javascript中,在所有情况下使用点表示法的标识符与使用字符串看起来像数组表示法的标识符的
完全相同。
因此,
b = {’i’:1};
b ['i'] //-> 1
bi //-> 1在字典中使用数字或布尔值时,Javascript将使用
的字符串表示形式访问元素数字或布尔值。在Python中不是这样-字符串和数字(​​或
布尔值)是不同的哈希值。

In Javascript, using the identifier in dot notation is completely identical in all cases to using a string that "looks like" the identifier in array notation. Hence, b = { 'i':1 } ; b['i'] // -> 1 b.i // -> 1 When a number or boolean is used in a dictionary, Javascript will access the element using a string representation of the number or boolean. Not so in Python — a string and a number (or boolean) are different hashables.

如果您对两种语言之间的差异,然后查看 ans

If you are interested in differences between both languages, then look at ans

这篇关于Python字典和Javascript对象之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆