HashMap 的 Python 等价物

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19588290/
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-19 14:07:24  来源:igfitidea点击:

Python equivalent for HashMap

pythonhashmap

提问by Wolf

I'm new to python. I have a directory which has many subfolders and files. So in these files I have to replace some specified set of strings to new strings. In java I have done this using HashMap. I have stored the old strings as keys and new strings as their corresponding values. I searched for the key in the hashMap and if I got a hit, I replaced with the corresponding value. Is there something similar to hashMap in Python or can you suggest how to go about this problem.

我是python的新手。我有一个包含许多子文件夹和文件的目录。所以在这些文件中,我必须将一些指定的字符串集替换为新字符串。在 Java 中,我使用HashMap. 我已将旧字符串存储为键,将新字符串存储为相应的值。我在 hashMap 中搜索键,如果命中,我将替换为相应的值。有没有类似于 Python 中的 hashMap 的东西,或者你能建议如何解决这个问题。

To give an example lets take the set of strings are Request, Response. I want to change them to MyRequest and MyResponse. My hashMap was

举个例子,让我们取一组字符串是请求,响应。我想将它们更改为 MyRequest 和 MyResponse。我的 hashMap 是

Key -- value
Request -- MyRequest
Response -- MyResponse

I need an equivalent to this.

我需要一个与此相当的。

采纳答案by Games Brainiac

You need a dict:

你需要一个dict

my_dict = {'cheese': 'cake'}

Example code (from the docs):

示例代码(来自文档):

>>> a = dict(one=1, two=2, three=3)
>>> b = {'one': 1, 'two': 2, 'three': 3}
>>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
>>> d = dict([('two', 2), ('one', 1), ('three', 3)])
>>> e = dict({'three': 3, 'one': 1, 'two': 2})
>>> a == b == c == d == e
True

You can read more about dictionaries here.

您可以在此处阅读有关词典的更多信息。