{"id":936875,"date":"2024-12-26T19:39:13","date_gmt":"2024-12-26T11:39:13","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/936875.html"},"modified":"2024-12-26T19:39:15","modified_gmt":"2024-12-26T11:39:15","slug":"python%e5%a6%82%e4%bd%95cell%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/936875.html","title":{"rendered":"python\u5982\u4f55cell\u6570\u7ec4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072950\/b06aa29c-3a41-418b-b799-79b393235683.webp\" alt=\"python\u5982\u4f55cell\u6570\u7ec4\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u521b\u5efa\u548c\u64cd\u4f5c\u7c7b\u4f3c\u4e8eMATLAB\u4e2dcell\u6570\u7ec4\u7684\u7ed3\u6784\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001\u5b57\u5178\u6216numpy\u5e93\u4e2d\u7684\u5bf9\u8c61\u6570\u7ec4\u7b49\u65b9\u6cd5\u3002\u5d4c\u5957\u5217\u8868\u3001\u5b57\u5178\u3001numpy\u5bf9\u8c61\u6570\u7ec4\u662fPython\u4e2d\u5b9e\u73b0cell\u6570\u7ec4\u7684\u5e38\u7528\u65b9\u6cd5\uff0c\u5176\u4e2d\u5d4c\u5957\u5217\u8868\u662f\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u3002<\/strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\u548c\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u5b9e\u73b0cell\u6570\u7ec4<\/p>\n<\/p>\n<p><p>Python\u7684\u5d4c\u5957\u5217\u8868\u662f\u4e00\u4e2a\u975e\u5e38\u7075\u6d3b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u4e8e\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u3002\u8fd9\u79cd\u7ed3\u6784\u7c7b\u4f3c\u4e8eMATLAB\u4e2d\u7684cell\u6570\u7ec4\uff0c\u5141\u8bb8\u5728\u6570\u7ec4\u7684\u6bcf\u4e2a\u4f4d\u7f6e\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5d4c\u5957\u5217\u8868\u7684\u521b\u5efa\u4e0e\u64cd\u4f5c<\/li>\n<\/ol>\n<p><p>\u5d4c\u5957\u5217\u8868\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u5d4c\u5957\u65b9\u62ec\u53f7\u6765\u521b\u5efa\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5d4c\u5957\u5217\u8868<\/p>\n<p>cell_array = [[1, 2, 3], [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;], [True, False, None]]<\/p>\n<h2><strong>\u8bbf\u95ee\u5d4c\u5957\u5217\u8868\u4e2d\u7684\u5143\u7d20<\/strong><\/h2>\n<p>print(cell_array[0])  # \u8f93\u51fa\uff1a[1, 2, 3]<\/p>\n<p>print(cell_array[1][1])  # \u8f93\u51fa\uff1a&#39;b&#39;<\/p>\n<h2><strong>\u4fee\u6539\u5d4c\u5957\u5217\u8868\u4e2d\u7684\u5143\u7d20<\/strong><\/h2>\n<p>cell_array[2][1] = True<\/p>\n<p>print(cell_array[2])  # \u8f93\u51fa\uff1a[True, True, None]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5d4c\u5957\u5217\u8868\u7684\u5e94\u7528\u573a\u666f<\/li>\n<\/ol>\n<p><p>\u5d4c\u5957\u5217\u8868\u5e7f\u6cdb\u7528\u4e8e\u9700\u8981\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u6570\u636e\u7684\u573a\u5408\u3002\u6bd4\u5982\uff0c\u5b58\u50a8\u4e00\u7ec4\u4e0d\u540c\u7c7b\u578b\u7684\u5b9e\u9a8c\u6570\u636e\uff0c\u6216\u8005\u5728\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u65f6\uff0c\u5d4c\u5957\u5217\u8868\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u800c\u6709\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5b57\u5178\u5b9e\u73b0cell\u6570\u7ec4<\/p>\n<\/p>\n<p><p>\u5b57\u5178\u662fPython\u4e2d\u7684\u53e6\u4e00\u79cd\u7075\u6d3b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u7c7b\u4f3c\u4e8ecell\u6570\u7ec4\u7684\u529f\u80fd\u3002\u5b57\u5178\u7684\u952e\u53ef\u4ee5\u662f\u4efb\u4f55\u4e0d\u53ef\u53d8\u7c7b\u578b\uff0c\u800c\u503c\u53ef\u4ee5\u662f\u4efb\u610f\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b57\u5178\u7684\u521b\u5efa\u4e0e\u64cd\u4f5c<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528\u5b57\u5178\u5b9e\u73b0cell\u6570\u7ec4\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5b57\u5178<\/p>\n<p>cell_dict = {&#39;first&#39;: [1, 2, 3], &#39;second&#39;: [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;], &#39;third&#39;: [True, False, None]}<\/p>\n<h2><strong>\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u5143\u7d20<\/strong><\/h2>\n<p>print(cell_dict[&#39;first&#39;])  # \u8f93\u51fa\uff1a[1, 2, 3]<\/p>\n<p>print(cell_dict[&#39;second&#39;][1])  # \u8f93\u51fa\uff1a&#39;b&#39;<\/p>\n<h2><strong>\u4fee\u6539\u5b57\u5178\u4e2d\u7684\u5143\u7d20<\/strong><\/h2>\n<p>cell_dict[&#39;third&#39;][1] = True<\/p>\n<p>print(cell_dict[&#39;third&#39;])  # \u8f93\u51fa\uff1a[True, True, None]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5b57\u5178\u7684\u5e94\u7528\u573a\u666f<\/li>\n<\/ol>\n<p><p>\u5b57\u5178\u7279\u522b\u9002\u5408\u7528\u4e8e\u9700\u8981\u901a\u8fc7\u952e\u503c\u5bf9\u6765\u5b58\u50a8\u548c\u67e5\u627e\u6570\u636e\u7684\u573a\u666f\u3002\u4f8b\u5982\uff0c\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6765\u5b58\u50a8\u548c\u7ba1\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528numpy\u5bf9\u8c61\u6570\u7ec4\u5b9e\u73b0cell\u6570\u7ec4<\/p>\n<\/p>\n<p><p>Numpy\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u652f\u6301\u591a\u7ef4\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u3002\u867d\u7136numpy\u4e3b\u8981\u7528\u4e8e\u6570\u503c\u8ba1\u7b97\uff0c\u4f46\u5b83\u4e5f\u652f\u6301\u5bf9\u8c61\u6570\u7ec4\uff0c\u5141\u8bb8\u5728\u6570\u7ec4\u4e2d\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<ol>\n<li>numpy\u5bf9\u8c61\u6570\u7ec4\u7684\u521b\u5efa\u4e0e\u64cd\u4f5c<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528numpy\u7684\u5bf9\u8c61\u6570\u7ec4\u53ef\u4ee5\u5b9e\u73b0\u7c7b\u4f3c\u4e8ecell\u6570\u7ec4\u7684\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2anumpy\u5bf9\u8c61\u6570\u7ec4<\/strong><\/h2>\n<p>cell_array = np.empty((3,), dtype=object)<\/p>\n<p>cell_array[0] = [1, 2, 3]<\/p>\n<p>cell_array[1] = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/p>\n<p>cell_array[2] = [True, False, None]<\/p>\n<h2><strong>\u8bbf\u95eenumpy\u5bf9\u8c61\u6570\u7ec4\u4e2d\u7684\u5143\u7d20<\/strong><\/h2>\n<p>print(cell_array[0])  # \u8f93\u51fa\uff1a[1, 2, 3]<\/p>\n<p>print(cell_array[1][1])  # \u8f93\u51fa\uff1a&#39;b&#39;<\/p>\n<h2><strong>\u4fee\u6539numpy\u5bf9\u8c61\u6570\u7ec4\u4e2d\u7684\u5143\u7d20<\/strong><\/h2>\n<p>cell_array[2][1] = True<\/p>\n<p>print(cell_array[2])  # \u8f93\u51fa\uff1a[True, True, None]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>numpy\u5bf9\u8c61\u6570\u7ec4\u7684\u5e94\u7528\u573a\u666f<\/li>\n<\/ol>\n<p><p>numpy\u7684\u5bf9\u8c61\u6570\u7ec4\u5728\u9700\u8981\u540c\u65f6\u5229\u7528numpy\u7684\u5f3a\u5927\u8ba1\u7b97\u529f\u80fd\u548c\u5b58\u50a8\u591a\u79cd\u7c7b\u578b\u6570\u636e\u7684\u573a\u5408\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5728\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u4e2d\uff0c\u53ef\u4ee5\u5229\u7528numpy\u5bf9\u8c61\u6570\u7ec4\u5b58\u50a8\u548c\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u867d\u7136\u6ca1\u6709\u76f4\u63a5\u5bf9\u5e94\u4e8eMATLAB\u4e2d\u7684cell\u6570\u7ec4\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001\u5b57\u5178\u548cnumpy\u5bf9\u8c61\u6570\u7ec4\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\u3002<strong>\u5d4c\u5957\u5217\u8868\u662f\u6700\u7b80\u5355\u548c\u6700\u5e38\u7528\u7684\u65b9\u6cd5<\/strong>\uff0c\u9002\u5408\u4e00\u822c\u7528\u9014\u7684\u6570\u636e\u5b58\u50a8\u548c\u64cd\u4f5c\u3002\u5b57\u5178\u9002\u5408\u9700\u8981\u952e\u503c\u5bf9\u5b58\u50a8\u7684\u573a\u666f\uff0c\u800cnumpy\u5bf9\u8c61\u6570\u7ec4\u9002\u5408\u9700\u8981\u7ed3\u5408\u6570\u503c\u8ba1\u7b97\u7684\u590d\u6742\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u5b9e\u73b0\u65b9\u5f0f\uff0c\u90fd\u9700\u8981\u6839\u636e\u5177\u4f53\u5e94\u7528\u573a\u666f\u548c\u6570\u636e\u7279\u70b9\u6765\u9009\u62e9\u6700\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4ee5\u4fbf\u6709\u6548\u5730\u5b58\u50a8\u548c\u64cd\u4f5c\u6570\u636e\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u5145\u5206\u53d1\u6325Python\u5728\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u4e2d\u7684\u5f3a\u5927\u80fd\u529b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u548c\u4f7f\u7528cell\u6570\u7ec4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0ccell\u6570\u7ec4\u901a\u5e38\u6307\u7684\u662f\u7528\u5217\u8868\u6216\u5b57\u5178\u6765\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e\u5217\u8868\u6765\u5b9e\u73b0\u7c7b\u4f3c\u4e8eMATLAB\u4e2d\u7684cell\u6570\u7ec4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">cell_array = [1, &#39;text&#39;, [1, 2, 3], {&#39;key&#39;: &#39;value&#39;}]\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0ccell_array\u5305\u542b\u4e0d\u540c\u7c7b\u578b\u7684\u5143\u7d20\uff0c\u5305\u62ec\u6574\u6570\u3001\u5b57\u7b26\u4e32\u3001\u5217\u8868\u548c\u5b57\u5178\u3002\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u6216\u4fee\u6539\u8fd9\u4e9b\u5143\u7d20\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0ccell\u6570\u7ec4\u7684\u5e94\u7528\u573a\u666f\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>cell\u6570\u7ec4\u5728\u6570\u636e\u5904\u7406\u548c\u79d1\u5b66\u8ba1\u7b97\u4e2d\u975e\u5e38\u6709\u7528\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5f02\u6784\u6570\u636e\u65f6\u3002\u53ef\u4ee5\u7528\u4e8e\u5b58\u50a8\u56fe\u50cf\u6570\u636e\u3001\u6587\u672c\u6570\u636e\u3001\u6df7\u5408\u7c7b\u578b\u7684\u6570\u636e\u96c6\u7b49\u3002\u4f8b\u5982\uff0c\u5728<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5c06\u4e0d\u540c\u7c7b\u578b\u7684\u7279\u5f81\u5b58\u50a8\u5728\u540c\u4e00\u4e2a\u7ed3\u6784\u4e2d\uff0ccell\u6570\u7ec4\u53ef\u4ee5\u9ad8\u6548\u5730\u7ba1\u7406\u8fd9\u4e9b\u7279\u5f81\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u7c7b\u4f3cMATLAB\u7684cell\u6570\u7ec4\u529f\u80fd\uff1f<\/strong><br \/>\u867d\u7136Python\u672c\u8eab\u6ca1\u6709\u76f4\u63a5\u7684cell\u6570\u7ec4\u7c7b\u578b\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u7684ndarray\u6216Pandas\u5e93\u7684DataFrame\u6765\u5b9e\u73b0\u76f8\u4f3c\u7684\u529f\u80fd\u3002NumPy\u652f\u6301\u591a\u7ef4\u6570\u7ec4\uff0c\u53ef\u4ee5\u5b58\u50a8\u540c\u7c7b\u578b\u7684\u5143\u7d20\uff0c\u800cPandas\u7684DataFrame\u53ef\u4ee5\u5904\u7406\u8868\u683c\u6570\u636e\uff0c\u5305\u62ec\u4e0d\u540c\u7c7b\u578b\u7684\u5217\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Pandas\u521b\u5efa\u7c7b\u4f3ccell\u6570\u7ec4\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndata = {&#39;Column1&#39;: [1, 2], &#39;Column2&#39;: [&#39;text1&#39;, &#39;text2&#39;], &#39;Column3&#39;: [[1, 2], [3, 4]]}\ndf = pd.DataFrame(data)\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cDataFrame\u4e2d\u7684\u6bcf\u4e00\u5217\u53ef\u4ee5\u5305\u542b\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\uff0c\u7c7b\u4f3c\u4e8ecell\u6570\u7ec4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u521b\u5efa\u548c\u64cd\u4f5c\u7c7b\u4f3c\u4e8eMATLAB\u4e2dcell\u6570\u7ec4\u7684\u7ed3\u6784\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001\u5b57\u5178\u6216numpy\u5e93\u4e2d\u7684\u5bf9\u8c61 [&hellip;]","protected":false},"author":3,"featured_media":936879,"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\/936875"}],"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=936875"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/936875\/revisions"}],"predecessor-version":[{"id":936881,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/936875\/revisions\/936881"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/936879"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=936875"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=936875"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=936875"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}