{"id":1089707,"date":"2025-01-08T13:55:52","date_gmt":"2025-01-08T05:55:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1089707.html"},"modified":"2025-01-08T13:55:55","modified_gmt":"2025-01-08T05:55:55","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e7%bb%9f%e8%ae%a10%e7%9a%84%e4%b8%aa%e6%95%b0%e5%ad%97-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1089707.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u7edf\u8ba10\u7684\u4e2a\u6570\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24202501\/3a3ded99-8649-4b8b-a2e3-9c82630eae79.webp\" alt=\"python\u4e2d\u5982\u4f55\u7edf\u8ba10\u7684\u4e2a\u6570\u5b57\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u7edf\u8ba10\u7684\u4e2a\u6570\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528\u5faa\u73af\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u5185\u7f6e\u51fd\u6570\u4ee5\u53ca\u5e93\u51fd\u6570\u7b49\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u7edf\u8ba1\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u5faa\u73af\u7edf\u8ba1<\/strong>\uff1a\u901a\u8fc7\u904d\u5386\u5217\u8868\u6216\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u68c0\u67e5\u662f\u5426\u4e3a0\uff0c\u5982\u679c\u662f\u5219\u8ba1\u6570\u5668\u52a0\u4e00\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7edf\u8ba1<\/strong>\uff1a\u5229\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u4e00\u4e2a\u65b0\u5217\u8868\uff0c\u5305\u542b\u6240\u6709\u4e3a0\u7684\u5143\u7d20\uff0c\u7136\u540e\u8ba1\u7b97\u8be5\u65b0\u5217\u8868\u7684\u957f\u5ea6\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u7edf\u8ba1<\/strong>\uff1a\u5982\u4f7f\u7528<code>count()<\/code>\u65b9\u6cd5\u6765\u7edf\u8ba1\u5217\u8868\u4e2d0\u7684\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528NumPy\u5e93\u51fd\u6570\u7edf\u8ba1<\/strong>\uff1aNumPy\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u7edf\u8ba1\u6570\u7ec4\u4e2d0\u7684\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u5c55\u5f00\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u7edf\u8ba1<\/strong>\uff1a\u8fd9\u79cd\u65b9\u6cd5\u6700\u4e3a\u7b80\u5355\u548c\u76f4\u89c2\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5217\u8868<code>nums<\/code>\uff0c\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u5217\u8868\u7684<code>count()<\/code>\u65b9\u6cd5\u6765\u7edf\u8ba10\u7684\u4e2a\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nums = [1, 0, 3, 0, 5, 0, 7, 0]<\/p>\n<p>zero_count = nums.count(0)<\/p>\n<p>print(f&quot;Number of zeros in the list: {zero_count}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>nums.count(0)<\/code>\u4f1a\u8fd4\u56de\u5217\u8868<code>nums<\/code>\u4e2d0\u7684\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u9ad8\u7ea7\u7528\u6cd5\u548c\u6027\u80fd\u4f18\u5316\u7684\u6280\u5de7\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u5faa\u73af\u7edf\u8ba1<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u5faa\u73af\u904d\u5386\u5217\u8868\u6216\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u68c0\u67e5\u662f\u5426\u4e3a0\uff0c\u5982\u679c\u662f\u5219\u8ba1\u6570\u5668\u52a0\u4e00\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5404\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5e76\u4e14\u6613\u4e8e\u7406\u89e3\u548c\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def count_zeros_via_loop(arr):<\/p>\n<p>    count = 0<\/p>\n<p>    for num in arr:<\/p>\n<p>        if num == 0:<\/p>\n<p>            count += 1<\/p>\n<p>    return count<\/p>\n<p>nums = [1, 0, 3, 0, 5, 0, 7, 0]<\/p>\n<p>zero_count = count_zeros_via_loop(nums)<\/p>\n<p>print(f&quot;Number of zeros in the list: {zero_count}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u89e3\u91ca<\/h3>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>count_zeros_via_loop<\/code>\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u5217\u8868<code>arr<\/code>\uff0c\u901a\u8fc7\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5224\u65ad\u662f\u5426\u4e3a0\uff0c\u5982\u679c\u662f\u5219\u5c06\u8ba1\u6570\u5668<code>count<\/code>\u52a01\uff0c\u6700\u540e\u8fd4\u56de\u8ba1\u6570\u5668\u7684\u503c\u3002<\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n)\uff0c\u5176\u4e2dn\u662f\u5217\u8868\u7684\u957f\u5ea6\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u6613\u4e8e\u7406\u89e3\uff0c\u4f46\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\u53ef\u80fd\u6548\u7387\u4e0d\u9ad8\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7edf\u8ba1<\/h2>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u751f\u6210\u5217\u8868\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u4e00\u4e2a\u5305\u542b\u6240\u67090\u7684\u5143\u7d20\u7684\u65b0\u5217\u8868\uff0c\u7136\u540e\u8ba1\u7b97\u65b0\u5217\u8868\u7684\u957f\u5ea6\u6765\u7edf\u8ba10\u7684\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def count_zeros_via_list_comprehension(arr):<\/p>\n<p>    return len([num for num in arr if num == 0])<\/p>\n<p>nums = [1, 0, 3, 0, 5, 0, 7, 0]<\/p>\n<p>zero_count = count_zeros_via_list_comprehension(nums)<\/p>\n<p>print(f&quot;Number of zeros in the list: {zero_count}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u89e3\u91ca<\/h3>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>count_zeros_via_list_comprehension<\/code>\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u5217\u8868<code>arr<\/code>\uff0c\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u4e00\u4e2a\u65b0\u5217\u8868\uff0c\u5305\u542b\u6240\u6709\u4e3a0\u7684\u5143\u7d20\uff0c\u7136\u540e\u8ba1\u7b97\u65b0\u5217\u8868\u7684\u957f\u5ea6\u3002<\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u662fO(n)\uff0c\u4f46\u7531\u4e8e\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u5217\u8868\uff0c\u56e0\u6b64\u5728\u5185\u5b58\u4f7f\u7528\u4e0a\u53ef\u80fd\u4e0d\u5982\u76f4\u63a5\u4f7f\u7528\u5faa\u73af\u7684\u65b9\u6cd5\u9ad8\u6548\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e09\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u7edf\u8ba1<\/h2>\n<\/p>\n<p><p>Python\u7684\u5185\u7f6e\u51fd\u6570\u548c\u65b9\u6cd5\u901a\u5e38\u662f\u6700\u4e3a\u9ad8\u6548\u548c\u7b80\u6d01\u7684\u89e3\u51b3\u65b9\u6848\u3002\u5217\u8868\u7684<code>count()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u76f4\u63a5\u7edf\u8ba1\u67d0\u4e2a\u5143\u7d20\u5728\u5217\u8868\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">nums = [1, 0, 3, 0, 5, 0, 7, 0]<\/p>\n<p>zero_count = nums.count(0)<\/p>\n<p>print(f&quot;Number of zeros in the list: {zero_count}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u89e3\u91ca<\/h3>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u76f4\u63a5\u8c03\u7528\u5217\u8868<code>nums<\/code>\u7684<code>count()<\/code>\u65b9\u6cd5\uff0c\u7edf\u8ba10\u7684\u4e2a\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7b80\u6d01\uff0c\u4e14\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n)\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u56db\u3001\u4f7f\u7528NumPy\u5e93\u51fd\u6570\u7edf\u8ba1<\/h2>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u51fd\u6570\u3002\u4f7f\u7528NumPy\u7684<code>sum()<\/code>\u51fd\u6570\u53ef\u4ee5\u65b9\u4fbf\u5730\u7edf\u8ba1\u6570\u7ec4\u4e2d0\u7684\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>nums = np.array([1, 0, 3, 0, 5, 0, 7, 0])<\/p>\n<p>zero_count = np.sum(nums == 0)<\/p>\n<p>print(f&quot;Number of zeros in the array: {zero_count}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u89e3\u91ca<\/h3>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u5217\u8868<code>nums<\/code>\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528<code>np.sum(nums == 0)<\/code>\u6765\u7edf\u8ba1\u6570\u7ec4\u4e2d0\u7684\u4e2a\u6570\u3002\u8868\u8fbe\u5f0f<code>nums == 0<\/code>\u4f1a\u751f\u6210\u4e00\u4e2a\u5e03\u5c14\u6570\u7ec4\uff0c\u5176\u4e2d0\u7684\u4f4d\u7f6e\u4e3a<code>True<\/code>\uff0c\u5176\u4ed6\u4f4d\u7f6e\u4e3a<code>False<\/code>\u3002<code>np.sum()<\/code>\u51fd\u6570\u4f1a\u5c06\u8fd9\u4e9b\u5e03\u5c14\u503c\u8f6c\u6362\u4e3a1\u548c0\uff0c\u5e76\u8fdb\u884c\u6c42\u548c\uff0c\u4ece\u800c\u5f97\u52300\u7684\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u975e\u5e38\u9ad8\u6548\uff0c\u56e0\u4e3aNumPy\u5e95\u5c42\u4f7f\u7528\u4e86C\u8bed\u8a00\u5b9e\u73b0\uff0c\u5177\u6709\u8f83\u9ad8\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u6027\u80fd\u4f18\u5316\u6280\u5de7<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6027\u80fd\u4f18\u5316\u7684\u6280\u5de7\uff1a<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u5728\u8ba1\u7b97\u65f6\u4e0d\u4f1a\u521b\u5efa\u5b8c\u6574\u7684\u5217\u8868\uff0c\u4ece\u800c\u8282\u7701\u5185\u5b58\u3002\u53ef\u4ee5\u7ed3\u5408<code>sum()<\/code>\u51fd\u6570\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u6765\u7edf\u8ba10\u7684\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def count_zeros_via_generator(arr):<\/p>\n<p>    return sum(1 for num in arr if num == 0)<\/p>\n<p>nums = [1, 0, 3, 0, 5, 0, 7, 0]<\/p>\n<p>zero_count = count_zeros_via_generator(nums)<\/p>\n<p>print(f&quot;Number of zeros in the list: {zero_count}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u89e3\u91ca<\/h3>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>count_zeros_via_generator<\/code>\uff0c\u901a\u8fc7\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u751f\u6210\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u8ba1\u7b97\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u4e3a0\uff0c\u7136\u540e\u4f7f\u7528<code>sum()<\/code>\u51fd\u6570\u8fdb\u884c\u6c42\u548c\u3002<\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u662fO(n)\uff0c\u4f46\u7531\u4e8e\u4e0d\u521b\u5efa\u4e2d\u95f4\u5217\u8868\uff0c\u56e0\u6b64\u5728\u5185\u5b58\u4f7f\u7528\u4e0a\u66f4\u4e3a\u9ad8\u6548\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u7279\u522b\u5927\u7684\u6570\u636e\u96c6\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u6765\u5e76\u884c\u8ba1\u7b97\u3002Python\u7684<code>concurrent.futures<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u5e76\u884c\u8ba1\u7b97\u63a5\u53e3\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import concurrent.futures<\/p>\n<p>def count_zeros_in_chunk(chunk):<\/p>\n<p>    return chunk.count(0)<\/p>\n<p>def count_zeros_via_multithreading(arr, num_threads=4):<\/p>\n<p>    chunk_size = len(arr) \/\/ num_threads<\/p>\n<p>    chunks = [arr[i * chunk_size:(i + 1) * chunk_size] for i in range(num_threads)]<\/p>\n<p>    with concurrent.futures.ThreadPoolExecutor() as executor:<\/p>\n<p>        results = executor.map(count_zeros_in_chunk, chunks)<\/p>\n<p>    return sum(results)<\/p>\n<p>nums = [1, 0, 3, 0, 5, 0, 7, 0] * 1000000  # Large dataset<\/p>\n<p>zero_count = count_zeros_via_multithreading(nums)<\/p>\n<p>print(f&quot;Number of zeros in the list: {zero_count}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u89e3\u91ca<\/h3>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>count_zeros_via_multithreading<\/code>\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u5217\u8868<code>arr<\/code>\u548c\u7ebf\u7a0b\u6570<code>num_threads<\/code>\u3002\u6211\u4eec\u5c06\u5217\u8868\u5206\u6210\u591a\u4e2a\u5757\uff0c\u6bcf\u4e2a\u5757\u7531\u4e00\u4e2a\u7ebf\u7a0b\u5904\u7406\uff0c\u6700\u540e\u6c47\u603b\u5404\u4e2a\u7ebf\u7a0b\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5904\u7406\u7279\u522b\u5927\u7684\u6570\u636e\u96c6\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\u7edf\u8ba10\u7684\u4e2a\u6570\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5faa\u73af\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u5185\u7f6e\u51fd\u6570\u4ee5\u53caNumPy\u5e93\u51fd\u6570\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f\u3002\u5bf9\u4e8e\u4e00\u822c\u60c5\u51b5\uff0c\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>count()<\/code>\u65b9\u6cd5\u6700\u4e3a\u7b80\u6d01\u9ad8\u6548\uff1b\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528NumPy\u5e93\u6216\u5e76\u884c\u8ba1\u7b97\u6765\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u4e0d\u4ec5\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u8fd8\u80fd\u663e\u8457\u63d0\u5347\u7a0b\u5e8f\u7684\u6027\u80fd\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u6570\u636e\u89c4\u6a21\u548c\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u4f18\u7684\u65b9\u6cd5\uff0c\u4ee5\u8fbe\u5230\u6700\u4f73\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u7edf\u8ba1\u5217\u8868\u4e2d0\u7684\u4e2a\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>list.count()<\/code>\u65b9\u6cd5\u8f7b\u677e\u7edf\u8ba1\u5217\u8868\u4e2d0\u7684\u4e2a\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5217\u8868<code>my_list = [0, 1, 0, 2, 0]<\/code>\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>my_list.count(0)<\/code>\u6765\u83b7\u53d60\u7684\u6570\u91cf\uff0c\u8fd9\u5c06\u8fd4\u56de3\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u5b57\u7b26\u4e32\u4e2d\u7edf\u8ba10\u7684\u4e2a\u6570\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u5728\u5b57\u7b26\u4e32\u4e2d\u7edf\u8ba1\u5b57\u7b26&#39;0&#39;\u7684\u6570\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>count()<\/code>\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u7ed9\u5b9a\u5b57\u7b26\u4e32<code>my_string = &quot;100200300&quot;<\/code>\uff0c\u901a\u8fc7<code>my_string.count(&#39;0&#39;)<\/code>\u53ef\u4ee5\u5f97\u51fa\u7ed3\u679c\u4e3a3\uff0c\u8fd9\u6837\u4fbf\u80fd\u4e86\u89e3\u5b57\u7b26\u4e32\u4e2d0\u7684\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<p><strong>\u5728\u591a\u7ef4\u6570\u7ec4\u4e2d\u5982\u4f55\u7edf\u8ba10\u7684\u4e2a\u6570\uff1f<\/strong><br \/>\u5bf9\u4e8e\u591a\u7ef4\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u7edf\u8ba10\u7684\u4e2a\u6570\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u5b89\u88c5NumPy\u5e93\uff0c\u7136\u540e\u53ef\u4ee5\u901a\u8fc7<code>np.count_nonzero(your_array == 0)<\/code>\u6765\u8ba1\u7b97\u6570\u7ec4\u4e2d0\u7684\u603b\u6570\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2aNumPy\u6570\u7ec4<code>arr = np.array([[0, 1], [2, 0], [0, 0]])<\/code>\uff0c\u6267\u884c<code>np.count_nonzero(arr == 0)<\/code>\u4f1a\u8fd4\u56de5\uff0c\u8868\u793a\u8be5\u6570\u7ec4\u4e2d0\u7684\u6570\u91cf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u7edf\u8ba10\u7684\u4e2a\u6570\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528\u5faa\u73af\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u5185\u7f6e\u51fd\u6570\u4ee5\u53ca\u5e93\u51fd\u6570\u7b49\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f [&hellip;]","protected":false},"author":3,"featured_media":1089715,"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\/1089707"}],"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=1089707"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1089707\/revisions"}],"predecessor-version":[{"id":1089716,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1089707\/revisions\/1089716"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1089715"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1089707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1089707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1089707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}