{"id":1142185,"date":"2025-01-08T22:37:32","date_gmt":"2025-01-08T14:37:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1142185.html"},"modified":"2025-01-08T22:37:35","modified_gmt":"2025-01-08T14:37:35","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e4%b8%80%e4%b8%aa%e5%ad%97%e6%ae%b5%e8%ae%be%e4%b8%ba%e9%94%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1142185.html","title":{"rendered":"python\u5982\u4f55\u5c06\u4e00\u4e2a\u5b57\u6bb5\u8bbe\u4e3a\u952e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24180434\/78e7c80e-5d70-4dde-9943-0ed083667f54.webp\" alt=\"python\u5982\u4f55\u5c06\u4e00\u4e2a\u5b57\u6bb5\u8bbe\u4e3a\u952e\" \/><\/p>\n<p><h3>PYTHON\u5982\u4f55\u5c06\u4e00\u4e2a\u5b57\u6bb5\u8bbe\u4e3a\u952e<\/h3>\n<\/p>\n<p><p><strong>\u5728Python\u4e2d\u5c06\u4e00\u4e2a\u5b57\u6bb5\u8bbe\u4e3a\u952e\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u3001<code>pandas<\/code>\u5e93\u3001<code>collections<\/code>\u6a21\u5757\u7b49<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u5b57\u5178\u65b9\u6cd5\u662f\u6700\u76f4\u63a5\u7684\u65b9\u5f0f\u3002\u901a\u8fc7\u5c06\u6570\u636e\u7ed3\u6784\u8f6c\u6362\u4e3a\u5b57\u5178\uff0c\u5e76\u5c06\u6240\u9700\u5b57\u6bb5\u4f5c\u4e3a\u952e\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>\u4e00\u3001\u4f7f\u7528\u5b57\u5178\u5c06\u5b57\u6bb5\u8bbe\u4e3a\u952e<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u4e4b\u4e00\uff0c\u975e\u5e38\u9002\u5408\u5c06\u67d0\u4e2a\u5b57\u6bb5\u8bbe\u4e3a\u952e\u3002\u5047\u8bbe\u6709\u4e00\u4e2a\u5217\u8868\uff0c\u5217\u8868\u4e2d\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u5b57\u5178\uff0c\u60f3\u8981\u5c06\u67d0\u4e2a\u5b57\u6bb5\u8bbe\u4e3a\u952e\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b57\u5178\u63a8\u5bfc\u5f0f\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [<\/p>\n<p>    {&#39;id&#39;: 1, &#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25},<\/p>\n<p>    {&#39;id&#39;: 2, &#39;name&#39;: &#39;Bob&#39;, &#39;age&#39;: 30},<\/p>\n<p>    {&#39;id&#39;: 3, &#39;name&#39;: &#39;Charlie&#39;, &#39;age&#39;: 35}<\/p>\n<p>]<\/p>\n<h2><strong>\u5c06&#39;id&#39;\u5b57\u6bb5\u8bbe\u4e3a\u952e<\/strong><\/h2>\n<p>data_dict = {item[&#39;id&#39;]: item for item in data}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c<code>data_dict<\/code>\u5c06\u4f1a\u662f\u4e00\u4e2a\u4ee5<code>id<\/code>\u4e3a\u952e\u7684\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h4>\u4e8c\u3001\u4f7f\u7528pandas\u5c06\u5b57\u6bb5\u8bbe\u4e3a\u952e<\/h4>\n<\/p>\n<p><p><code>pandas<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u6548\u7684\u64cd\u4f5c\u6570\u636e\u7684\u65b9\u6cd5\u3002\u5728\u5904\u7406\u6570\u636e\u65f6\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528<code>pandas<\/code>\u7684<code>DataFrame<\/code>\u7ed3\u6784\u3002\u5728<code>DataFrame<\/code>\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u7d22\u5f15\u6765\u5c06\u67d0\u4e2a\u5b57\u6bb5\u8bbe\u4e3a\u952e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa DataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5c06&#39;id&#39;\u5b57\u6bb5\u8bbe\u4e3a\u7d22\u5f15<\/strong><\/h2>\n<p>df.set_index(&#39;id&#39;, inplace=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c<code>df<\/code>\u7684\u7d22\u5f15\u5c06\u53d8\u4e3a<code>id<\/code>\u5b57\u6bb5\uff0c\u8fdb\u4e00\u6b65\u64cd\u4f5c\u5c06\u66f4\u52a0\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><h4>\u4e09\u3001\u4f7f\u7528collections\u6a21\u5757\u7684defaultdict<\/h4>\n<\/p>\n<p><p><code>collections<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u6709\u7528\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5176\u4e2d\u7684<code>defaultdict<\/code>\u53ef\u4ee5\u7b80\u5316\u5b57\u5178\u7684\u64cd\u4f5c\u3002\u5728\u5c06\u5b57\u6bb5\u8bbe\u4e3a\u952e\u65f6\uff0c\u53ef\u4ee5\u5229\u7528<code>defaultdict<\/code>\u81ea\u52a8\u521d\u59cb\u5316\u5b57\u5178\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<p>data_dict = defaultdict(list)<\/p>\n<p>for item in data:<\/p>\n<p>    data_dict[item[&#39;id&#39;]].append(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c<code>data_dict<\/code>\u5c06\u4f1a\u662f\u4e00\u4e2a\u4ee5<code>id<\/code>\u4e3a\u952e\u7684\u5b57\u5178\uff0c\u5e76\u4e14\u6bcf\u4e2a\u952e\u5bf9\u5e94\u7684\u503c\u662f\u4e00\u4e2a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>\u56db\u3001\u4f7f\u7528SQLAlchemy ORM\u7684\u6a21\u578b\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u52a0\u590d\u6742\u7684\u6570\u636e\u64cd\u4f5c\u548c\u7ba1\u7406\uff0c\u53ef\u4ee5\u4f7f\u7528SQLAlchemy ORM\uff08\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\uff09\u5c06\u5b57\u6bb5\u8bbe\u4e3a\u952e\u3002\u5728SQLAlchemy\u4e2d\uff0c\u53ef\u4ee5\u5b9a\u4e49\u6a21\u578b\u7c7b\uff0c\u5e76\u901a\u8fc7\u914d\u7f6e\u6620\u5c04\u5b57\u6bb5\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy import create_engine, Column, Integer, String<\/p>\n<p>from sqlalchemy.ext.declarative import declarative_base<\/p>\n<p>from sqlalchemy.orm import sessionmaker<\/p>\n<h2><strong>\u5b9a\u4e49\u6a21\u578b\u7c7b<\/strong><\/h2>\n<p>Base = declarative_base()<\/p>\n<p>class User(Base):<\/p>\n<p>    __tablename__ = &#39;users&#39;<\/p>\n<p>    id = Column(Integer, primary_key=True)<\/p>\n<p>    name = Column(String)<\/p>\n<p>    age = Column(Integer)<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5\u548c\u4f1a\u8bdd<\/strong><\/h2>\n<p>engine = create_engine(&#39;sqlite:\/\/\/:memory:&#39;)<\/p>\n<p>Base.metadata.create_all(engine)<\/p>\n<p>Session = sessionmaker(bind=engine)<\/p>\n<p>session = Session()<\/p>\n<h2><strong>\u63d2\u5165\u6570\u636e<\/strong><\/h2>\n<p>session.add_all([<\/p>\n<p>    User(id=1, name=&#39;Alice&#39;, age=25),<\/p>\n<p>    User(id=2, name=&#39;Bob&#39;, age=30),<\/p>\n<p>    User(id=3, name=&#39;Charlie&#39;, age=35)<\/p>\n<p>])<\/p>\n<p>session.commit()<\/p>\n<h2><strong>\u67e5\u8be2\u6570\u636e\u5e76\u5c06&#39;id&#39;\u5b57\u6bb5\u8bbe\u4e3a\u952e<\/strong><\/h2>\n<p>users = session.query(User).all()<\/p>\n<p>user_dict = {user.id: user for user in users}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c<code>user_dict<\/code>\u5c06\u4f1a\u662f\u4e00\u4e2a\u4ee5<code>id<\/code>\u4e3a\u952e\u7684\u5b57\u5178\uff0c\u6bcf\u4e2a\u952e\u5bf9\u5e94\u7684\u503c\u662f\u4e00\u4e2a<code>User<\/code>\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h4>\u4e94\u3001\u4f7f\u7528JSON\u6570\u636e\u89e3\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406JSON\u6570\u636e\u65f6\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u89e3\u6790JSON\u5e76\u5c06\u67d0\u4e2a\u5b57\u6bb5\u8bbe\u4e3a\u952e\u3002\u5047\u8bbe\u6709\u4e00\u4e2aJSON\u6587\u4ef6\uff0c\u5185\u5bb9\u4e0e\u4e0a\u8ff0\u6570\u636e\u7c7b\u4f3c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>json<\/code>\u5e93\u6765\u89e3\u6790\u5e76\u8f6c\u6362\u4e3a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u8bfb\u53d6JSON\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;data.json&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = json.load(file)<\/p>\n<h2><strong>\u5c06&#39;id&#39;\u5b57\u6bb5\u8bbe\u4e3a\u952e<\/strong><\/h2>\n<p>data_dict = {item[&#39;id&#39;]: item for item in data}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c<code>data_dict<\/code>\u5c06\u4f1a\u662f\u4e00\u4e2a\u4ee5<code>id<\/code>\u4e3a\u952e\u7684\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h4>\u516d\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u66f4\u52a0\u7075\u6d3b\u7684\u65b9\u5f0f\u6765\u5904\u7406\u6570\u636e\uff0c\u53ef\u4ee5\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5c06\u67d0\u4e2a\u5b57\u6bb5\u8bbe\u4e3a\u952e\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def set_field_as_key(data, key_field):<\/p>\n<p>    return {item[key_field]: item for item in data}<\/p>\n<h2><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong><\/h2>\n<p>data_dict = set_field_as_key(data, &#39;id&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c\u901a\u8fc7\u8c03\u7528<code>set_field_as_key<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u4efb\u610f\u5b57\u6bb5\u8bbe\u4e3a\u952e\u3002<\/p>\n<\/p>\n<p><h4>\u4e03\u3001\u4f7f\u7528\u5217\u8868\u7406\u89e3\u548c\u751f\u6210\u5668<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u6765\u8282\u7701\u5185\u5b58\u3002\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u7c7b\u4f3c\uff0c\u4f46\u5b83\u4e0d\u4f1a\u7acb\u5373\u751f\u6210\u6574\u4e2a\u5217\u8868\uff0c\u800c\u662f\u6309\u9700\u751f\u6210\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data_gen = ({item[&#39;id&#39;]: item} for item in data)<\/p>\n<h2><strong>\u5c06\u751f\u6210\u5668\u8f6c\u6362\u4e3a\u5b57\u5178<\/strong><\/h2>\n<p>data_dict = {k: v for d in data_gen for k, v in d.items()}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c<code>data_dict<\/code>\u5c06\u4f1a\u662f\u4e00\u4e2a\u4ee5<code>id<\/code>\u4e3a\u952e\u7684\u5b57\u5178\uff0c\u5e76\u4e14\u5728\u751f\u6210\u8fc7\u7a0b\u4e2d\u66f4\u8282\u7701\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><h4>\u516b\u3001\u4f7f\u7528MapReduce\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5206\u5e03\u5f0f\u8ba1\u7b97\u548c\u5927\u6570\u636e\u5904\u7406\uff0c\u53ef\u4ee5\u4f7f\u7528MapReduce\u65b9\u6cd5\u6765\u5c06\u5b57\u6bb5\u8bbe\u4e3a\u952e\u3002MapReduce\u662f\u4e00\u4e2a\u7f16\u7a0b\u6a21\u578b\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def map_function(item):<\/p>\n<p>    return (item[&#39;id&#39;], item)<\/p>\n<p>def reduce_function(mapped_data):<\/p>\n<p>    result = {}<\/p>\n<p>    for key, value in mapped_data:<\/p>\n<p>        result[key] = value<\/p>\n<p>    return result<\/p>\n<h2><strong>\u4f7f\u7528MapReduce\u65b9\u6cd5<\/strong><\/h2>\n<p>mapped_data = map(map_function, data)<\/p>\n<p>data_dict = reduce_function(mapped_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c<code>data_dict<\/code>\u5c06\u4f1a\u662f\u4e00\u4e2a\u4ee5<code>id<\/code>\u4e3a\u952e\u7684\u5b57\u5178\uff0c\u901a\u8fc7MapReduce\u65b9\u6cd5\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>\u4e5d\u3001\u4f7f\u7528NumPy\u548c\u7ed3\u6784\u5316\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>\u5728\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0c<code>NumPy<\/code>\u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u636e\u5904\u7406\u65b9\u6cd5\u3002\u53ef\u4ee5\u4f7f\u7528\u7ed3\u6784\u5316\u6570\u7ec4\u6765\u5c06\u5b57\u6bb5\u8bbe\u4e3a\u952e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u7ed3\u6784\u5316\u6570\u7ec4<\/strong><\/h2>\n<p>dtype = [(&#39;id&#39;, int), (&#39;name&#39;, &#39;U10&#39;), (&#39;age&#39;, int)]<\/p>\n<p>data_array = np.array([(1, &#39;Alice&#39;, 25), (2, &#39;Bob&#39;, 30), (3, &#39;Charlie&#39;, 35)], dtype=dtype)<\/p>\n<h2><strong>\u5c06&#39;id&#39;\u5b57\u6bb5\u8bbe\u4e3a\u952e<\/strong><\/h2>\n<p>data_dict = {item[&#39;id&#39;]: item for item in data_array}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c<code>data_dict<\/code>\u5c06\u4f1a\u662f\u4e00\u4e2a\u4ee5<code>id<\/code>\u4e3a\u952e\u7684\u5b57\u5178\uff0c\u4f7f\u7528\u7ed3\u6784\u5316\u6570\u7ec4\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>\u5341\u3001\u4f7f\u7528\u5d4c\u5957\u5b57\u5178\u548c\u591a\u7ea7\u7d22\u5f15<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5b57\u5178\u548c\u591a\u7ea7\u7d22\u5f15\u6765\u5c06\u5b57\u6bb5\u8bbe\u4e3a\u952e\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u591a\u4e2a\u5b57\u6bb5\u4f5c\u4e3a\u952e\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_data = [<\/p>\n<p>    {&#39;country&#39;: &#39;USA&#39;, &#39;city&#39;: &#39;New York&#39;, &#39;population&#39;: 8000000},<\/p>\n<p>    {&#39;country&#39;: &#39;USA&#39;, &#39;city&#39;: &#39;Los Angeles&#39;, &#39;population&#39;: 4000000},<\/p>\n<p>    {&#39;country&#39;: &#39;Canada&#39;, &#39;city&#39;: &#39;Toronto&#39;, &#39;population&#39;: 3000000}<\/p>\n<p>]<\/p>\n<h2><strong>\u5c06&#39;country&#39;\u548c&#39;city&#39;\u5b57\u6bb5\u8bbe\u4e3a\u952e<\/strong><\/h2>\n<p>nested_dict = {}<\/p>\n<p>for item in nested_data:<\/p>\n<p>    country = item[&#39;country&#39;]<\/p>\n<p>    city = item[&#39;city&#39;]<\/p>\n<p>    if country not in nested_dict:<\/p>\n<p>        nested_dict[country] = {}<\/p>\n<p>    nested_dict[country][city] = item<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c<code>nested_dict<\/code>\u5c06\u4f1a\u662f\u4e00\u4e2a\u4ee5<code>country<\/code>\u548c<code>city<\/code>\u4e3a\u952e\u7684\u5d4c\u5957\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5c06\u4e00\u4e2a\u5b57\u6bb5\u8bbe\u4e3a\u952e\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<strong>\u4f7f\u7528\u5b57\u5178\u662f\u6700\u76f4\u63a5\u548c\u5e38\u89c1\u7684\u65b9\u6cd5<\/strong>\uff0c\u800c<strong>pandas\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u80fd\u529b<\/strong>\uff0c\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u5206\u6790\u3002<strong>collections\u6a21\u5757\u7684defaultdict\u7b80\u5316\u4e86\u5b57\u5178\u64cd\u4f5c<\/strong>\uff0c\u800c<strong>SQLAlchemy ORM\u9002\u7528\u4e8e\u590d\u6742\u7684\u6570\u636e\u7ba1\u7406<\/strong>\u3002\u901a\u8fc7\u5408\u7406\u9009\u62e9\u548c\u7ec4\u5408\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5c06\u5b57\u6bb5\u8bbe\u4e3a\u952e\uff0c\u5b9e\u73b0\u6570\u636e\u7684\u5feb\u901f\u67e5\u8be2\u548c\u7ba1\u7406\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u5b57\u5178\u4e2d\u7684\u7279\u5b9a\u5b57\u6bb5\u8bbe\u4e3a\u952e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5982\u679c\u4f60\u60f3\u5c06\u5b57\u5178\u4e2d\u7684\u4e00\u4e2a\u7279\u5b9a\u5b57\u6bb5\u8bbe\u4e3a\u952e\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u3002\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u5b57\u5178\u7684\u5217\u8868\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u63d0\u53d6\u67d0\u4e2a\u5b57\u6bb5\u7684\u503c\u5e76\u5c06\u5176\u4f5c\u4e3a\u65b0\u7684\u952e\u6765\u6784\u5efa\u65b0\u7684\u5b57\u5178\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>{item[&#39;field&#39;]: item for item in list_of_dicts}<\/code>\u7684\u65b9\u5f0f\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u5904\u7406\u5d4c\u5957\u5b57\u5178\u5e76\u5c06\u67d0\u4e2a\u5b57\u6bb5\u8bbe\u4e3a\u952e\uff1f<\/strong><br \/>\u5904\u7406\u5d4c\u5957\u5b57\u5178\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u6765\u904d\u5386\u6bcf\u4e2a\u5b57\u5178\u5e76\u63d0\u53d6\u6240\u9700\u5b57\u6bb5\u3002\u5982\u679c\u67d0\u4e2a\u5b57\u6bb5\u9700\u8981\u4f5c\u4e3a\u952e\uff0c\u53ef\u4ee5\u5728\u9012\u5f52\u8fc7\u7a0b\u4e2d\u52a8\u6001\u6784\u5efa\u65b0\u5b57\u5178\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784\uff0c\u786e\u4fdd\u6240\u9700\u5b57\u6bb5\u88ab\u6b63\u786e\u63d0\u53d6\u5e76\u8bbe\u4e3a\u952e\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5c06\u5217\u8868\u4e2d\u7684\u5bf9\u8c61\u5b57\u6bb5\u8bbe\u4e3a\u5b57\u5178\u952e\uff1f<\/strong><br \/>\u5f53\u5904\u7406\u5bf9\u8c61\u5217\u8868\u65f6\uff0c\u53ef\u4ee5\u5229\u7528\u5bf9\u8c61\u5c5e\u6027\u4f5c\u4e3a\u5b57\u5178\u7684\u952e\u3002\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u5faa\u73af\u904d\u5386\u5bf9\u8c61\u5217\u8868\uff0c\u4f7f\u7528\u5c5e\u6027\u503c\u4f5c\u4e3a\u952e\uff0c\u5e76\u5c06\u6574\u4e2a\u5bf9\u8c61\u6216\u5176\u4ed6\u76f8\u5173\u6570\u636e\u4f5c\u4e3a\u503c\uff0c\u4fbf\u80fd\u8f7b\u677e\u6784\u5efa\u65b0\u7684\u5b57\u5178\u3002\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u7b80\u6d01\u6613\u8bfb\uff0c\u8fd8\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"PYTHON\u5982\u4f55\u5c06\u4e00\u4e2a\u5b57\u6bb5\u8bbe\u4e3a\u952e \u5728Python\u4e2d\u5c06\u4e00\u4e2a\u5b57\u6bb5\u8bbe\u4e3a\u952e\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u3001pandas\u5e93\u3001c [&hellip;]","protected":false},"author":3,"featured_media":1142192,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1142185"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1142185"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1142185\/revisions"}],"predecessor-version":[{"id":1142194,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1142185\/revisions\/1142194"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1142192"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1142185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1142185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1142185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}