{"id":1060,"date":"2023-03-24T09:47:07","date_gmt":"2023-03-24T01:47:07","guid":{"rendered":""},"modified":"2023-03-24T09:47:07","modified_gmt":"2023-03-24T01:47:07","slug":"Python - Maps","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1060.html","title":{"rendered":"Python - Maps"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

Python - Maps<\/h1>\n

Python - Maps\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n

\n Python Maps\u4e5f\u79f0\u4e3aChainMap\uff0c\u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u5c06\u591a\u4e2a\u8bcd\u5178\u4f5c\u4e3a\u4e00\u4e2a\u5355\u5143\u4e00\u8d77\u7ba1\u7406\u3002\u7ec4\u5408\u7684\u5b57\u5178\u6309\u7279\u5b9a\u987a\u5e8f\u5305\u542b\u952e\u548c\u503c\u5bf9\uff0c\u4ece\u800c\u6d88\u9664\u4e86\u4efb\u4f55\u91cd\u590d\u7684\u952e\u3002ChainMap\u7684\u6700\u4f73\u7528\u9014\u662f\u4e00\u6b21\u641c\u7d22\u591a\u4e2a\u8bcd\u5178\uff0c\u5e76\u83b7\u53d6\u6b63\u786e\u7684\u952e\u503c\u5bf9\u6620\u5c04\u3002\u6211\u4eec\u8fd8\u770b\u5230\u8fd9\u4e9bChainMap\u8868\u73b0\u4e3a\u5806\u6808\u6570\u636e\u7ed3\u6784\u3002\n <\/div>\n

\u521b\u5efa\u4e00\u4e2aChainMap<\/h2>\n
\n \u6211\u4eec\u521b\u5efa\u4e24\u4e2a\u5b57\u5178\uff0c\u5e76\u4f7f\u7528\u96c6\u5408\u5e93\u4e2d\u7684ChainMap\u65b9\u6cd5\u5c06\u5b83\u4eec\u5408\u5e76\u5728\u4e00\u8d77\u3002\u7136\u540e\uff0c\u6211\u4eec\u6253\u5370\u5b57\u5178\u7ec4\u5408\u7ed3\u679c\u7684\u952e\u548c\u503c\u3002\u5982\u679c\u6709\u91cd\u590d\u7684\u952e\uff0c\u5219\u4ec5\u4fdd\u7559\u7b2c\u4e00\u4e2a\u952e\u7684\u503c\u3002\n <\/div>\n
\n
 import <\/span>collections
dict1 = {'day1'<\/span><\/span><\/span>: 'Mon'<\/span>, 'day2'<\/span>: 'Tue'<\/span>}
dict2 = {'day3'<\/span>: 'Wed'<\/span>, 'day1': 'Thu'<\/span>}
res = collections.ChainMap<\/span>(dict1, dict2)
# Creating a single dictionary
<\/span> print(res.maps,'\\n'<\/span>)
print('Keys = {}'<\/span>.format<\/span>(list(res.keys<\/span>())))
print('Values = {}'<\/span>.format<\/span>(list(res.values<\/span>())))
print()
# print <\/span>all the elements from <\/span>the result
<\/span> print('elements:'<\/span>)
for <\/span><\/span>key, val in <\/span>res.items<\/span>():
    print('{} = {}'<\/span>.format<\/span>(key, val))
print()
# Find a specific value in <\/span>the result
<\/span> print('day3 in <\/span>res: {}'<\/span>.format<\/span>(('day1' in <\/span>res)))
print('day4 in <\/span>res: {}'<\/span>.format<\/span>(('day4'<\/span> in <\/span>res)))
  <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u4ee5\u4e0a\u4ee3\u7801\u540e\uff0c\u5c06\u4ea7\u751f\u4ee5\u4e0b\u7ed3\u679c\u3002\n <\/div>\n
\n
 [{'day1'<\/span><\/span><\/span>: 'Mon'<\/span><\/span>, 'day2'<\/span><\/span>: 'Tue'<\/span><\/span>}, {'day1': 'Thu'<\/span>, 'day3'<\/span><\/span>: 'Wed'<\/span><\/span>}] 
Keys = ['day1', 'day3', 'day2']
Values = ['Mon', 'Wed', 'Tue']
elements:
day1 = Mon
day3 = Wed
day2 = Tue
day3 in <\/span>res: True<\/span>
day4 in <\/span>res: False<\/span>
  <\/span><\/code><\/pre>\n<\/p><\/div>\n

Map\u91cd\u65b0\u6392\u5e8f<\/h2>\n
\n \u5982\u679c\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\u5408\u5e76\u5b57\u5178\u65f6\u6539\u53d8\u5b57\u5178\u7684\u987a\u5e8f\uff0c\u6211\u4eec\u5c06\u770b\u5230\u5143\u7d20\u7684\u4f4d\u7f6e\u4e92\u6362\uff0c\u5c31\u597d\u50cf\u5b83\u4eec\u5728\u8fde\u7eed\u7684\u94fe\u4e2d\u4e00\u6837\u3002\u8fd9\u518d\u6b21\u663e\u793a\u4e86\u5730\u56fe\u4f5c\u4e3a\u5806\u6808\u7684\u884c\u4e3a\u3002\n <\/div>\n
\n
 import <\/span>collections
dict1 = {'day1'<\/span>: 'Mon'<\/span>, 'day2'<\/span>: 'Tue'<\/span>}
dict2 = {'day3'<\/span>: 'Wed'<\/span>, 'day4'<\/span>: 'Thu'<\/span>}
res1 = collections.ChainMap<\/span>(dict1, dict2)
print(res1.maps,'\\n'<\/span><\/span>)
res2 = collections.ChainMap<\/span>(dict2, dict1)
print(res2.maps,'\\n')
  <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u4ee5\u4e0a\u4ee3\u7801\u540e\uff0c\u5c06\u4ea7\u751f\u4ee5\u4e0b\u7ed3\u679c\u3002\n <\/div>\n
\n
 [{'day1'<\/span><\/span>: 'Mon'<\/span><\/span>, 'day2'<\/span><\/span>: 'Tue'<\/span><\/span>}, {'day3'<\/span><\/span>: 'Wed'<\/span><\/span>, 'day4'<\/span><\/span>: 'Thu'<\/span><\/span>}] 
[{'day3': 'Wed', 'day4': 'Thu'}, {'day1': 'Mon', 'day2': 'Tue'}]
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u66f4\u65b0Map<\/h2>\n
\n \u5f53\u5b57\u5178\u7684\u5143\u7d20\u88ab\u66f4\u65b0\u65f6\uff0c\u7ed3\u679c\u7acb\u5373\u5728ChainMap\u7684\u7ed3\u679c\u4e2d\u88ab\u66f4\u65b0\u3002\u5728\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u770b\u5230\u65b0\u7684\u66f4\u65b0\u503c\u5c06\u53cd\u6620\u5728\u7ed3\u679c\u4e2d\uff0c\u800c\u65e0\u9700\u518d\u6b21\u663e\u5f0f\u5e94\u7528ChainMap\u65b9\u6cd5\u3002\n <\/div>\n
\n
  import <\/span>collections
dict1 = {'day1'<\/span>: 'Mon'<\/span>, 'day2'<\/span>: 'Tue'<\/span>}
dict2 = {'day3'<\/span>: 'Wed'<\/span>, 'day4'<\/span><\/span>: 'Thu'<\/span>}
res = collections.ChainMap<\/span>(dict1, dict2)
print(res.maps,'\\n'<\/span><\/span>)
dict2['day4'] = 'Fri'<\/span>
print(res.maps,'\\n')
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u4ee5\u4e0a\u4ee3\u7801\u540e\uff0c\u5c06\u4ea7\u751f\u4ee5\u4e0b\u7ed3\u679c\u3002\n <\/div>\n
\n
 [{'day1'<\/span><\/span>: 'Mon'<\/span><\/span>, 'day2'<\/span><\/span>: 'Tue'<\/span><\/span>}, {'day3'<\/span><\/span>: 'Wed'<\/span><\/span>, 'day4'<\/span><\/span>: 'Thu'<\/span>}] 
[{'day1': 'Mon', 'day2': 'Tue'}, {'day3': 'Wed', 'day4': 'Fri'<\/span>}]
<\/span><\/code><\/pre>\n<\/p><\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python - Mapszh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[],"class_list":["post-1060","post","type-post","status-publish","format-standard","hentry","category-python3"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1060"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1060"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1060\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}