简体   繁体   English

Javascript Map() 的 Python 等效 function 是什么?

[英]What is the Python Equivalent function of Javascript Map()?

I am searching for a python equivalent code/function for the below javascript code.我正在为以下 javascript 代码搜索 python 等效代码/功能。

let nameSetFlagMap = new Map();

Ordered dictionary 有序字典

from collections import OrderedDict

nameSetFlagMap = OrderedDict()
nameSetFlagMap["a"] = "a"
nameSetFlagMap["b"] = "b"

what you are looking for would be Dictionary.你要找的是字典。

nameSetFlagMap={}
#or
nameSetFlagMap=dict()

you can learn more about them here你可以在这里了解更多关于它们的信息

Both answers above are wrong because they do not have the same behavior as Map() from js上面的两个答案都是错误的,因为它们的行为与 js 中的 Map() 不同


from collections import OrderedDict
asd = OrderedDict()

asd2 = []

asd[asd2] = 123

asd2.append(444)

print(asd)

print(asd[asd2])

output: output:

Traceback (most recent call last):
  File "/home/nikel/asd.py", line 10, in <module>
    asd[asd2] = 123
TypeError: unhashable type: 'list'

from collections import OrderedDict
asd = OrderedDict()

asd2 = {}

asd[asd2] = 123

asd2.append(444)

print(asd)

print(asd[asd2])

output: output:

Traceback (most recent call last):
  File "/home/nikel/asd.py", line 10, in <module>
    asd[asd2] = 123
TypeError: unhashable type: 'list'

asd = new Map()

asd[555] = 666

asd2 = []


asd[asd2] = 123

asd2.push(444)

console.log(asd)

console.log(asd[asd2])

output: output:

Map(0) { '555': 666, '': 123 }
123

I'm currently searching for analog too, so will update after I will find it.我目前也在寻找模拟,所以找到它后会更新。

But according to this answer looks like it is impossible.但是根据这个答案看起来是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM