{"id":53378,"date":"2024-12-03T14:49:28","date_gmt":"2024-12-03T06:49:28","guid":{"rendered":"https:\/\/fwq.ai\/blog\/53378\/"},"modified":"2024-12-03T14:49:28","modified_gmt":"2024-12-03T06:49:28","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-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/53378\/","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><b><\/b>     <\/p>\n<h1>\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<\/h1>\n<p>\u5bf9\u4e8e\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0c\u7c73\u4e91\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300a\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\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241129\/17328861316749be7310a2c.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><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>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\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\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u5427\uff01<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u5982\u4f55\u5229\u7528 SQL \u7684 if test \u5224\u65ad\u5b57\u6bb5\u662f\u5426\u5728\u5217\u8868\u4e2d\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\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 \u5bf9\u4e8e\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0c\u7c73\u4e91\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300a\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\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01 \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 \u793a\u4f8b\uff1a data1 = [ { &#8220;appName&#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-53378","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53378","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=53378"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53378\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=53378"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=53378"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=53378"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}