Python等效于HashMap

时间:2013-10-25 11:14:53

标签: python hashmap

我是python的新手。我有一个目录,其中包含许多子文件夹和文件。因此,在这些文件中,我必须将一些指定的字符串集替换为新字符串。在java中,我使用HashMap完成了此操作。我将旧字符串存储为键,将新字符串存储为相应的值。我搜索了hashMap中的密钥,如果有一个匹配,我用相应的值替换。在Python中是否有类似于hashMap的东西,或者你可以建议如何解决这个问题。

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

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

我需要与此等效。

1 个答案:

答案 0 :(得分:64)

您需要dict

my_dict = {'cheese': 'cake'}

示例代码(来自文档):

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

您可以阅读有关词典here的更多信息。