{"id":1126885,"date":"2025-01-08T20:05:15","date_gmt":"2025-01-08T12:05:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1126885.html"},"modified":"2025-01-08T20:05:18","modified_gmt":"2025-01-08T12:05:18","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%b0%86%e7%94%b7%e7%94%9f%e5%a5%b3%e7%94%9f%e5%88%86%e5%bc%80","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1126885.html","title":{"rendered":"Python\u4e2d\u5982\u4f55\u5c06\u7537\u751f\u5973\u751f\u5206\u5f00"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25094047\/265b7b4a-24d1-4e60-b4a9-213fcadfdb0e.webp\" alt=\"Python\u4e2d\u5982\u4f55\u5c06\u7537\u751f\u5973\u751f\u5206\u5f00\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5c06\u7537\u751f\u5973\u751f\u5206\u5f00\uff0c\u53ef\u4ee5\u901a\u8fc7\u6570\u636e\u5206\u7c7b\u3001\u6570\u636e\u8fc7\u6ee4\u3001\u4f7f\u7528\u5217\u8868\u548c\u5b57\u5178\u7b49\u65b9\u6cd5\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4e3b\u8981\u65b9\u6cd5\uff1a\u6570\u636e\u5206\u7c7b\u3001\u6570\u636e\u8fc7\u6ee4\u3001\u4f7f\u7528\u5217\u8868\u548c\u5b57\u5178\u3002<\/strong> <\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c\u6570\u636e\u5206\u7c7b\u662f\u4e00\u79cd\u975e\u5e38\u6709\u6548\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u5c06\u6570\u636e\u6309\u6027\u522b\u5206\u7c7b\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5bf9\u7537\u751f\u548c\u5973\u751f\u8fdb\u884c\u4e0d\u540c\u7684\u5904\u7406\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u6570\u636e\u5206\u7c7b\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6570\u636e\u5206\u7c7b<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u5206\u7c7b\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6570\u636e\u5904\u7406\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e0d\u540c\u7684\u5c5e\u6027\u5c06\u6570\u636e\u5206\u6210\u82e5\u5e72\u7c7b\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u65b9\u6cd5\u6765\u5b9e\u73b0\u6570\u636e\u5206\u7c7b\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u5206\u7c7b<\/h4>\n<\/p>\n<p><p>\u6761\u4ef6\u8bed\u53e5\u662f\u4e00\u79cd\u57fa\u672c\u7684\u63a7\u5236\u7ed3\u6784\uff0c\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u6761\u4ef6\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">students = [<\/p>\n<p>    {&quot;name&quot;: &quot;Alice&quot;, &quot;gender&quot;: &quot;Female&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Bob&quot;, &quot;gender&quot;: &quot;Male&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Cathy&quot;, &quot;gender&quot;: &quot;Female&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;David&quot;, &quot;gender&quot;: &quot;Male&quot;}<\/p>\n<p>]<\/p>\n<p>males = []<\/p>\n<p>females = []<\/p>\n<p>for student in students:<\/p>\n<p>    if student[&quot;gender&quot;] == &quot;Male&quot;:<\/p>\n<p>        males.append(student)<\/p>\n<p>    else:<\/p>\n<p>        females.append(student)<\/p>\n<p>print(&quot;Males:&quot;, males)<\/p>\n<p>print(&quot;Females:&quot;, females)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4ee3\u7801\u901a\u8fc7\u904d\u5386\u5b66\u751f\u5217\u8868\uff0c\u6839\u636e\u6027\u522b\u5c06\u5b66\u751f\u5206\u6210\u7537\u751f\u548c\u5973\u751f\u4e24\u4e2a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5206\u7c7b<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5b9e\u73b0\u5217\u8868\u7684\u521b\u5efa\u548c\u8fc7\u6ee4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">students = [<\/p>\n<p>    {&quot;name&quot;: &quot;Alice&quot;, &quot;gender&quot;: &quot;Female&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Bob&quot;, &quot;gender&quot;: &quot;Male&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Cathy&quot;, &quot;gender&quot;: &quot;Female&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;David&quot;, &quot;gender&quot;: &quot;Male&quot;}<\/p>\n<p>]<\/p>\n<p>males = [student for student in students if student[&quot;gender&quot;] == &quot;Male&quot;]<\/p>\n<p>females = [student for student in students if student[&quot;gender&quot;] == &quot;Female&quot;]<\/p>\n<p>print(&quot;Males:&quot;, males)<\/p>\n<p>print(&quot;Females:&quot;, females)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4ee3\u7801\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5b9e\u73b0\u4e86\u4e0e\u4e0a\u4e00\u4e2a\u793a\u4f8b\u76f8\u540c\u7684\u529f\u80fd\uff0c\u4f46\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u6570\u636e\u8fc7\u6ee4<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u8fc7\u6ee4\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6570\u636e\u5904\u7406\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7\u6761\u4ef6\u7b5b\u9009\u51fa\u7b26\u5408\u6761\u4ef6\u7684\u6570\u636e\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528filter\u51fd\u6570\u6765\u5b9e\u73b0\u6570\u636e\u8fc7\u6ee4\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528filter\u51fd\u6570<\/h4>\n<\/p>\n<p><p>filter\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u9ad8\u9636\u51fd\u6570\uff0c\u53ef\u4ee5\u6839\u636e\u6761\u4ef6\u7b5b\u9009\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">students = [<\/p>\n<p>    {&quot;name&quot;: &quot;Alice&quot;, &quot;gender&quot;: &quot;Female&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Bob&quot;, &quot;gender&quot;: &quot;Male&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Cathy&quot;, &quot;gender&quot;: &quot;Female&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;David&quot;, &quot;gender&quot;: &quot;Male&quot;}<\/p>\n<p>]<\/p>\n<p>males = list(filter(lambda student: student[&quot;gender&quot;] == &quot;Male&quot;, students))<\/p>\n<p>females = list(filter(lambda student: student[&quot;gender&quot;] == &quot;Female&quot;, students))<\/p>\n<p>print(&quot;Males:&quot;, males)<\/p>\n<p>print(&quot;Females:&quot;, females)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4ee3\u7801\u4f7f\u7528filter\u51fd\u6570\u7b5b\u9009\u51fa\u7537\u751f\u548c\u5973\u751f\uff0c\u5e76\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528pandas\u5e93\u8fdb\u884c\u6570\u636e\u8fc7\u6ee4<\/h4>\n<\/p>\n<p><p>pandas\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u6570\u636e\u8fdb\u884c\u8fc7\u6ee4\u548c\u5206\u7c7b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {<\/p>\n<p>    &quot;name&quot;: [&quot;Alice&quot;, &quot;Bob&quot;, &quot;Cathy&quot;, &quot;David&quot;],<\/p>\n<p>    &quot;gender&quot;: [&quot;Female&quot;, &quot;Male&quot;, &quot;Female&quot;, &quot;Male&quot;]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>males = df[df[&quot;gender&quot;] == &quot;Male&quot;]<\/p>\n<p>females = df[df[&quot;gender&quot;] == &quot;Female&quot;]<\/p>\n<p>print(&quot;Males:&quot;)<\/p>\n<p>print(males)<\/p>\n<p>print(&quot;Females:&quot;)<\/p>\n<p>print(females)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4ee3\u7801\u4f7f\u7528pandas\u5e93\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u6846\uff0c\u5e76\u6839\u636e\u6027\u522b\u8fdb\u884c\u8fc7\u6ee4\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u548c\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u548c\u5b57\u5178\u662fPython\u4e2d\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u4e8e\u5b58\u50a8\u548c\u5904\u7406\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528\u5217\u8868\u548c\u5b57\u5178\u6765\u5c06\u7537\u751f\u548c\u5973\u751f\u5206\u5f00\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u5d4c\u5957\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u5d4c\u5957\u5217\u8868\u662f\u4e00\u79cd\u5217\u8868\u4e2d\u5305\u542b\u5217\u8868\u7684\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u4e8e\u5b58\u50a8\u591a\u4e2a\u7c7b\u522b\u7684\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">students = [<\/p>\n<p>    [&quot;Alice&quot;, &quot;Female&quot;],<\/p>\n<p>    [&quot;Bob&quot;, &quot;Male&quot;],<\/p>\n<p>    [&quot;Cathy&quot;, &quot;Female&quot;],<\/p>\n<p>    [&quot;David&quot;, &quot;Male&quot;]<\/p>\n<p>]<\/p>\n<p>males = []<\/p>\n<p>females = []<\/p>\n<p>for student in students:<\/p>\n<p>    if student[1] == &quot;Male&quot;:<\/p>\n<p>        males.append(student)<\/p>\n<p>    else:<\/p>\n<p>        females.append(student)<\/p>\n<p>print(&quot;Males:&quot;, males)<\/p>\n<p>print(&quot;Females:&quot;, females)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4ee3\u7801\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u5b58\u50a8\u5b66\u751f\u4fe1\u606f\uff0c\u5e76\u6839\u636e\u6027\u522b\u5c06\u5b66\u751f\u5206\u6210\u7537\u751f\u548c\u5973\u751f\u4e24\u4e2a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u662f\u4e00\u79cd\u952e\u503c\u5bf9\u7ed3\u6784\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b58\u50a8\u548c\u67e5\u627e\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">students = {<\/p>\n<p>    &quot;Alice&quot;: &quot;Female&quot;,<\/p>\n<p>    &quot;Bob&quot;: &quot;Male&quot;,<\/p>\n<p>    &quot;Cathy&quot;: &quot;Female&quot;,<\/p>\n<p>    &quot;David&quot;: &quot;Male&quot;<\/p>\n<p>}<\/p>\n<p>males = {}<\/p>\n<p>females = {}<\/p>\n<p>for name, gender in students.items():<\/p>\n<p>    if gender == &quot;Male&quot;:<\/p>\n<p>        males[name] = gender<\/p>\n<p>    else:<\/p>\n<p>        females[name] = gender<\/p>\n<p>print(&quot;Males:&quot;, males)<\/p>\n<p>print(&quot;Females:&quot;, females)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4ee3\u7801\u4f7f\u7528\u5b57\u5178\u5b58\u50a8\u5b66\u751f\u4fe1\u606f\uff0c\u5e76\u6839\u636e\u6027\u522b\u5c06\u5b66\u751f\u5206\u6210\u7537\u751f\u548c\u5973\u751f\u4e24\u4e2a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7efc\u5408\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u7efc\u5408\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7efc\u5408\u5e94\u7528\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u5b66\u751f\u4fe1\u606f\u7684CSV\u6587\u4ef6\uff0c\u5305\u542b\u5b66\u751f\u7684\u59d3\u540d\u3001\u6027\u522b\u3001\u5e74\u9f84\u3001\u73ed\u7ea7\u7b49\u4fe1\u606f\u3002\u6211\u4eec\u5e0c\u671b\u5c06\u8fd9\u4e9b\u5b66\u751f\u6309\u6027\u522b\u5206\u5f00\uff0c\u5e76\u5206\u522b\u8ba1\u7b97\u7537\u751f\u548c\u5973\u751f\u7684\u5e73\u5747\u5e74\u9f84\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8bfb\u53d6CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u8bfb\u53d6CSV\u6587\u4ef6\uff0c\u5e76\u5c06\u6570\u636e\u5b58\u50a8\u5728\u4e00\u4e2a\u6570\u636e\u7ed3\u6784\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u6765\u5b9e\u73b0\u8fd9\u4e00\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.read_csv(&quot;students.csv&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6309\u6027\u522b\u5206\u7ec4<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u7684\u5206\u7ec4\u529f\u80fd\uff0c\u5c06\u5b66\u751f\u6309\u6027\u522b\u5206\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">grouped = df.groupby(&quot;gender&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u8ba1\u7b97\u5e73\u5747\u5e74\u9f84<\/h4>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5206\u522b\u8ba1\u7b97\u7537\u751f\u548c\u5973\u751f\u7684\u5e73\u5747\u5e74\u9f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">males = grouped.get_group(&quot;Male&quot;)<\/p>\n<p>females = grouped.get_group(&quot;Female&quot;)<\/p>\n<p>male_avg_age = males[&quot;age&quot;].mean()<\/p>\n<p>female_avg_age = females[&quot;age&quot;].mean()<\/p>\n<p>print(&quot;Average age of males:&quot;, male_avg_age)<\/p>\n<p>print(&quot;Average age of females:&quot;, female_avg_age)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u7efc\u5408\u4f7f\u7528\u8bfb\u53d6\u6587\u4ef6\u3001\u5206\u7ec4\u548c\u8ba1\u7b97\u7edf\u8ba1\u91cf\u7684\u65b9\u6cd5\u6765\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5c06\u7537\u751f\u5973\u751f\u5206\u5f00\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5305\u62ec<strong>\u6570\u636e\u5206\u7c7b\u3001\u6570\u636e\u8fc7\u6ee4\u3001\u4f7f\u7528\u5217\u8868\u548c\u5b57\u5178<\/strong>\u7b49\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u6570\u636e\u5206\u7c7b\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u5408\u5904\u7406\u5c0f\u89c4\u6a21\u6570\u636e\uff1b\u6570\u636e\u8fc7\u6ee4\u65b9\u6cd5\u7075\u6d3b\u9ad8\u6548\uff0c\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\uff1b\u4f7f\u7528\u5217\u8868\u548c\u5b57\u5178\u65b9\u6cd5\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b58\u50a8\u548c\u67e5\u627e\u6570\u636e\uff0c\u9002\u5408\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u7efc\u5408\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6839\u636e\u6027\u522b\u5c06\u7537\u751f\u548c\u5973\u751f\u5206\u5f00\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u548c\u5217\u8868\u63a8\u5bfc\u6765\u6839\u636e\u6027\u522b\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u6709\u4e00\u4e2a\u5305\u542b\u6027\u522b\u4fe1\u606f\u7684\u5217\u8868\u6216\u6570\u636e\u7ed3\u6784\u3002\u53ef\u4ee5\u4f7f\u7528<code>if<\/code>\u8bed\u53e5\u68c0\u67e5\u6bcf\u4e2a\u5143\u7d20\u7684\u6027\u522b\u5c5e\u6027\uff0c\u7136\u540e\u5c06\u5176\u6dfb\u52a0\u5230\u76f8\u5e94\u7684\u5217\u8868\u4e2d\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">data = [{&#39;name&#39;: &#39;Alice&#39;, &#39;gender&#39;: &#39;female&#39;}, {&#39;name&#39;: &#39;Bob&#39;, &#39;gender&#39;: &#39;male&#39;}, ...]\nmales = [person for person in data if person[&#39;gender&#39;] == &#39;male&#39;]\nfemales = [person for person in data if person[&#39;gender&#39;] == &#39;female&#39;]\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5904\u7406\u6027\u522b\u672a\u660e\u786e\u6807\u8bb0\u7684\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u5904\u7406\u6570\u636e\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6027\u522b\u672a\u6807\u8bb0\u6216\u4e0d\u660e\u786e\u7684\u60c5\u51b5\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u6761\u4ef6\u8bed\u53e5\u4e2d\u6dfb\u52a0\u989d\u5916\u7684\u5224\u65ad\u6765\u5904\u7406\u8fd9\u4e9b\u60c5\u51b5\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9\u5c06\u8fd9\u4e9b\u672a\u6807\u8bb0\u7684\u8bb0\u5f55\u5355\u72ec\u5b58\u653e\uff0c\u6216\u8005\u5c06\u5176\u6807\u8bb0\u4e3a\u201c\u672a\u77e5\u201d\u3002<\/p>\n<pre><code class=\"language-python\">unknown = [person for person in data if person[&#39;gender&#39;] not in [&#39;male&#39;, &#39;female&#39;]]\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u53ef\u89c6\u5316\u5206\u5f00\u7684\u6027\u522b\u6570\u636e\uff1f<\/strong><br \/>\u4e00\u65e6\u4f60\u6210\u529f\u5c06\u7537\u751f\u548c\u5973\u751f\u5206\u5f00\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684\u53ef\u89c6\u5316\u5e93\uff08\u5982Matplotlib\u6216Seaborn\uff09\u6765\u5c55\u793a\u8fd9\u4e9b\u6570\u636e\u3002\u53ef\u4ee5\u7ed8\u5236\u67f1\u72b6\u56fe\u6216\u997c\u56fe\uff0c\u6765\u6e05\u6670\u5730\u5c55\u793a\u6027\u522b\u5206\u5e03\u60c5\u51b5\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\n\nlabels = [&#39;Male&#39;, &#39;Female&#39;]\nsizes = [len(males), len(females)]\nplt.bar(labels, sizes)\nplt.title(&#39;Gender Distribution&#39;)\nplt.show()\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5c06\u7537\u751f\u5973\u751f\u5206\u5f00\uff0c\u53ef\u4ee5\u901a\u8fc7\u6570\u636e\u5206\u7c7b\u3001\u6570\u636e\u8fc7\u6ee4\u3001\u4f7f\u7528\u5217\u8868\u548c\u5b57\u5178\u7b49\u65b9\u6cd5\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4e3b\u8981\u65b9\u6cd5\uff1a\u6570\u636e [&hellip;]","protected":false},"author":3,"featured_media":1126892,"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\/1126885"}],"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=1126885"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1126885\/revisions"}],"predecessor-version":[{"id":1126893,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1126885\/revisions\/1126893"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1126892"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1126885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1126885"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1126885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}