我是python的新手.我有一个目录,其中包含许多子文件夹和文件.因此,在这些文件中,我必须将一些指定的字符串集替换为新字符串.在java中我使用了这个HashMap.我将旧字符串存储为键,将新字符串存储为相应的值.我搜索了hashMap中的密钥,如果有一个匹配,我用相应的值替换.是否有类似于Python中的hashMap的东西,或者你可以建议如何解决这个问题.
举一个例子,让我们看一下字符串集是Request,Response.我想将它们更改为MyRequest和MyResponse.我的hashMap是
Key -- value
Request -- MyRequest
Response -- MyResponse
Run Code Online (Sandbox Code Playgroud)
我需要一个与此相当的东西.
Gam*_*iac 65
你需要一个dict:
my_dict = {'cheese': 'cake'}
Run Code Online (Sandbox Code Playgroud)
示例代码(来自文档):
>>> 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
Run Code Online (Sandbox Code Playgroud)
您可以在此处阅读有关词典的更多信息.
| 归档时间: |
|
| 查看次数: |
104347 次 |
| 最近记录: |