{"id":50352,"date":"2024-12-03T11:19:12","date_gmt":"2024-12-03T03:19:12","guid":{"rendered":"https:\/\/fwq.ai\/blog\/50352\/"},"modified":"2024-12-03T11:19:12","modified_gmt":"2024-12-03T03:19:12","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e7%b1%bb%e4%bc%bcphp-array_column%e5%87%bd%e6%95%b0%e7%9a%84%e5%8a%9f%e8%83%bd%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/50352\/","title":{"rendered":"Python\u5982\u4f55\u5b9e\u73b0\u7c7b\u4f3cPHP array_column\u51fd\u6570\u7684\u529f\u80fd\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python\u5982\u4f55\u5b9e\u73b0\u7c7b\u4f3cPHP array_column\u51fd\u6570\u7684\u529f\u80fd\uff1f <\/h1>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u6211\u4eec\u8981\u52aa\u529b\u5b66\u4e60\u5566\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4e0b\u6587\u4e2d\u7684\u5185\u5bb9\u6211\u4eec\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5728\u9605\u8bfb\u672c\u6587\u8fc7\u7a0b\u4e2d\u6709\u9047\u5230\u4e0d\u6e05\u695a\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u7559\u8a00\u5440\uff01\u6211\u4eec\u4e00\u8d77\u8ba8\u8bba\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241117\/17318515106739f4f608f22.jpg\" class=\"aligncenter\" title=\"Python\u5982\u4f55\u5b9e\u73b0\u7c7b\u4f3cPHP array_column\u51fd\u6570\u7684\u529f\u80fd\uff1f\u63d2\u56fe\" alt=\"Python\u5982\u4f55\u5b9e\u73b0\u7c7b\u4f3cPHP array_column\u51fd\u6570\u7684\u529f\u80fd\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u7528 python \u5b9e\u73b0\u7c7b\u4f3c php array_column \u51fd\u6570<\/strong><\/p>\n<p>php \u4e2d\u7684 array_column \u51fd\u6570\u53ef\u4ee5\u4ece\u5d4c\u5957\u5217\u8868\u4e2d\u63d0\u53d6\u6307\u5b9a\u5217\u7684\u503c\u3002\u5728 python \u4e2d\uff0c\u867d\u7136\u6ca1\u6709\u5185\u7f6e\u7684\u76f4\u63a5\u5bf9\u5e94\u51fd\u6570\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u65b9\u6cd5\u5b9e\u73b0\u7c7b\u4f3c\u7684\u6548\u679c\uff1a<\/p>\n<p><strong>\u63d0\u53d6\u5355\u4e2a\u5217<\/strong><\/p>\n<p>\u4f7f\u7528\u5217\u8868\u89e3\u6790\uff0c\u53ef\u4ee5\u4ece\u5d4c\u5957\u5b57\u5178\u5217\u8868\u4e2d\u63d0\u53d6\u6307\u5b9a\u5217\u7684\u503c\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\uff1a<\/p>\n<pre>def extract_column(data, column_name):\n    \"\"\"\n    \u63d0\u53d6\u5217\u8868\u4e2d\u6307\u5b9a\u5217\u540d\u7684\u503c\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\n\n    :param data: \u5305\u542b\u5b57\u5178\u7684\u5217\u8868\n    :param column_name: \u8981\u63d0\u53d6\u7684\u5217\u540d\n    :return: \u5305\u542b\u63d0\u53d6\u503c\u7684\u5217\u8868\n    \"\"\"\n    return [item[column_name] for item in data]<\/pre>\n<p><strong>\u63d0\u53d6\u952e\u503c\u5bf9<\/strong><\/p>\n<p>\u8981\u5c06\u63d0\u53d6\u7684\u503c\u4f5c\u4e3a\u952e\u503c\u5bf9\u8fd4\u56de\uff0c\u53ef\u4ee5\u518d\u6b21\u4f7f\u7528\u5217\u8868\u89e3\u6790\uff0c\u4f46\u8fd9\u6b21\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\uff1a<\/p>\n<pre>def extract_column_to_dict(data, key_column_name, value_column_name):\n    \"\"\"\n    \u63d0\u53d6\u5217\u8868\u4e2d\u6307\u5b9a\u952e\u540d\u548c\u503c\u540d\u7684\u5b57\u5178\n\n    :param data: \u5305\u542b\u5b57\u5178\u7684\u5217\u8868\n    :param key_column_name: \u952e\u540d\u5bf9\u5e94\u7684\u5217\u540d\n    :param value_column_name: \u503c\u5bf9\u5e94\u7684\u5217\u540d\n    :return: \u5305\u542b\u63d0\u53d6\u952e\u503c\u5bf9\u7684\u5b57\u5178\n    \"\"\"\n    return {item[key_column_name]: item[value_column_name] for item in data}<\/pre>\n<p><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/p>\n<pre>data = [\n    {'id': 1, 'name': 'Alice', 'age': 25},\n    {'id': 2, 'name': 'Bob', 'age': 30},\n    {'id': 3, 'name': 'Charlie', 'age': 35},\n]\n\nnames = extract_column(data, 'name')\nprint(names)  # \u8f93\u51fa\uff1a['Alice', 'Bob', 'Charlie']\n\nnew_dict = extract_column_to_dict(data, 'id', 'name')\nprint(new_dict)  # \u8f93\u51fa\uff1a{1: 'Alice', 2: 'Bob', 3: 'Charlie'}<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u7c73\u4e91\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u5982\u4f55\u5b9e\u73b0\u7c7b\u4f3cPHP array_column\u51fd\u6570\u7684\u529f\u80fd\uff1f \u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u6211\u4eec\u8981\u52aa\u529b\u5b66\u4e60\u5566\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4e0b\u6587\u4e2d\u7684\u5185\u5bb9\u6211\u4eec\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5728\u9605\u8bfb\u672c\u6587\u8fc7\u7a0b\u4e2d\u6709\u9047\u5230\u4e0d\u6e05\u695a\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u7559\u8a00\u5440\uff01\u6211\u4eec\u4e00\u8d77\u8ba8\u8bba\uff0c\u4e00\u8d77\u5b66\u4e60\uff01 \u5982\u4f55\u7528 python \u5b9e\u73b0\u7c7b\u4f3c php array_column \u51fd\u6570 php \u4e2d\u7684 array_column \u51fd\u6570\u53ef\u4ee5\u4ece\u5d4c\u5957\u5217\u8868\u4e2d\u63d0\u53d6\u6307\u5b9a\u5217\u7684\u503c\u3002\u5728 python \u4e2d\uff0c\u867d\u7136\u6ca1\u6709\u5185\u7f6e\u7684\u76f4\u63a5\u5bf9\u5e94\u51fd\u6570\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u65b9\u6cd5\u5b9e\u73b0\u7c7b\u4f3c\u7684\u6548\u679c\uff1a \u63d0\u53d6\u5355\u4e2a\u5217 \u4f7f\u7528\u5217\u8868\u89e3\u6790\uff0c\u53ef\u4ee5\u4ece\u5d4c\u5957\u5b57\u5178\u5217\u8868\u4e2d\u63d0\u53d6\u6307\u5b9a\u5217\u7684\u503c\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\uff1a def extract_column(data, column_name): &#8220;&#8221;&#8221; \u63d0\u53d6\u5217\u8868\u4e2d\u6307\u5b9a\u5217\u540d\u7684\u503c\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5217\u8868 :param data: \u5305\u542b\u5b57\u5178\u7684\u5217\u8868 :param column_name: \u8981\u63d0\u53d6\u7684\u5217\u540d :return: \u5305\u542b\u63d0\u53d6\u503c\u7684\u5217\u8868 &#8220;&#8221;&#8221; return [item[column_name] for item in data] \u63d0\u53d6\u952e\u503c\u5bf9 \u8981\u5c06\u63d0\u53d6\u7684\u503c\u4f5c\u4e3a\u952e\u503c\u5bf9\u8fd4\u56de\uff0c\u53ef\u4ee5\u518d\u6b21\u4f7f\u7528\u5217\u8868\u89e3\u6790\uff0c\u4f46\u8fd9\u6b21\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\uff1a def extract_column_to_dict(data, key_column_name, value_column_name): &#8220;&#8221;&#8221; \u63d0\u53d6\u5217\u8868\u4e2d\u6307\u5b9a\u952e\u540d\u548c\u503c\u540d\u7684\u5b57\u5178 :param data: \u5305\u542b\u5b57\u5178\u7684\u5217\u8868 :param key_column_name: \u952e\u540d\u5bf9\u5e94\u7684\u5217\u540d :param value_column_name: \u503c\u5bf9\u5e94\u7684\u5217\u540d :return: [&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-50352","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50352","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=50352"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50352\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=50352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=50352"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=50352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}