Python 字典与 Javascript 对象之间有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20987485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:43:35  来源:igfitidea点击:

What are the differences between Python Dictionaries vs Javascript Objects?

javascriptpythonobjectdictionary

提问by Abdullahi Farah

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.

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

ex python code:

前 python 代码:

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

ex javascript code:

前 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;

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

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

采纳答案by a.m.

From :

从 :

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.

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} 

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 

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 

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.

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

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

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

 b = {i:j} 

在 Python 中, i 和 j 都被评估,但在 Javascript 中,只有 j 被评估。在 Javascript 中,您还可以使用标识符 i 以点表示法书写。因此在 Python 中,

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

在 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 中并非如此——字符串和数字(​​或布尔值)是不同的哈希值。

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

如果您对两种语言之间的差异感兴趣,请查看ans

回答by techkuz

  • Keys in Python dictionaries must be be something hashable (e.g., a string, a number, a float, etc.).

  • The following is a valid object in JavaScript:

    const javascriptObject = { name: 'Alexander Pushkin', year: 1799 }
    

    However, it would be invalid as a Python dictionary:

    python_dictionary = {name: 'Alexander Pushkin', year: 1799}
    
    # Traceback (most recent call last):
    # NameError: name 'name' is not defined
    

    A quick fix would be to convert the Python dictionary's keys into strings:

    my_dictionary = {'name': 'Alexander Pushkin', 'year': 1799}
    
  • Via dicts you can make objects in JavaScript. They not just hold data, but also have many other powerful functionalities such as constructors.

  • Python 字典中的键必须是可散列的(例如,字符串、数字、浮点数等)。

  • 以下是 JavaScript 中的有效对象:

    const javascriptObject = { name: 'Alexander Pushkin', year: 1799 }
    

    但是,它作为 Python 字典是无效的:

    python_dictionary = {name: 'Alexander Pushkin', year: 1799}
    
    # Traceback (most recent call last):
    # NameError: name 'name' is not defined
    

    一个快速的解决方法是将 Python 字典的键转换为字符串:

    my_dictionary = {'name': 'Alexander Pushkin', 'year': 1799}
    
  • 通过 dicts,您可以在 JavaScript 中创建对象。它们不仅保存数据,还具有许多其他强大的功能,例如构造函数。

回答by AdiTOSH 007

Consider an identifier used in an initializer, such as

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

 b = {i:j} 

In Python both i and j are evaluated but in Javascript, only j is evaluated. In Javascript you can also write the dot notation, using the identifier i. Hence in Python,

在 Python 中, i 和 j 都被评估,但在 Javascript 中,只有 j 被评估。在 Javascript 中,您还可以使用标识符 i 编写点符号。因此在 Python 中,

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

In Javascript,

在 JavaScript 中,

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

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.

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

However,

然而,

//b[i] will be valid in this case of javascrip code
b = {}
var i = 'k';
b[i] = 2
console.log(b); // -> {'k':2}
//thus b[i] = b['k'] = 2

So, even though there are differences but all the applications of dictionary in python can be performed by the object in javascript and vice versa.

因此,尽管存在差异,但python中字典的所有应用程序都可以由javascript中的对象执行,反之亦然。