{"id":1051053,"date":"2024-12-31T14:13:05","date_gmt":"2024-12-31T06:13:05","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1051053.html"},"modified":"2024-12-31T14:13:07","modified_gmt":"2024-12-31T06:13:07","slug":"python%e5%a6%82%e4%bd%95%e7%94%a8%e5%88%97%e8%a1%a8%e6%89%93%e5%87%ba1%e5%88%b0100","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1051053.html","title":{"rendered":"python\u5982\u4f55\u7528\u5217\u8868\u6253\u51fa1\u5230100"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/5e938eeb-0d85-4711-b9b8-e8245e55f247.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u7528\u5217\u8868\u6253\u51fa1\u5230100\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u751f\u6210\u5f0f\u5feb\u901f\u751f\u6210\u5305\u542b1\u5230100\u7684\u5217\u8868\u3002<\/strong>\u3001<strong>\u5217\u8868\u751f\u6210\u5f0f\u4e0d\u4ec5\u7b80\u6d01\uff0c\u8fd8\u5177\u6709\u9ad8\u6548\u7684\u6027\u80fd<\/strong>\u3002\u8fd9\u91cc\u6709\u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [i for i in range(1, 101)]<\/p>\n<p>print(numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u8f7b\u677e\u751f\u6210\u4e00\u4e2a\u5305\u542b1\u5230100\u7684\u5217\u8868\u3002\u5217\u8868\u751f\u6210\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u8fde\u7eed\u7684\u6570\u5b57\u5217\u8868\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u66f4\u590d\u6742\u7684\u751f\u6210\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5217\u8868\u751f\u6210\u5f0f\u7684\u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u751f\u6210\u5f0f\u662fPython\u4e2d\u4e00\u79cd\u7b80\u6d01\u800c\u9ad8\u6548\u7684\u521b\u5efa\u5217\u8868\u7684\u65b9\u5f0f\u3002\u5b83\u901a\u8fc7\u4e00\u4e2a\u8868\u8fbe\u5f0f\u6765\u751f\u6210\u5217\u8868\u7684\u5143\u7d20\uff0c\u5e76\u4e14\u53ef\u4ee5\u5305\u542b\u6761\u4ef6\u5224\u65ad\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u57fa\u672c\u7528\u6cd5\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u4e00\u4e2a\u5305\u542b1\u5230100\u7684\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u5982\u4e0a\u6240\u8ff0\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u751f\u6210\u5305\u542b1\u5230100\u7684\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [i for i in range(1, 101)]<\/p>\n<p>print(numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528\u4e86<code>range(1, 101)<\/code>\u51fd\u6570\u751f\u6210\u4ece1\u5230100\u7684\u6570\u5b57\u5e8f\u5217\uff0c\u5e76\u901a\u8fc7\u5217\u8868\u751f\u6210\u5f0f\u5c06\u8fd9\u4e9b\u6570\u5b57\u6dfb\u52a0\u5230\u5217\u8868<code>numbers<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>2. \u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5e73\u65b9\u6570\u7684\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u6211\u4eec\u60f3\u8981\u751f\u6210\u4e00\u4e2a\u5305\u542b1\u5230100\u7684\u5e73\u65b9\u6570\u7684\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">squares = [i2 for i in range(1, 101)]<\/p>\n<p>print(squares)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5728\u5217\u8868\u751f\u6210\u5f0f\u4e2d\u4f7f\u7528\u4e86\u8868\u8fbe\u5f0f<code>i2<\/code>\u6765\u8ba1\u7b97\u6bcf\u4e2a\u6570\u5b57\u7684\u5e73\u65b9\uff0c\u5e76\u5c06\u7ed3\u679c\u6dfb\u52a0\u5230\u5217\u8868<code>squares<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u6761\u4ef6\u8fc7\u6ee4<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u751f\u6210\u5f0f\u8fd8\u53ef\u4ee5\u5305\u542b\u6761\u4ef6\u5224\u65ad\uff0c\u7528\u4e8e\u7b5b\u9009\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u4e00\u4e2a\u5305\u542b1\u5230100\u4e4b\u95f4\u7684\u5076\u6570\u7684\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u5728\u5217\u8868\u751f\u6210\u5f0f\u4e2d\u6dfb\u52a0\u6761\u4ef6\u5224\u65ad\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">evens = [i for i in range(1, 101) if i % 2 == 0]<\/p>\n<p>print(evens)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528\u6761\u4ef6<code>if i % 2 == 0<\/code>\u6765\u7b5b\u9009\u51fa1\u5230100\u4e4b\u95f4\u7684\u5076\u6570\uff0c\u5e76\u5c06\u8fd9\u4e9b\u5076\u6570\u6dfb\u52a0\u5230\u5217\u8868<code>evens<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>2. \u521b\u5efa\u4e00\u4e2a\u5305\u542b1\u5230100\u4e4b\u95f4\u80fd\u88ab3\u6574\u9664\u7684\u6570\u7684\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">divisible_by_3 = [i for i in range(1, 101) if i % 3 == 0]<\/p>\n<p>print(divisible_by_3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528\u6761\u4ef6<code>if i % 3 == 0<\/code>\u6765\u7b5b\u9009\u51fa1\u5230100\u4e4b\u95f4\u80fd\u88ab3\u6574\u9664\u7684\u6570\uff0c\u5e76\u5c06\u8fd9\u4e9b\u6570\u6dfb\u52a0\u5230\u5217\u8868<code>divisible_by_3<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5d4c\u5957\u5217\u8868\u751f\u6210\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u751f\u6210\u5f0f\u53ef\u4ee5\u5d4c\u5957\u4f7f\u7528\uff0c\u7528\u4e8e\u751f\u6210\u591a\u7ef4\u5217\u8868\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1. \u751f\u6210\u4e00\u4e2a\u5305\u542b1\u523010\u7684\u4e58\u6cd5\u8868\u7684\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u5d4c\u5957\u5217\u8868\u751f\u6210\u5f0f\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">multiplication_table = [[i * j for j in range(1, 11)] for i in range(1, 11)]<\/p>\n<p>for row in multiplication_table:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u751f\u6210\u5f0f\u751f\u6210\u4e00\u4e2a\u5305\u542b1\u523010\u7684\u4e58\u6cd5\u8868\u7684\u4e8c\u7ef4\u5217\u8868\uff0c\u5e76\u5c06\u5176\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h4>2. \u751f\u6210\u4e00\u4e2a3&#215;3\u7684\u5355\u4f4d\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u5355\u4f4d\u77e9\u9635\u662f\u6307\u5bf9\u89d2\u7ebf\u4e0a\u7684\u5143\u7d20\u4e3a1\uff0c\u5176\u4f59\u5143\u7d20\u4e3a0\u7684\u77e9\u9635\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u751f\u62103&#215;3\u7684\u5355\u4f4d\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">identity_matrix = [[1 if i == j else 0 for j in range(3)] for i in range(3)]<\/p>\n<p>for row in identity_matrix:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u751f\u6210\u5f0f\u751f\u6210\u4e00\u4e2a3&#215;3\u7684\u5355\u4f4d\u77e9\u9635\uff0c\u5e76\u5c06\u5176\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5217\u8868\u751f\u6210\u5f0f\u7684\u6027\u80fd\u4f18\u52bf<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u751f\u6210\u5f0f\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u5728\u6027\u80fd\u4e0a\u4e5f\u5177\u6709\u4f18\u52bf\u3002\u76f8\u8f83\u4e8e\u4f20\u7edf\u7684for\u5faa\u73af\uff0c\u5217\u8868\u751f\u6210\u5f0f\u5728\u5904\u7406\u5927\u6570\u636e\u91cf\u65f6\u8868\u73b0\u66f4\u4e3a\u51fa\u8272\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u6027\u80fd\u5bf9\u6bd4\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528for\u5faa\u73af\u751f\u6210\u5217\u8868<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>start_time = time.time()<\/p>\n<p>numbers = []<\/p>\n<p>for i in range(1, 1000001):<\/p>\n<p>    numbers.append(i)<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(&quot;For loop time:&quot;, end_time - start_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u5217\u8868\u751f\u6210\u5f0f\u751f\u6210\u5217\u8868<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">start_time = time.time()<\/p>\n<p>numbers = [i for i in range(1, 1000001)]<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(&quot;List comprehension time:&quot;, end_time - start_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u6bd4\u8f83\u4e24\u79cd\u65b9\u6cd5\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u53ef\u4ee5\u53d1\u73b0\u5217\u8868\u751f\u6210\u5f0f\u901a\u5e38\u6bd4\u4f20\u7edf\u7684for\u5faa\u73af\u66f4\u5feb\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u6570\u636e\u91cf\u65f6\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5217\u8868\u751f\u6210\u5f0f\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u751f\u6210\u5f0f\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u573a\u666f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5e94\u7528\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1. \u6570\u636e\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u8fc7\u7a0b\u4e2d\uff0c\u5217\u8868\u751f\u6210\u5f0f\u53ef\u4ee5\u7528\u4e8e\u5feb\u901f\u751f\u6210\u548c\u7b5b\u9009\u6570\u636e\u3002\u4f8b\u5982\uff0c\u751f\u6210\u4e00\u4e2a\u5305\u542b\u6240\u6709\u6587\u4ef6\u540d\u7684\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>files = [f for f in os.listdir(&#39;.&#39;) if os.path.isfile(f)]<\/p>\n<p>print(files)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528\u5217\u8868\u751f\u6210\u5f0f\u751f\u6210\u5f53\u524d\u76ee\u5f55\u4e0b\u6240\u6709\u6587\u4ef6\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>2. \u6570\u636e\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u751f\u6210\u5f0f\u53ef\u4ee5\u7528\u4e8e\u5c06\u4e00\u79cd\u6570\u636e\u683c\u5f0f\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u6570\u636e\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u5c06\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u6240\u6709\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5927\u5199\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">strings = [&#39;hello&#39;, &#39;world&#39;, &#39;python&#39;]<\/p>\n<p>uppercase_strings = [s.upper() for s in strings]<\/p>\n<p>print(uppercase_strings)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528\u5217\u8868\u751f\u6210\u5f0f\u5c06\u5b57\u7b26\u4e32\u5217\u8868\u4e2d\u7684\u6240\u6709\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5927\u5199\u3002<\/p>\n<\/p>\n<p><h4>3. \u6570\u636e\u7b5b\u9009<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u751f\u6210\u5f0f\u53ef\u4ee5\u7528\u4e8e\u6839\u636e\u6761\u4ef6\u7b5b\u9009\u6570\u636e\u3002\u4f8b\u5982\uff0c\u7b5b\u9009\u51fa\u4e00\u4e2a\u5217\u8868\u4e2d\u6240\u6709\u5927\u4e8e10\u7684\u6570\u5b57\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 15, 3, 22, 5, 18]<\/p>\n<p>filtered_numbers = [n for n in numbers if n &gt; 10]<\/p>\n<p>print(filtered_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528\u5217\u8868\u751f\u6210\u5f0f\u7b5b\u9009\u51fa\u5217\u8868\u4e2d\u6240\u6709\u5927\u4e8e10\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6ce8\u610f\u4e8b\u9879\u548c\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u5217\u8868\u751f\u6210\u5f0f\u975e\u5e38\u5f3a\u5927\uff0c\u4f46\u5728\u4f7f\u7528\u65f6\u4e5f\u9700\u8981\u6ce8\u610f\u4e00\u4e9b\u4e8b\u9879\u548c\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n<\/p>\n<p><h4>1. \u4ee3\u7801\u53ef\u8bfb\u6027<\/h4>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u5217\u8868\u751f\u6210\u5f0f\u65f6\uff0c\u5c3d\u91cf\u4fdd\u6301\u4ee3\u7801\u7684\u7b80\u6d01\u548c\u53ef\u8bfb\u6027\u3002\u5982\u679c\u5217\u8868\u751f\u6210\u5f0f\u8fc7\u4e8e\u590d\u6742\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4ee3\u7801\u96be\u4ee5\u7406\u89e3\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8003\u8651\u5c06\u5217\u8868\u751f\u6210\u5f0f\u62c6\u5206\u4e3a\u591a\u4e2a\u6b65\u9aa4\uff0c\u6216\u8005\u4f7f\u7528\u4f20\u7edf\u7684for\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h4>2. \u907f\u514d\u5d4c\u5957\u8fc7\u6df1<\/h4>\n<\/p>\n<p><p>\u5d4c\u5957\u5217\u8868\u751f\u6210\u5f0f\u867d\u7136\u5f3a\u5927\uff0c\u4f46\u5d4c\u5957\u8fc7\u6df1\u4f1a\u5bfc\u81f4\u4ee3\u7801\u96be\u4ee5\u7ef4\u62a4\u3002\u5c3d\u91cf\u907f\u514d\u8fc7\u4e8e\u590d\u6742\u7684\u5d4c\u5957\u7ed3\u6784\uff0c\u4fdd\u6301\u4ee3\u7801\u7684\u7b80\u5355\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><h4>3. \u6027\u80fd\u4f18\u5316<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6570\u636e\u91cf\u65f6\uff0c\u5217\u8868\u751f\u6210\u5f0f\u53ef\u4ee5\u63d0\u4f9b\u6027\u80fd\u4f18\u52bf\u3002\u7136\u800c\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u751f\u6210\u7684\u4e2d\u95f4\u7ed3\u679c\u53ef\u80fd\u4f1a\u5360\u7528\u5927\u91cf\u5185\u5b58\u3002\u6839\u636e\u5177\u4f53\u60c5\u51b5\uff0c\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5904\u7406\u65b9\u5f0f\uff0c\u907f\u514d\u5185\u5b58\u6d88\u8017\u8fc7\u5927\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u751f\u6210\u5f0f\u662fPython\u4e2d\u4e00\u79cd\u7b80\u6d01\u3001\u9ad8\u6548\u7684\u521b\u5efa\u5217\u8868\u7684\u65b9\u5f0f\u3002\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5217\u8868\u751f\u6210\u5f0f\u7684\u57fa\u672c\u7528\u6cd5\u3001\u6761\u4ef6\u8fc7\u6ee4\u3001\u5d4c\u5957\u4f7f\u7528\u3001\u6027\u80fd\u4f18\u52bf\u4ee5\u53ca\u5b9e\u9645\u5e94\u7528\u573a\u666f\u3002\u540c\u65f6\uff0c\u6211\u4eec\u4e5f\u8ba8\u8bba\u4e86\u4e00\u4e9b\u4f7f\u7528\u5217\u8868\u751f\u6210\u5f0f\u65f6\u9700\u8981\u6ce8\u610f\u7684\u4e8b\u9879\u548c\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u662f\u5728\u6570\u636e\u5904\u7406\u3001\u6570\u636e\u8f6c\u6362\u8fd8\u662f\u6570\u636e\u7b5b\u9009\u4e2d\uff0c\u5217\u8868\u751f\u6210\u5f0f\u90fd\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5feb\u901f\u751f\u6210\u6240\u9700\u7684\u6570\u636e\uff0c\u5e76\u63d0\u9ad8\u4ee3\u7801\u7684\u7b80\u6d01\u6027\u548c\u6027\u80fd\u3002\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u638c\u63e1\u548c\u7075\u6d3b\u8fd0\u7528\u5217\u8868\u751f\u6210\u5f0f\uff0c\u5c06\u4f7f\u6211\u4eec\u7684Python\u7f16\u7a0b\u66f4\u52a0\u9ad8\u6548\u548c\u4fbf\u6377\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u5217\u8868\u751f\u62101\u5230100\u7684\u6570\u5b57\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>range()<\/code>\u51fd\u6570\u7ed3\u5408<code>list()<\/code>\u51fd\u6570\u6765\u751f\u6210\u4e00\u4e2a\u5305\u542b1\u5230100\u6570\u5b57\u7684\u5217\u8868\u3002\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">numbers = list(range(1, 101))\nprint(numbers)\n<\/code><\/pre>\n<p>\u8fd9\u884c\u4ee3\u7801\u4f1a\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u4ece1\u5230100\u7684\u6240\u6709\u6574\u6570\u7684\u5217\u8868\uff0c\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<p><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5728Python\u4e2d\u751f\u62101\u5230100\u7684\u6570\u5b57\u6709\u4ec0\u4e48\u597d\u5904\uff1f<\/strong><br \/>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u4e14\u9ad8\u6548\u7684\u521b\u5efa\u5217\u8868\u7684\u65b9\u6cd5\u3002\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u62101\u5230100\u7684\u6570\u5b57\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u7b80\u6d01\uff0c\u6613\u4e8e\u9605\u8bfb\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">numbers = [i for i in range(1, 101)]\nprint(numbers)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u76f4\u89c2\uff0c\u800c\u4e14\u5728\u5904\u7406\u66f4\u590d\u6742\u7684\u903b\u8f91\u65f6\u4e5f\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5c06\u751f\u6210\u76841\u5230100\u7684\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u6216\u5904\u7406\uff1f<\/strong><br \/>\u867d\u7136\u751f\u6210\u7684\u5217\u8868\u9ed8\u8ba4\u662f\u6309\u987a\u5e8f\u6392\u5217\u7684\uff0c\u4f46\u60a8\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528<code>sort()<\/code>\u65b9\u6cd5\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002\u6b64\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6216\u5217\u8868\u63a8\u5bfc\u5f0f\u5bf9\u8fd9\u4e9b\u6570\u5b57\u8fdb\u884c\u5904\u7406\u3002\u4f8b\u5982\uff0c\u82e5\u60f3\u8981\u751f\u6210\u6bcf\u4e2a\u6570\u5b57\u7684\u5e73\u65b9\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a  <\/p>\n<pre><code class=\"language-python\">squares = [i**2 for i in range(1, 101)]\nprint(squares)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u7075\u6d3b\u6027\u4f7f\u5f97\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5bf9\u5217\u8868\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u751f\u6210\u5f0f\u5feb\u901f\u751f\u6210\u5305\u542b1\u5230100\u7684\u5217\u8868\u3002\u3001\u5217\u8868\u751f\u6210\u5f0f\u4e0d\u4ec5\u7b80\u6d01\uff0c\u8fd8\u5177\u6709\u9ad8\u6548\u7684\u6027\u80fd\u3002\u8fd9\u91cc [&hellip;]","protected":false},"author":3,"featured_media":1051059,"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\/1051053"}],"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=1051053"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1051053\/revisions"}],"predecessor-version":[{"id":1051064,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1051053\/revisions\/1051064"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1051059"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1051053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1051053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1051053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}