{"id":1104115,"date":"2025-01-08T16:17:26","date_gmt":"2025-01-08T08:17:26","guid":{"rendered":""},"modified":"2025-01-08T16:17:32","modified_gmt":"2025-01-08T08:17:32","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e7%9f%a9%e9%98%b5%e8%bd%ac%e6%8d%a2%e6%88%90%e5%88%97%e8%a1%a8-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1104115.html","title":{"rendered":"python\u5982\u4f55\u5c06\u77e9\u9635\u8f6c\u6362\u6210\u5217\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065624\/c9dabe5b-f261-4171-914a-de8c84226d13.webp\" alt=\"python\u5982\u4f55\u5c06\u77e9\u9635\u8f6c\u6362\u6210\u5217\u8868\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5c06\u77e9\u9635\u8f6c\u6362\u6210\u5217\u8868\uff0c<strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u3001\u4f7f\u7528 NumPy flatten \u65b9\u6cd5\u3001\u4f7f\u7528 NumPy tolist \u65b9\u6cd5<\/strong>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff0c\u5e76\u8be6\u7ec6\u63a2\u8ba8\u5176\u4ed6\u65b9\u6cd5\u7684\u4f7f\u7528\u53ca\u5176\u4f18\u52bf\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u662f\u4e00\u79cd\u7b80\u5355\u800c\u6709\u6548\u7684\u65b9\u6cd5\uff0c\u5c06\u77e9\u9635\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u5217\u8868\u63a8\u5bfc\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u6613\u8bfb\uff0c\u5e76\u4e14\u5728\u5904\u7406\u5c0f\u578b\u77e9\u9635\u65f6\u6027\u80fd\u4f18\u8d8a\u3002<\/p>\n<\/p>\n<p><p><strong>\u5217\u8868\u63a8\u5bfc\u6cd5\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p>flattened_list = [element for row in matrix for element in row]<\/p>\n<p>print(flattened_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5217\u8868\u63a8\u5bfc\u901a\u8fc7\u5d4c\u5957\u5faa\u73af\u904d\u5386\u77e9\u9635\u7684\u6bcf\u4e00\u884c\u548c\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u6700\u7ec8\u751f\u6210\u4e00\u4e2a\u4e00\u7ef4\u5217\u8868\u3002\u7531\u4e8e\u5217\u8868\u63a8\u5bfc\u5728Python\u7684\u89e3\u91ca\u5668\u4e2d\u8fdb\u884c\u4e86\u4f18\u5316\uff0c\u56e0\u6b64\u5728\u5904\u7406\u5c0f\u578b\u77e9\u9635\u65f6\uff0c\u5217\u8868\u63a8\u5bfc\u6cd5\u901a\u5e38\u80fd\u591f\u63d0\u4f9b\u826f\u597d\u7684\u6027\u80fd\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 NumPy flatten \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u5e93\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u5904\u7406\u77e9\u9635\u548c\u6570\u7ec4\u7684\u5f3a\u5927\u529f\u80fd\u3002\u4f7f\u7528NumPy\u7684<code>flatten<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u77e9\u9635\u8f6c\u6362\u4e3a\u4e00\u7ef4\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<p>flattened_list = matrix.flatten()<\/p>\n<p>print(flattened_list.tolist())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u5c06\u77e9\u9635\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528<code>flatten<\/code>\u65b9\u6cd5\u5c06\u5176\u5c55\u5e73\u4e3a\u4e00\u7ef4\u6570\u7ec4\u3002\u6700\u540e\u4f7f\u7528<code>tolist<\/code>\u65b9\u6cd5\u5c06NumPy\u6570\u7ec4\u8f6c\u6362\u4e3aPython\u5217\u8868\u3002<strong>NumPy\u5728\u5904\u7406\u5927\u578b\u77e9\u9635\u65f6\uff0c\u6027\u80fd\u548c\u6548\u7387\u4f18\u8d8a<\/strong>\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u8fdb\u884c\u5927\u91cf\u77e9\u9635\u8fd0\u7b97\u7684\u573a\u666f\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 NumPy tolist \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>tolist<\/code>\u65b9\u6cd5\u76f4\u63a5\u5c06NumPy\u6570\u7ec4\u8f6c\u6362\u4e3aPython\u5217\u8868\uff0c\u9002\u7528\u4e8e\u4e0d\u9700\u8981\u8fdb\u884c\u989d\u5916\u64cd\u4f5c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<p>flattened_list = matrix.tolist()<\/p>\n<p>print(flattened_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528<code>tolist<\/code>\u65b9\u6cd5\u5c06NumPy\u6570\u7ec4\u8f6c\u6362\u4e3a\u5d4c\u5957\u5217\u8868\u3002\u867d\u7136\u6b64\u65b9\u6cd5\u4e0d\u4f1a\u5c55\u5e73\u77e9\u9635\uff0c\u4f46\u5bf9\u4e8e\u67d0\u4e9b\u7279\u5b9a\u5e94\u7528\u573a\u666f\uff08\u4f8b\u5982\uff0c\u9700\u8981\u4fdd\u7559\u77e9\u9635\u7684\u5d4c\u5957\u7ed3\u6784\uff09\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528 itertools.ch<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>itertools<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u7ec4\u7528\u4e8e\u9ad8\u6548\u64cd\u4f5c\u8fed\u4ee3\u5668\u7684\u5de5\u5177\u3002\u4f7f\u7528<code>itertools.chain<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u77e9\u9635\u5c55\u5e73\u4e3a\u4e00\u7ef4\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<p>matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p>flattened_list = list(itertools.chain.from_iterable(matrix))<\/p>\n<p>print(flattened_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>itertools.chain.from_iterable<\/code>\u65b9\u6cd5\u5c06\u5d4c\u5957\u5217\u8868\u5c55\u5e73\u4e3a\u4e00\u7ef4\u5217\u8868\u3002<strong>\u6b64\u65b9\u6cd5\u5728\u5904\u7406\u5927\u578b\u77e9\u9635\u65f6\u975e\u5e38\u9ad8\u6548\uff0c\u5e76\u4e14\u5728\u5185\u5b58\u4f7f\u7528\u65b9\u9762\u8868\u73b0\u4f18\u5f02<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u624b\u52a8\u904d\u5386\u77e9\u9635<\/h3>\n<\/p>\n<p><p>\u624b\u52a8\u904d\u5386\u77e9\u9635\u662f\u4e00\u79cd\u6700\u57fa\u7840\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5bf9\u77e9\u9635\u7ed3\u6784\u548c\u904d\u5386\u65b9\u5f0f\u6709\u7279\u522b\u9700\u6c42\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p>flattened_list = []<\/p>\n<p>for row in matrix:<\/p>\n<p>    for element in row:<\/p>\n<p>        flattened_list.append(element)<\/p>\n<p>print(flattened_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u5d4c\u5957\u5faa\u73af\u624b\u52a8\u904d\u5386\u77e9\u9635\uff0c\u5e76\u5c06\u6bcf\u4e2a\u5143\u7d20\u6dfb\u52a0\u5230\u4e00\u7ef4\u5217\u8868\u4e2d\u3002\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u4ee3\u7801\u8f83\u591a\uff0c\u4f46\u5bf9\u4e8e\u7406\u89e3\u77e9\u9635\u904d\u5386\u8fc7\u7a0b\u548c\u5b9a\u5236\u5316\u9700\u6c42\u975e\u5e38\u6709\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528 functools.reduce \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>functools<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u51fd\u6570\u5f0f\u7f16\u7a0b\u5de5\u5177\uff0c\u4f7f\u7528<code>reduce<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u77e9\u9635\u5c55\u5e73\u4e3a\u4e00\u7ef4\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import reduce<\/p>\n<p>matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p>flattened_list = reduce(lambda x, y: x + y, matrix)<\/p>\n<p>print(flattened_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>reduce<\/code>\u65b9\u6cd5\u901a\u8fc7\u7d2f\u79ef\u64cd\u4f5c\u5c06\u5d4c\u5957\u5217\u8868\u5c55\u5e73\u4e3a\u4e00\u7ef4\u5217\u8868\u3002<strong>\u6b64\u65b9\u6cd5\u5728\u5904\u7406\u8f83\u5927\u77e9\u9635\u65f6\u6027\u80fd\u826f\u597d\uff0c\u5e76\u4e14\u4ee3\u7801\u7b80\u6d01<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528 pandas \u5e93<\/h3>\n<\/p>\n<p><p><code>pandas<\/code>\u662fPython\u4e2d\u7528\u4e8e\u6570\u636e\u5206\u6790\u7684\u5e93\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u5904\u7406\u77e9\u9635\u548c\u6570\u636e\u6846\u7684\u5f3a\u5927\u529f\u80fd\u3002\u4f7f\u7528<code>pandas<\/code>\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u77e9\u9635\u8f6c\u6362\u4e3a\u4e00\u7ef4\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p>df = pd.DataFrame(matrix)<\/p>\n<p>flattened_list = df.values.flatten().tolist()<\/p>\n<p>print(flattened_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u5c06\u77e9\u9635\u8f6c\u6362\u4e3a<code>pandas<\/code>\u6570\u636e\u6846\uff0c\u7136\u540e\u4f7f\u7528<code>values.flatten<\/code>\u65b9\u6cd5\u5c06\u5176\u5c55\u5e73\u4e3a\u4e00\u7ef4\u6570\u7ec4\uff0c\u6700\u540e\u4f7f\u7528<code>tolist<\/code>\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3aPython\u5217\u8868\u3002<strong>\u6b64\u65b9\u6cd5\u5728\u5904\u7406\u6570\u636e\u5206\u6790\u4efb\u52a1\u65f6\u975e\u5e38\u65b9\u4fbf\uff0c\u7279\u522b\u662f\u9700\u8981\u4e0e\u5176\u4ed6<code>pandas<\/code>\u529f\u80fd\u7ed3\u5408\u4f7f\u7528\u65f6<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u7efc\u5408\u6bd4\u8f83\u4e0e\u63a8\u8350<\/h3>\n<\/p>\n<p><p>\u5728\u5c06\u77e9\u9635\u8f6c\u6362\u4e3a\u5217\u8868\u7684\u8fc7\u7a0b\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u5c0f\u578b\u77e9\u9635<\/strong>\uff1a\u5217\u8868\u63a8\u5bfc\u6cd5\u548c\u624b\u52a8\u904d\u5386\u65b9\u6cd5\u4ee3\u7801\u7b80\u6d01\uff0c\u6613\u4e8e\u7406\u89e3\u548c\u5b9e\u73b0\u3002<\/li>\n<li><strong>\u5927\u578b\u77e9\u9635<\/strong>\uff1aNumPy\u5e93\u63d0\u4f9b\u7684<code>flatten<\/code>\u548c<code>tolist<\/code>\u65b9\u6cd5\u5728\u6027\u80fd\u548c\u6548\u7387\u65b9\u9762\u8868\u73b0\u51fa\u8272\uff0c\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u5927\u91cf\u77e9\u9635\u8fd0\u7b97\u7684\u573a\u666f\u3002<\/li>\n<li><strong>\u9700\u8981\u4fdd\u7559\u5d4c\u5957\u7ed3\u6784<\/strong>\uff1aNumPy\u7684<code>tolist<\/code>\u65b9\u6cd5\u80fd\u591f\u4fdd\u7559\u77e9\u9635\u7684\u5d4c\u5957\u7ed3\u6784\uff0c\u9002\u7528\u4e8e\u7279\u5b9a\u9700\u6c42\u3002<\/li>\n<li><strong>\u9ad8\u6548\u5185\u5b58\u4f7f\u7528<\/strong>\uff1a<code>itertools.chain<\/code>\u65b9\u6cd5\u5728\u5904\u7406\u5927\u578b\u77e9\u9635\u65f6\u5185\u5b58\u4f7f\u7528\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u5185\u5b58\u654f\u611f\u7684\u573a\u666f\u3002<\/li>\n<li><strong>\u51fd\u6570\u5f0f\u7f16\u7a0b<\/strong>\uff1a<code>functools.reduce<\/code>\u65b9\u6cd5\u5728\u4ee3\u7801\u7b80\u6d01\u6027\u548c\u6027\u80fd\u65b9\u9762\u8868\u73b0\u4f18\u5f02\uff0c\u9002\u7528\u4e8e\u51fd\u6570\u5f0f\u7f16\u7a0b\u98ce\u683c\u7684\u573a\u666f\u3002<\/li>\n<li><strong>\u6570\u636e\u5206\u6790<\/strong>\uff1a<code>pandas<\/code>\u5e93\u5728\u5904\u7406\u6570\u636e\u5206\u6790\u4efb\u52a1\u65f6\u975e\u5e38\u65b9\u4fbf\uff0c\u7279\u522b\u662f\u9700\u8981\u4e0e\u5176\u4ed6<code>pandas<\/code>\u529f\u80fd\u7ed3\u5408\u4f7f\u7528\u65f6\u3002<\/li>\n<\/ul>\n<p><h3>\u516b\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f\u4e0e\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5c06\u77e9\u9635\u8f6c\u6362\u4e3a\u5217\u8868\u7684\u9700\u6c42\u53ef\u80fd\u51fa\u73b0\u5728\u4ee5\u4e0b\u51e0\u79cd\u573a\u666f\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u6570\u636e\u9884\u5904\u7406<\/strong>\uff1a\u5728<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u548c\u6570\u636e\u5206\u6790\u4efb\u52a1\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\uff0c\u5c06\u77e9\u9635\u8f6c\u6362\u4e3a\u4e00\u7ef4\u5217\u8868\u4fbf\u4e8e\u7279\u5f81\u63d0\u53d6\u548c\u5206\u6790\u3002<\/li>\n<li><strong>\u56fe\u50cf\u5904\u7406<\/strong>\uff1a\u5728\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u4e2d\uff0c\u56fe\u50cf\u901a\u5e38\u8868\u793a\u4e3a\u77e9\u9635\u5f62\u5f0f\uff0c\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u7ef4\u5217\u8868\u4fbf\u4e8e\u8fdb\u884c\u50cf\u7d20\u7ea7\u64cd\u4f5c\u548c\u5206\u6790\u3002<\/li>\n<li><strong>\u7edf\u8ba1\u5206\u6790<\/strong>\uff1a\u5728\u7edf\u8ba1\u5206\u6790\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u77e9\u9635\u6570\u636e\u8fdb\u884c\u5c55\u5e73\u64cd\u4f5c\uff0c\u4ee5\u4fbf\u8ba1\u7b97\u7edf\u8ba1\u91cf\u6216\u8fdb\u884c\u53ef\u89c6\u5316\u5206\u6790\u3002<\/li>\n<li><strong>\u6570\u636e\u5b58\u50a8<\/strong>\uff1a\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5c06\u77e9\u9635\u8f6c\u6362\u4e3a\u4e00\u7ef4\u5217\u8868\u4fbf\u4e8e\u5c06\u6570\u636e\u5b58\u50a8\u5230\u6570\u636e\u5e93\u6216\u6587\u4ef6\u4e2d\u3002<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b9e\u9645\u5e94\u7528\u573a\u666f\u7684\u793a\u4f8b\uff1a\u5c06\u56fe\u50cf\u77e9\u9635\u8f6c\u6362\u4e3a\u4e00\u7ef4\u5217\u8868\uff0c\u4ee5\u4fbf\u8fdb\u884c\u50cf\u7d20\u7ea7\u64cd\u4f5c\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u56fe\u50cf\u77e9\u9635<\/strong><\/h2>\n<p>image_matrix = np.random.randint(0, 256, (100, 100), dtype=np.uint8)<\/p>\n<h2><strong>\u5c06\u56fe\u50cf\u77e9\u9635\u8f6c\u6362\u4e3a\u4e00\u7ef4\u5217\u8868<\/strong><\/h2>\n<p>flattened_image = image_matrix.flatten().tolist()<\/p>\n<h2><strong>\u5bf9\u4e00\u7ef4\u5217\u8868\u8fdb\u884c\u50cf\u7d20\u7ea7\u64cd\u4f5c\uff08\u4f8b\u5982\uff0c\u589e\u52a0\u4eae\u5ea6\uff09<\/strong><\/h2>\n<p>brightened_image = [min(pixel + 50, 255) for pixel in flattened_image]<\/p>\n<h2><strong>\u5c06\u4e00\u7ef4\u5217\u8868\u8f6c\u6362\u56de\u56fe\u50cf\u77e9\u9635<\/strong><\/h2>\n<p>brightened_image_matrix = np.array(brightened_image, dtype=np.uint8).reshape((100, 100))<\/p>\n<h2><strong>\u663e\u793a\u539f\u59cb\u56fe\u50cf\u548c\u589e\u52a0\u4eae\u5ea6\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>plt.subplot(1, 2, 1)<\/p>\n<p>plt.title(&#39;Original Image&#39;)<\/p>\n<p>plt.imshow(image_matrix, cmap=&#39;gray&#39;)<\/p>\n<p>plt.subplot(1, 2, 2)<\/p>\n<p>plt.title(&#39;Brightened Image&#39;)<\/p>\n<p>plt.imshow(brightened_image_matrix, cmap=&#39;gray&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u56fe\u50cf\u77e9\u9635\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u7ef4\u5217\u8868\uff0c\u5e76\u5bf9\u6bcf\u4e2a\u50cf\u7d20\u8fdb\u884c\u4eae\u5ea6\u589e\u52a0\u64cd\u4f5c\u3002\u6700\u540e\uff0c\u5c06\u4e00\u7ef4\u5217\u8868\u8f6c\u6362\u56de\u56fe\u50cf\u77e9\u9635\u5e76\u663e\u793a\u539f\u59cb\u56fe\u50cf\u548c\u589e\u52a0\u4eae\u5ea6\u540e\u7684\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u5185\u5bb9\uff0c\u53ef\u4ee5\u770b\u5230\u5728Python\u4e2d\u5c06\u77e9\u9635\u8f6c\u6362\u4e3a\u5217\u8868\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u4e0d\u4ec5\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u6027\u80fd\u548c\u53ef\u8bfb\u6027\uff0c\u8fd8\u80fd\u6ee1\u8db3\u4e0d\u540c\u5e94\u7528\u573a\u666f\u7684\u9700\u6c42\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7ed3\u5408\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u4f18\u65b9\u6cd5\uff0c\u80fd\u591f\u6709\u6548\u63d0\u5347\u4efb\u52a1\u7684\u6548\u7387\u548c\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u77e9\u9635\u8f6c\u6362\u4e3a\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5c06\u77e9\u9635\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001NumPy\u5e93\u548c\u539f\u751fPython\u7684\u5d4c\u5957\u5faa\u73af\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u4e8c\u7ef4\u77e9\u9635\uff0c\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u5b9e\u73b0\u8f6c\u6362\uff1a<code>list_of_lists = [item for sublist in matrix for item in sublist]<\/code>\u3002\u5982\u679c\u4f7f\u7528NumPy\u5e93\uff0c\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528<code>matrix.flatten().tolist()<\/code>\u6765\u5f97\u5230\u4e00\u7ef4\u5217\u8868\u3002<\/p>\n<p><strong>\u4f7f\u7528NumPy\u8f6c\u6362\u77e9\u9635\u4e3a\u5217\u8868\u65f6\u6709\u54ea\u4e9b\u6ce8\u610f\u4e8b\u9879\uff1f<\/strong><br \/>\u4f7f\u7528NumPy\u5e93\u8f6c\u6362\u77e9\u9635\u4e3a\u5217\u8868\u65f6\uff0c\u786e\u4fdd\u60a8\u5df2\u7ecf\u5b89\u88c5\u4e86NumPy\u6a21\u5757\uff0c\u5e76\u4e14\u77e9\u9635\u662fNumPy\u6570\u7ec4\u7c7b\u578b\u3002\u8c03\u7528<code>flatten()<\/code>\u65b9\u6cd5\u5c06\u77e9\u9635\u5c55\u5e73\u4e3a\u4e00\u7ef4\u6570\u7ec4\uff0c\u4f7f\u7528<code>tolist()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3aPython\u5217\u8868\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u5904\u7406\u7684\u6570\u636e\u7c7b\u578b\u4e00\u81f4\uff0c\u4ee5\u907f\u514d\u6f5c\u5728\u7684\u7c7b\u578b\u8f6c\u6362\u95ee\u9898\u3002<\/p>\n<p><strong>\u5728\u6ca1\u6709NumPy\u7684\u60c5\u51b5\u4e0b\u5982\u4f55\u5904\u7406\u77e9\u9635\u8f6c\u6362\uff1f<\/strong><br \/>\u5728\u6ca1\u6709NumPy\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5d4c\u5957\u5faa\u73af\u7ed3\u6784\u6765\u5b9e\u73b0\u77e9\u9635\u8f6c\u6362\u3002\u901a\u8fc7\u904d\u5386\u6bcf\u4e00\u884c\u548c\u6bcf\u4e00\u5217\uff0c\u5c06\u5143\u7d20\u6dfb\u52a0\u5230\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u4e2d\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">matrix = [[1, 2, 3], [4, 5, 6]]\nresult = []\nfor row in matrix:\n    for element in row:\n        result.append(element)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u53ef\u4ee5\u5c06\u4e8c\u7ef4\u77e9\u9635\u8f6c\u6362\u4e3a\u4e00\u7ef4\u5217\u8868\uff0c\u786e\u4fdd\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5c06\u77e9\u9635\u8f6c\u6362\u6210\u5217\u8868\uff0c\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u3001\u4f7f\u7528 NumPy flatten \u65b9\u6cd5\u3001\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1104133,"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\/1104115"}],"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=1104115"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1104115\/revisions"}],"predecessor-version":[{"id":1104135,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1104115\/revisions\/1104135"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1104133"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1104115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1104115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1104115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}