{"id":1070418,"date":"2025-01-08T10:57:59","date_gmt":"2025-01-08T02:57:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1070418.html"},"modified":"2025-01-08T10:58:01","modified_gmt":"2025-01-08T02:58:01","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e5%88%97%e8%a1%a8%e8%bd%ac%e5%8c%96%e4%b8%ba%e6%95%b4%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1070418.html","title":{"rendered":"python\u5982\u4f55\u5c06\u5217\u8868\u8f6c\u5316\u4e3a\u6574\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101142\/bc1bad6c-e7d3-44a9-bf93-f3b89538fa0d.webp\" alt=\"python\u5982\u4f55\u5c06\u5217\u8868\u8f6c\u5316\u4e3a\u6574\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5c06\u5217\u8868\u8f6c\u5316\u4e3a\u6574\u6570\u7684\u5e38\u89c1\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5faa\u73af\u904d\u5386\u5e76\u62fc\u63a5\u5b57\u7b26\u4e32\u7136\u540e\u8f6c\u6362\u6210\u6574\u6570\u3001\u4f7f\u7528map\u51fd\u6570\u7ed3\u5408join\u65b9\u6cd5\u3001\u4f7f\u7528\u9012\u5f52\u65b9\u6cd5\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528\u5faa\u73af\u904d\u5386\u5e76\u62fc\u63a5\u5b57\u7b26\u4e32\u7136\u540e\u8f6c\u6362\u6210\u6574\u6570\u662f\u6700\u4e3a\u5e38\u89c1\u548c\u7b80\u4fbf\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u8fd9\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u5faa\u73af\u904d\u5386\u5e76\u62fc\u63a5\u5b57\u7b26\u4e32\u7136\u540e\u8f6c\u6362\u6210\u6574\u6570<\/strong>\uff1a\u8fd9\u662f\u6700\u4e3a\u76f4\u89c2\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u6211\u4eec\u53ef\u4ee5\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5e76\u62fc\u63a5\u6210\u4e00\u4e2a\u5927\u7684\u5b57\u7b26\u4e32\uff0c\u6700\u540e\u5c06\u62fc\u63a5\u540e\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># Sample list<\/p>\n<p>list_items = [1, 2, 3, 4]<\/p>\n<h2><strong>Initialize an empty string<\/strong><\/h2>\n<p>num_str = &quot;&quot;<\/p>\n<h2><strong>Traverse through the list and concatenate each element to the string<\/strong><\/h2>\n<p>for item in list_items:<\/p>\n<p>    num_str += str(item)<\/p>\n<h2><strong>Convert the concatenated string to an integer<\/strong><\/h2>\n<p>result = int(num_str)<\/p>\n<p>print(result)  # Output: 1234<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528map\u51fd\u6570\u7ed3\u5408join\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>map<\/code>\u51fd\u6570\u7ed3\u5408<code>join<\/code>\u65b9\u6cd5\u662f\u53e6\u4e00\u79cd\u7b80\u4fbf\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u3002<code>map<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c<code>join<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u8fd9\u4e9b\u5b57\u7b26\u4e32\u62fc\u63a5\u6210\u4e00\u4e2a\u5927\u7684\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u518d\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list_items = [1, 2, 3, 4]<\/p>\n<p>num_str = &#39;&#39;.join(map(str, list_items))<\/p>\n<p>result = int(num_str)<\/p>\n<p>print(result)  # Output: 1234<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u9012\u5f52\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u65b9\u6cd5\u76f8\u5bf9\u590d\u6742\uff0c\u4f46\u5728\u67d0\u4e9b\u7279\u5b9a\u60c5\u51b5\u4e0b\u4e5f\u80fd\u6d3e\u4e0a\u7528\u573a\u3002\u9012\u5f52\u65b9\u6cd5\u662f\u901a\u8fc7\u9012\u5f52\u8c03\u7528\u51fd\u6570\uff0c\u5c06\u5217\u8868\u5143\u7d20\u9010\u4e2a\u62fc\u63a5\u6210\u5b57\u7b26\u4e32\uff0c\u6700\u540e\u518d\u8f6c\u6362\u6210\u6574\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def list_to_int(lst):<\/p>\n<p>    if not lst:<\/p>\n<p>        return &quot;&quot;<\/p>\n<p>    return str(lst[0]) + list_to_int(lst[1:])<\/p>\n<p>list_items = [1, 2, 3, 4]<\/p>\n<p>num_str = list_to_int(list_items)<\/p>\n<p>result = int(num_str)<\/p>\n<p>print(result)  # Output: 1234<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528reduce\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>reduce<\/code>\u51fd\u6570\u6765\u81ea<code>functools<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u5c06\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u5408\u5e76\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u6700\u540e\u518d\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u8f83\u5c11\u4f7f\u7528\uff0c\u4f46\u540c\u6837\u6709\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import reduce<\/p>\n<p>list_items = [1, 2, 3, 4]<\/p>\n<p>num_str = reduce(lambda x, y: str(x) + str(y), list_items)<\/p>\n<p>result = int(num_str)<\/p>\n<p>print(result)  # Output: 1234<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u5305\u542b\u975e\u6574\u6570\u5143\u7d20\u7684\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5217\u8868\u53ef\u80fd\u5305\u542b\u975e\u6574\u6570\u5143\u7d20\uff0c\u5982\u5b57\u7b26\u4e32\u6216\u6d6e\u70b9\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8fc7\u6ee4\u5668\u6216\u6761\u4ef6\u8bed\u53e5\u6765\u786e\u4fdd\u5217\u8868\u4e2d\u7684\u6240\u6709\u5143\u7d20\u90fd\u662f\u6574\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list_items = [1, &#39;a&#39;, 3, 4.5, 6]<\/p>\n<h2><strong>Filter out non-integer elements<\/strong><\/h2>\n<p>filtered_list = [item for item in list_items if isinstance(item, int)]<\/p>\n<h2><strong>Convert filtered list to integer<\/strong><\/h2>\n<p>num_str = &#39;&#39;.join(map(str, filtered_list))<\/p>\n<p>result = int(num_str)<\/p>\n<p>print(result)  # Output: 136<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u5305\u542b\u8d1f\u6570\u7684\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5904\u7406\u8d1f\u6570\u7684\u5217\u8868\u65f6\uff0c\u9700\u8981\u7279\u522b\u5c0f\u5fc3\uff0c\u786e\u4fdd\u8d1f\u53f7\u4e0d\u4f1a\u88ab\u5ffd\u7565\u6216\u8bef\u5904\u7406\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6761\u4ef6\u8bed\u53e5\u6765\u68c0\u67e5\u6bcf\u4e2a\u5143\u7d20\u5e76\u9002\u5f53\u5904\u7406\u8d1f\u53f7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list_items = [-1, 2, -3, 4]<\/p>\n<h2><strong>Initialize an empty string<\/strong><\/h2>\n<p>num_str = &quot;&quot;<\/p>\n<p>for item in list_items:<\/p>\n<p>    if item &lt; 0:<\/p>\n<p>        num_str += &#39;-&#39; + str(abs(item))<\/p>\n<p>    else:<\/p>\n<p>        num_str += str(item)<\/p>\n<p>result = int(num_str)<\/p>\n<p>print(result)  # Output: -1234<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u975e\u5e38\u5927\u7684\u6574\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u975e\u5e38\u5927\u7684\u6574\u6570\u65f6\uff0cPython\u7684\u5185\u7f6e\u6574\u6570\u7c7b\u578b\u4e5f\u53ef\u4ee5\u5904\u7406\uff0c\u56e0\u4e3aPython\u7684\u6574\u6570\u7c7b\u578b\u662f\u4efb\u610f\u7cbe\u5ea6\u7684\u3002\u7136\u800c\uff0c\u5982\u679c\u5217\u8868\u592a\u5927\uff0c\u62fc\u63a5\u548c\u8f6c\u6362\u8fc7\u7a0b\u53ef\u80fd\u4f1a\u975e\u5e38\u8017\u65f6\u548c\u5360\u7528\u5927\u91cf\u5185\u5b58\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f18\u5316\u7b97\u6cd5\u548c\u4f7f\u7528\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\u662f\u5fc5\u8981\u7684\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<h2><strong>Generate a large list<\/strong><\/h2>\n<p>list_items = list(range(1, 100000))<\/p>\n<h2><strong>Measure time taken to convert list to integer<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>num_str = &#39;&#39;.join(map(str, list_items))<\/p>\n<p>result = int(num_str)<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&quot;Time taken: {end_time - start_time} seconds&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u4f55\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u60c5\u5883\u3002\u5bf9\u4e8e\u4e00\u822c\u7684\u5c0f\u578b\u5217\u8868\uff0c\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6216<code>map<\/code>\u51fd\u6570\u7ed3\u5408<code>join<\/code>\u65b9\u6cd5\u662f\u6700\u4e3a\u7b80\u4fbf\u548c\u9ad8\u6548\u7684\u3002\u800c\u5bf9\u4e8e\u5305\u542b\u590d\u6742\u6570\u636e\u7c7b\u578b\u6216\u8d1f\u6570\u7684\u5217\u8868\uff0c\u5219\u9700\u8981\u66f4\u4e3a\u590d\u6742\u7684\u5904\u7406\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><p><strong>\u6700\u4f73\u5b9e\u8df5<\/strong>\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u786e\u4fdd\u5217\u8868\u5143\u7d20\u7c7b\u578b\u4e00\u81f4<\/strong>\uff1a\u5728\u5904\u7406\u5217\u8868\u524d\uff0c\u53ef\u4ee5\u5148\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\u6216\u8fc7\u6ee4\uff0c\u786e\u4fdd\u6240\u6709\u5143\u7d20\u90fd\u662f\u6574\u6570\u3002<\/li>\n<li><strong>\u5904\u7406\u5f02\u5e38\u60c5\u51b5<\/strong>\uff1a\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u5982\u5217\u8868\u4e3a\u7a7a\u3001\u5217\u8868\u5305\u542b\u975e\u6574\u6570\u5143\u7d20\u7b49\u3002\u5e94\u5728\u4ee3\u7801\u4e2d\u6dfb\u52a0\u9002\u5f53\u7684\u5f02\u5e38\u5904\u7406\u903b\u8f91\u3002<\/li>\n<li><strong>\u4f18\u5316\u6027\u80fd<\/strong>\uff1a\u5bf9\u4e8e\u975e\u5e38\u5927\u7684\u5217\u8868\uff0c\u5e94\u8003\u8651\u7b97\u6cd5\u4f18\u5316\u548c\u4f7f\u7528\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4ee5\u63d0\u9ad8\u6027\u80fd\u5e76\u51cf\u5c11\u5185\u5b58\u5360\u7528\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5c06\u5217\u8868\u8f6c\u5316\u4e3a\u6574\u6570\uff0c\u6ee1\u8db3\u5404\u79cd\u5e94\u7528\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u5217\u8868\u4e2d\u7684\u5b57\u7b26\u4e32\u5143\u7d20\u8f6c\u6362\u4e3a\u6574\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216<code>map()<\/code>\u51fd\u6570\u5c06\u5217\u8868\u4e2d\u7684\u5b57\u7b26\u4e32\u5143\u7d20\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u5305\u542b\u5b57\u7b26\u4e32\u7684\u5217\u8868<code>string_list = [&#39;1&#39;, &#39;2&#39;, &#39;3&#39;]<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u5217\u8868\uff1a  <\/p>\n<pre><code class=\"language-python\">int_list = [int(x) for x in string_list]\n# \u6216\u8005\nint_list = list(map(int, string_list))\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6574\u6570\u7684\u5217\u8868<code>[1, 2, 3]<\/code>\u3002<\/p>\n<p><strong>\u5982\u679c\u5217\u8868\u4e2d\u5305\u542b\u6df7\u5408\u6570\u636e\u7c7b\u578b\uff0c\u8be5\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u5f53\u5217\u8868\u4e2d\u5305\u542b\u6df7\u5408\u6570\u636e\u7c7b\u578b\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u6765\u8fc7\u6ee4\u548c\u8f6c\u6362\u5143\u7d20\u3002\u53ef\u4ee5\u4f7f\u7528<code>isinstance()<\/code>\u51fd\u6570\u68c0\u67e5\u6bcf\u4e2a\u5143\u7d20\u7684\u7c7b\u578b\uff0c\u5e76\u53ea\u8f6c\u6362\u53ef\u4ee5\u8f6c\u6362\u7684\u5143\u7d20\uff0c\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">mixed_list = [&#39;1&#39;, &#39;two&#39;, 3, &#39;4&#39;]\nint_list = [int(x) for x in mixed_list if isinstance(x, str) and x.isdigit()]\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u53ea\u6709\u7b26\u5408\u6761\u4ef6\u7684\u5b57\u7b26\u4e32\u88ab\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u907f\u514d\u8f6c\u6362\u5931\u8d25\u7684\u60c5\u51b5\u3002<\/p>\n<p><strong>\u8f6c\u6362\u540e\u7684\u6574\u6570\u5217\u8868\u5982\u4f55\u8fdb\u884c\u8fdb\u4e00\u6b65\u64cd\u4f5c\uff1f<\/strong><br \/>\u8f6c\u6362\u540e\u7684\u6574\u6570\u5217\u8868\u53ef\u4ee5\u8fdb\u884c\u591a\u79cd\u64cd\u4f5c\uff0c\u4f8b\u5982\u6c42\u548c\u3001\u6392\u5e8f\u6216\u8ba1\u7b97\u5e73\u5747\u503c\u3002\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u5982<code>sum()<\/code>\u53ef\u4ee5\u5feb\u901f\u8ba1\u7b97\u603b\u548c\uff0c<code>sorted()<\/code>\u53ef\u4ee5\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff0c<code>mean()<\/code>\u51fd\u6570\uff08\u9700\u8981\u5bfc\u5165<code>statistics<\/code>\u6a21\u5757\uff09\u53ef\u4ee5\u8ba1\u7b97\u5e73\u5747\u503c\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">total = sum(int_list)  \nsorted_list = sorted(int_list)  \nfrom statistics import mean  \naverage = mean(int_list)\n<\/code><\/pre>\n<p>\u8fd9\u4e9b\u64cd\u4f5c\u4f7f\u5f97\u8f6c\u6362\u540e\u7684\u6574\u6570\u5217\u8868\u5728\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u65f6\u66f4\u52a0\u7075\u6d3b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5c06\u5217\u8868\u8f6c\u5316\u4e3a\u6574\u6570\u7684\u5e38\u89c1\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5faa\u73af\u904d\u5386\u5e76\u62fc\u63a5\u5b57\u7b26\u4e32\u7136\u540e\u8f6c\u6362\u6210\u6574\u6570\u3001\u4f7f\u7528map\u51fd\u6570\u7ed3\u5408jo [&hellip;]","protected":false},"author":3,"featured_media":1070422,"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\/1070418"}],"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=1070418"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1070418\/revisions"}],"predecessor-version":[{"id":1070424,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1070418\/revisions\/1070424"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1070422"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1070418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1070418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1070418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}