简体   繁体   English

c ++地图 <int, int> 在python中

[英]c++ map<int, int> in python

Does anyone know the c++ equivalent on python? 有谁知道python上的c ++等价物? Initially I thought I could replicate this by initializing a simple array on python, but it obviously does not do the same. 最初我以为我可以通过在python上初始化一个简单的数组来复制它,但它显然不会这样做。 Any ideas? 有任何想法吗?

map<int, int>

Kind Regards and many thanks, 亲切的问候和非常感谢,

Rodrigo 罗德里戈

Yes but remember that python objects are not statically typed so a declaration like this map is not needed (and would not work) What you do in python is declare an empty dictionary and add ints to it. 是的但是请记住python对象不是静态类型的,因此不需要像这个地图这样的声明(并且不起作用)你在python中做的是声明一个空字典并向它添加int。

Example: 例:

my_map = {} #empty dictionary
my_map[1] = 100
my_map[2] = 150

But there would be no problem continuing adding objects of different types to the dictionary: 但是继续向字典中添加不同类型的对象是没有问题的:

my_map[3] = "Hello world"

and changing the type of the existing objects: 并更改现有对象的类型:

my_map[1] = "Hello again"

You can use dict 你可以使用dict

elevators = {}
clone_floor, clone_pos = map(int, raw_input().split())
elevators[clone_floor] = clone_pos

Hope this helps. 希望这可以帮助。

It is called a dictionary. 它被称为字典。 One of the most powerful features in Python. Python中最强大的功能之一。 See here for the documentation. 请参阅此处获取文档。

d = dict()
d[1] = 5
d[5] = 2
print d[1]
print d[5]
elevators = {}
elevatorFloor, elevatorPos = map(int, raw_input().split())
elevators[elevatorFloor] = elevatorPos

This should work in Python 2. 这应该适用于Python 2。

Managed to get it sorted. 管理以使其排序。 Some syntax errors we all missed but here's the correct way: 我们都错过了一些语法错误,但这是正确的方法:

elevators = {}

elevators['clone_floor'] = clone_pos

This works. 这有效。

Thanks again! 再次感谢!

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

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