{"id":2948,"date":"2024-11-10T13:13:25","date_gmt":"2024-11-10T05:13:25","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2948\/"},"modified":"2024-11-10T13:13:25","modified_gmt":"2024-11-10T05:13:25","slug":"%e5%a6%82%e4%bd%95%e5%b0%86%e6%95%b0%e7%bb%84%e4%b8%ad%e5%af%b9%e8%b1%a1%e7%9a%84%e9%94%ae%e5%90%8d%e6%9b%bf%e6%8d%a2%e4%b8%ba%e5%8f%a6%e4%b8%80%e4%b8%aa%e6%95%b0%e7%bb%84%e6%8f%90%e4%be%9b%e7%9a%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2948\/","title":{"rendered":"\u5982\u4f55\u5c06\u6570\u7ec4\u4e2d\u5bf9\u8c61\u7684\u952e\u540d\u66ff\u6362\u4e3a\u53e6\u4e00\u4e2a\u6570\u7ec4\u63d0\u4f9b\u7684\u952e\u540d\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173094628377786.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5c06\u6570\u7ec4\u4e2d\u5bf9\u8c61\u7684\u952e\u540d\u66ff\u6362\u4e3a\u53e6\u4e00\u4e2a\u6570\u7ec4\u63d0\u4f9b\u7684\u952e\u540d\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5c06\u6570\u7ec4\u4e2d\u5bf9\u8c61\u7684\u952e\u540d\u66ff\u6362\u4e3a\u53e6\u4e00\u4e2a\u6570\u7ec4\u63d0\u4f9b\u7684\u952e\u540d\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u66ff\u6362\u6570\u7ec4\u5bf9\u8c61\u4e2d\u7684\u952e\u540d<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u7ed9\u5b9a\u4e24\u4e2a\u5177\u6709\u76f8\u540c\u7ed3\u6784\u4f46\u952e\u540d\u4e0d\u540c\u7684\u6570\u7ec4 data1 \u548c data2\uff0c\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u65b9\u6cd5\u5c06 data1 \u4e2d\u7684\u5bf9\u8c61\u952e\u540d\u66ff\u6362\u4e3a data2 \u4e2d\u63d0\u4f9b\u7684\u952e\u540d\uff1f<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<pre>def replace_keys(data1, new_keys):\n  \"\"\"\u66ff\u6362\u6570\u7ec4\u5bf9\u8c61\u4e2d\u7684\u952e\u540d\n\n  args:\n    data1: \u539f\u59cb\u6570\u7ec4\n    new_keys: \u4fdd\u5b58\u65b0\u952e\u540d\u7684\u6570\u7ec4\n\n  returns:\n    \u66ff\u6362\u952e\u540d\u540e\u7684\u6570\u7ec4\n  \"\"\"\n\n  result = []\n  for obj in data1:\n    new_obj = {}\n    for key, value in obj.items():\n      new_key = new_keys[key]\n      new_obj[new_key] = value\n    result.append(new_obj)\n\n  return result<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>data1 = [\n  {\n    \"appName\": \"\u5e94\u75281\",\n    \"capacity\": 233456\n  },\n  {\n    \"title\": \"\u5e94\u75282\",\n    \"key\": 124535\n  }\n]\n\ndata2 = [\n  {\n    \"name\": \"\u5e94\u75281\",\n    \"value\": 233456\n  },\n  {\n    \"name\": \"\u5e94\u75282\",\n    \"value\": 124535\n  }\n]\n\nresult = replace_keys(data1, {\n  \"appName\": \"name\",\n  \"capacity\": \"value\",\n  \"title\": \"name\",\n  \"key\": \"value\"\n})\n\nprint(result)\n# \u8f93\u51fa\uff1a[\n#   {\"name\": \"\u5e94\u75281\", \"value\": 233456},\n#   {\"name\": \"\u5e94\u75282\", \"value\": 124535}\n# ]<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5c06\u6570\u7ec4\u4e2d\u5bf9\u8c61\u7684\u952e\u540d\u66ff\u6362\u4e3a\u53e6\u4e00\u4e2a\u6570\u7ec4\u63d0\u4f9b\u7684\u952e\u540d\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u66ff\u6362\u6570\u7ec4\u5bf9\u8c61\u4e2d\u7684\u952e\u540d \u95ee\u9898\uff1a \u7ed9\u5b9a\u4e24\u4e2a\u5177\u6709\u76f8\u540c\u7ed3\u6784\u4f46\u952e\u540d\u4e0d\u540c\u7684\u6570\u7ec4 data1 \u548c data2\uff0c\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u65b9\u6cd5\u5c06 data1 \u4e2d\u7684\u5bf9\u8c61\u952e\u540d\u66ff\u6362\u4e3a data2 \u4e2d\u63d0\u4f9b\u7684\u952e\u540d\uff1f \u89e3\u51b3\u65b9\u6848\uff1a def replace_keys(data1, new_keys): &#8220;&#8221;&#8221;\u66ff\u6362\u6570\u7ec4\u5bf9\u8c61\u4e2d\u7684\u952e\u540d args: data1: \u539f\u59cb\u6570\u7ec4 new_keys: \u4fdd\u5b58\u65b0\u952e\u540d\u7684\u6570\u7ec4 returns: \u66ff\u6362\u952e\u540d\u540e\u7684\u6570\u7ec4 &#8220;&#8221;&#8221; result = [] for obj in data1: new_obj = {} for key, value in obj.items(): new_key = new_keys[key] new_obj[new_key] = value result.append(new_obj) return result \u767b\u5f55\u540e\u590d\u5236 \u793a\u4f8b\uff1a data1 = [ { &#8220;appName&#8221;: &#8220;\u5e94\u75281&#8221;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-2948","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2948","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=2948"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2948\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}