{"id":343587,"date":"2021-02-03T15:27:37","date_gmt":"2021-02-03T07:27:37","guid":{"rendered":"http:\/\/4563.org\/?p=343587"},"modified":"2021-02-03T15:27:37","modified_gmt":"2021-02-03T07:27:37","slug":"%e7%bf%bb%e8%af%91%ef%bc%9a%e3%80%8a%e5%ae%9e%e7%94%a8%e7%9a%84-python-%e7%bc%96%e7%a8%8b%e3%80%8b01_05_lists","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=343587","title":{"rendered":"\u7ffb\u8bd1\uff1a\u300a\u5b9e\u7528\u7684 Python \u7f16\u7a0b\u300b01_05_Lists"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u7ffb\u8bd1\uff1a\u300a\u5b9e\u7528\u7684 Python \u7f16\u7a0b\u300b01_05_Lists               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : codists <\/span>  <span><i><\/i> 2<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<h1>1.5 \u5217\u8868<\/h1>\n<p> <\/p>\n<p>\u672c\u8282\u4ecb\u7ecd Python \u539f\u59cb\u6570\u636e\u7c7b\u578b\u5217\u8868\uff08 list \uff09\u3002 \u5217\u8868\u662f\u4e00\u79cd\u6709\u5e8f\u7684\u96c6\u5408\u3002<\/p>\n<h3>\u521b\u5efa\u5217\u8868<\/h3>\n<p>\u4f7f\u7528\u65b9\u62ec\u53f7 [] \u6765\u5b9a\u4e49\u5217\u8868\u5b57\u9762\u91cf\u3002<\/p>\n<pre><code>names = [ 'Elwood', 'Jake', 'Curtis' ] nums = [ 39, 38, 42, 65, 111] <\/code><\/pre>\n<p>\u6709\u65f6\u5019\uff0c\u5217\u8868\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5176\u5b83\u65b9\u6cd5\u521b\u5efa\u3002\u4f8b\u5982\uff1a\u4f7f\u7528\u5b57\u7b26\u4e32\u7684 <code>split()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u62c6\u5206\u4e3a\u4e00\u4e2a\u5217\u8868\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; line = 'GOOG,100,490.10' &gt;&gt;&gt; row = line.split(',') &gt;&gt;&gt; row ['GOOG', '100', '490.10'] &gt;&gt;&gt; <\/code><\/pre>\n<h3>\u5217\u8868\u64cd\u4f5c<\/h3>\n<p>\u5217\u8868\u53ef\u4ee5\u5b58\u50a8\u4efb\u4f55\u7c7b\u578b\u7684\u9879\uff08\u8bd1\u6ce8\uff1aitem\uff0c\u6216\u8005\u79f0\u4e3a<strong>\u5143\u7d20<\/strong>\uff09\u3002\u4f7f\u7528 <code>append()<\/code> \u65b9\u6cd5\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u9879\uff1a<\/p>\n<pre><code>names.append('Murphy')    # Adds at end names.insert(2, 'Aretha') # Inserts in middle <\/code><\/pre>\n<p>\u4f7f\u7528\u52a0\u53f7 + \u6765\u62fc\u63a5\u5217\u8868\uff1a<\/p>\n<pre><code>s = [1, 2, 3] t = ['a', 'b'] s + t           # [1, 2, 3, 'a', 'b'] <\/code><\/pre>\n<p>\u5217\u8868\u901a\u8fc7\u6574\u6570\u8fdb\u884c\u7d22\u5f15\uff0c\u7d22\u5f15\u4ece 0 \u5f00\u59cb\u3002<\/p>\n<pre><code>names = [ 'Elwood', 'Jake', 'Curtis' ]  names[0]  # 'Elwood' names[1]  # 'Jake' names[2]  # 'Curtis' <\/code><\/pre>\n<p>\u8d1f\u7d22\u5f15\u4ece\u5217\u8868\u5c3e\u90e8\u5f00\u59cb\u8ba1\u6570\uff1a<\/p>\n<pre><code>names[-1] # 'Curtis' <\/code><\/pre>\n<p>\u4f60\u53ef\u4ee5\u6539\u53d8\u5217\u8868\u4e2d\u7684\u4efb\u4f55\u9879:<\/p>\n<pre><code>names[1] = 'Joliet Jake' names                     # [ 'Elwood', 'Joliet Jake', 'Curtis' ] <\/code><\/pre>\n<p>\u5217\u8868\u7684\u957f\u5ea6\uff1a<\/p>\n<pre><code>names = ['Elwood','Jake','Curtis'] len(names)  # 3 <\/code><\/pre>\n<p>\u6210\u5458\u6d4b\u8bd5\uff08<code>in<\/code>, <code>not in<\/code>\uff09\uff1a<\/p>\n<pre><code>'Elwood' in names       # True 'Britney' not in names  # True <\/code><\/pre>\n<p>\u590d\u5236\uff08<code>s * n<\/code>\uff09\uff1a<\/p>\n<pre><code>s = [1, 2, 3] s * 3   # [1, 2, 3, 1, 2, 3, 1, 2, 3] <\/code><\/pre>\n<h3>\u5217\u8868\u904d\u5386\u548c\u67e5\u627e\uff1a<\/h3>\n<p>\u4f7f\u7528 <code>for<\/code> \u904d\u5386\u5217\u8868\u5185\u5bb9\uff1a<\/p>\n<pre><code>for name in names:     # use name     # e.g. print(name)     ... <\/code><\/pre>\n<p>\u8fd9\u548c\u5176\u5b83\u7f16\u7a0b\u8bed\u8a00\u7684 <code>foreach<\/code> \u8bed\u53e5\u7c7b\u4f3c\u3002<\/p>\n<p>\u4e3a\u4e86\u5feb\u901f\u627e\u5230\u67d0\u9879\u7684\u4f4d\u7f6e\uff08\u7d22\u5f15\uff09\uff0c\u4f7f\u7528 <code>index()<\/code> \u51fd\u6570\uff1a<\/p>\n<pre><code>names = ['Elwood','Jake','Curtis'] names.index('Curtis')   # 2 <\/code><\/pre>\n<p>\u5982\u679c\u5143\u7d20\u51fa\u73b0\u4e0d\u6b62\u4e00\u6b21\uff0c<code>index()<\/code> \u65b9\u6cd5\u5c06\u4f1a\u8fd4\u56de\u5143\u7d20\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\u3002<\/p>\n<p>\u5982\u679c\u5143\u7d20\u6ca1\u6709\u627e\u5230\uff0c<code>index()<\/code> \u65b9\u6cd5\u5c06\u5f15\u53d1 <code>ValueError<\/code> \u5f02\u5e38\u3002<\/p>\n<h3>\u5217\u8868\u5220\u9664<\/h3>\n<p>\u4f60\u53ef\u4ee5\u6309\u5143\u7d20\u503c\u6216\u8005\u6309\u7d22\u5f15\u5220\u9664\u5143\u7d20\uff1a<\/p>\n<pre><code># Using the value names.remove('Curtis')  # Using the index del names[1] <\/code><\/pre>\n<p>\u79fb\u9664\u4e00\u4e2a\u5143\u7d20\u4e0d\u4f1a\u201c\u7559\u7a7a\u201d\u3002\u5176\u5b83\u7684\u5143\u7d20\u5c06\u4f1a\u79fb\u52a8\u6765\u586b\u5145\u5220\u9664\u5143\u7d20\u540e\u817e\u51fa\u7684\u7a7a\u95f4\u3002\u5982\u679c\u5143\u7d20\u51fa\u73b0\u4e0d\u6b62\u4e00\u6b21\uff0c<code>remove()<\/code> \u65b9\u6cd5\u5c06\u53ea\u5220\u9664\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u5143\u7d20\u3002<\/p>\n<h3>\u5217\u8868\u6392\u5e8f<\/h3>\n<p>\u5217\u8868\u53ef\u4ee5\u201c\u539f\u5730\u201d\u6392\u5e8f\uff1a<\/p>\n<pre><code>s = [10, 1, 7, 3] s.sort()                    # [1, 3, 7, 10]  # Reverse order s = [10, 1, 7, 3] s.sort(reverse=True)        # [10, 7, 3, 1]  # It works with any ordered data s = ['foo', 'bar', 'spam'] s.sort()                    # ['bar', 'foo', 'spam'] <\/code><\/pre>\n<p>\u5982\u679c\u4f60\u60f3\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u4f7f\u7528 <code>sorted()<\/code> \u51fd\u6570\uff1a<\/p>\n<pre><code>t = sorted(s)               # s unchanged, t holds sorted values <\/code><\/pre>\n<h3>\u5217\u8868\u548c\u6570\u5b66<\/h3>\n<p>\u8b66\u544a\uff1a\u5217\u8868\u4e0d\u662f\u4e3a\u6570\u5b66\u8fd0\u7b97\u800c\u8bbe\u8ba1\u7684\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; nums = [1, 2, 3, 4, 5] &gt;&gt;&gt; nums * 2 [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] &gt;&gt;&gt; nums + [10, 11, 12, 13, 14] [1, 2, 3, 4, 5, 10, 11, 12, 13, 14] <\/code><\/pre>\n<p>\u7279\u522b\u5730\uff0c\u5217\u8868\u65e0\u6cd5\u50cf MATLAB, Octave, R \u90a3\u6837\u8868\u793a\u5411\u91cf \/\u77e9\u9635\u3002\u4f46\u662f\uff0c\u6709\u4e00\u4e9b\u5305\u53ef\u4ee5\u5e2e\u52a9\u4f60\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff08\u4f8b\u5982\uff1anumpy\uff09\u3002<\/p>\n<h2>\u7ec3\u4e60<\/h2>\n<p>\u5728\u672c\u6b21\u7ec3\u4e60\u4e2d\uff0c\u6211\u4eec\u5c1d\u8bd5\u4f7f\u7528 Python \u7684\u5217\u8868\u6570\u636e\u7c7b\u578b\u3002\u5728\u4e0a\u4e00\u8282\u4e2d\uff0c\u4f60\u4f7f\u7528\u4e86\u5305\u542b\u80a1\u7968\u4ee3\u7801\u7684\u5b57\u7b26\u4e32\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; symbols = 'HPQ,AAPL,IBM,MSFT,YHOO,DOA,GOOG' <\/code><\/pre>\n<p>\u4f7f\u7528\u5b57\u7b26\u4e32\u7684 <code>split()<\/code> \u65b9\u6cd5\u628a symbols \u62c6\u5206\u4e3a\u4e00\u4e2a\u5305\u542b\u80a1\u7968\u4ee3\u7801\u540d\u5b57\u7684\u5217\u8868\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; symlist = symbols.split(',') <\/code><\/pre>\n<h3>\u7ec3\u4e60 1.19\uff1a\u63d0\u53d6\u548c\u91cd\u65b0\u5206\u914d\u5217\u8868\u5143\u7d20<\/h3>\n<p>\u5c1d\u8bd5\u4e00\u4e9b\u67e5\u627e\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; symlist[0] 'HPQ' &gt;&gt;&gt; symlist[1] 'AAPL' &gt;&gt;&gt; symlist[-1] 'GOOG' &gt;&gt;&gt; symlist[-2] 'DOA' &gt;&gt;&gt; <\/code><\/pre>\n<p>\u5c1d\u8bd5\u91cd\u65b0\u5206\u914d\u4e00\u4e2a\u503c\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; symlist[2] = 'AIG' &gt;&gt;&gt; symlist ['HPQ', 'AAPL', 'AIG', 'MSFT', 'YHOO', 'DOA', 'GOOG'] &gt;&gt;&gt; <\/code><\/pre>\n<p>\u5207\u7247\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; symlist[0:3] ['HPQ', 'AAPL', 'AIG'] &gt;&gt;&gt; symlist[-2:] ['DOA', 'GOOG'] &gt;&gt;&gt; <\/code><\/pre>\n<p>\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u5217\u8868\u5e76\u6dfb\u52a0\u4e00\u4e2a\u5143\u7d20\u5230\u5176\u4e2d\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; mysyms = [] &gt;&gt;&gt; mysyms.append('GOOG') &gt;&gt;&gt; mysyms ['GOOG'] <\/code><\/pre>\n<p>\u4f60\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5217\u8868\u7684\u4e00\u90e8\u5206\u91cd\u65b0\u5206\u914d\u5230\u53e6\u4e00\u4e2a\u5217\u8868\u4e2d\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; symlist[-2:] = mysyms &gt;&gt;&gt; symlist ['HPQ', 'AAPL', 'AIG', 'MSFT', 'YHOO', 'GOOG'] &gt;&gt;&gt; <\/code><\/pre>\n<p>\u5f53\u6267\u884c\u6b64\u64cd\u4f5c\u65f6\uff0c\u5de6\u624b\u8fb9\u7684\u5217\u8868\uff08<code>symlist<\/code>\uff09\u7684\u5927\u5c0f\u5c06\u4f1a\u88ab\u9002\u5f53\u8c03\u6574\uff0c\u4ee5\u9002\u5e94\u53f3\u624b\u8fb9\u7684\u5217\u8868\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5728\u4e0a\u9762\u7684\u5b9e\u4f8b\u4e2d\uff0c<code>symlist<\/code> \u7684\u6700\u540e\u4e24\u9879\u88ab <code>mysyms<\/code> \u5217\u8868\u4e2d\u7684\u5355\u4e2a\u5143\u7d20\uff08&#8217;GOOG&#8217;\uff09\u53d6\u4ee3\u3002<\/p>\n<h3>\u7ec3\u4e60 1.20\uff1a\u904d\u5386\u5217\u8868\u5143\u7d20<\/h3>\n<p><code>for<\/code> \u5faa\u73af\u53ef\u4ee5\u904d\u5386\u5217\u8868\u8fd9\u6837\u7684\u5e8f\u5217\u3002\u901a\u8fc7\u8f93\u5165\u4e0b\u5217\u7684\u5faa\u73af\u5e76\u4e14\u67e5\u770b\u53d1\u751f\u4e86\u4ec0\u4e48\u6765\u9a8c\u8bc1\u8fd9\u70b9\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; for s in symlist:         print('s =', s) # Look at the output <\/code><\/pre>\n<h3>\u7ec3\u4e60 1.21\uff1a\u6210\u5458\u6d4b\u8bd5<\/h3>\n<p>\u4f7f\u7528 <code>in<\/code> \u6216\u8005 <code>not in<\/code> \u64cd\u4f5c\u7b26\u6765\u68c0\u67e5 <code>'AIG'<\/code>\uff0c<code>'AA'<\/code>\uff0c\u548c <code>'CAT'<\/code> \u662f\u5426\u5728 symbols \u5217\u8868\u4e2d\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; # Is 'AIG' IN the `symlist`? True &gt;&gt;&gt; # Is 'AA' IN the `symlist`? False &gt;&gt;&gt; # Is 'CAT' NOT IN the `symlist`? True &gt;&gt;&gt; <\/code><\/pre>\n<h3>\u7ec3\u4e60 1.22\uff1a\u6dfb\u52a0\uff0c\u63d2\u5165\u548c\u5220\u9664\u5143\u7d20<\/h3>\n<p>\u4f7f\u7528 <code>append()<\/code> \u65b9\u6cd5\u628a <code>'RHT'<\/code> \u6dfb\u52a0\u5230\u5217\u8868 <code>symlist<\/code> \u7684\u672b\u5c3e\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; # append 'RHT' &gt;&gt;&gt; symlist ['HPQ', 'AAPL', 'AIG', 'MSFT', 'YHOO', 'GOOG', 'RHT'] &gt;&gt;&gt; <\/code><\/pre>\n<p>\u4f7f\u7528 <code>insert()<\/code> \u65b9\u6cd5\u628a <code>'AA'<\/code> \u4f5c\u4e3a\u5217\u8868\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20\u63d2\u5165\u5230\u5217\u8868\u4e2d\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; # Insert 'AA' as the second item in the list &gt;&gt;&gt; symlist ['HPQ', 'AA', 'AAPL', 'AIG', 'MSFT', 'YHOO', 'GOOG', 'RHT'] &gt;&gt;&gt; <\/code><\/pre>\n<p>\u4f7f\u7528 <code>remove()<\/code> \u65b9\u6cd5\u4ece\u5217\u8868\u4e2d\u5220\u9664 <code>'MSFT'<\/code>\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; # Remove 'MSFT' &gt;&gt;&gt; symlist ['HPQ', 'AA', 'AAPL', 'AIG', 'YHOO', 'GOOG', 'RHT'] &gt;&gt;&gt; <\/code><\/pre>\n<p>\u6dfb\u52a0\u4e00\u4e2a\u91cd\u590d\u7684 <code>'YHOO'<\/code> \u6761\u76ee\u5230\u5217\u8868\u7684\u672b\u5c3e\u3002<\/p>\n<p><em>\u6ce8\u610f\uff1a\u5217\u8868\u6709\u91cd\u590d\u7684\u503c\u662f\u5b8c\u5168\u6ca1\u6709\u95ee\u9898\u7684\uff1a<\/em><\/p>\n<pre><code>&gt;&gt;&gt; # Append 'YHOO' &gt;&gt;&gt; symlist ['HPQ', 'AA', 'AAPL', 'AIG', 'YHOO', 'GOOG', 'RHT', 'YHOO'] &gt;&gt;&gt; <\/code><\/pre>\n<p>\u4f7f\u7528 <code>index()<\/code> \u65b9\u6cd5\u67e5\u770b <code>'YHOO'<\/code> \u5728\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; # Find the first index of 'YHOO' 4 &gt;&gt;&gt; symlist[4] 'YHOO' &gt;&gt;&gt; <\/code><\/pre>\n<p>\u7edf\u8ba1 <code>'YHOO'<\/code> \u5728\u5217\u8868\u4e2d\u51fa\u73b0\u4e86\u591a\u5c11\u6b21\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; symlist.count('YHOO') 2 &gt;&gt;&gt; <\/code><\/pre>\n<p>\u5220\u9664\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684 <code>'YHOO'<\/code>\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; # Remove first occurrence 'YHOO' &gt;&gt;&gt; symlist ['HPQ', 'AA', 'AAPL', 'AIG', 'GOOG', 'RHT', 'YHOO'] &gt;&gt;&gt; <\/code><\/pre>\n<p>\u4f17\u6240\u5468\u77e5\uff0c\u6ca1\u6709\u65b9\u6cd5\u627e\u5230\u6216\u8005\u5220\u9664\u67d0\u4e2a\u5143\u7d20\u5728\u5217\u8868\u4e2d\u91cd\u590d\u51fa\u73b0\u7684\u6240\u6709\u9879\u3002\u4f46\u662f\uff0c\u6211\u4eec\u5c06\u4f1a\u5728\u7b2c\u4e8c\u8282\u770b\u5230\u4e00\u79cd\u4f18\u96c5\u7684\u65b9\u5f0f\u53bb\u5b9e\u73b0\u5b83\u3002<\/p>\n<h3>\u7ec3\u4e60 1.23\uff1a\u6392\u5e8f<\/h3>\n<p>\u60f3\u8981\u5bf9\u4e00\u4e2a\u5217\u8868\u6392\u5e8f\u5417\uff1f\u4f7f\u7528 <code>sort()<\/code> \u65b9\u6cd5\u3002\u8bd5\u8bd5\u770b\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; symlist.sort() &gt;&gt;&gt; symlist ['AA', 'AAPL', 'AIG', 'GOOG', 'HPQ', 'RHT', 'YHOO'] &gt;&gt;&gt; <\/code><\/pre>\n<p>\u60f3\u8981\u5bf9\u4e00\u4e2a\u5217\u8868\u5012\u5e8f\u5417\uff1f\u5c1d\u8bd5\u8fd9\u4e2a\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; symlist.sort(reverse=True) &gt;&gt;&gt; symlist ['YHOO', 'RHT', 'HPQ', 'GOOG', 'AIG', 'AAPL', 'AA'] &gt;&gt;&gt; <\/code><\/pre>\n<p>\u6ce8\u610f\uff1a\u5bf9\u5217\u8868\u6392\u5e8f\u53ef\u4ee5\u201c\u539f\u5730\u201d\u4fee\u6539\u5176\u5185\u5bb9\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u662f\u5bf9\u5217\u8868\u7684\u5143\u7d20\u8fdb\u884c\u201c\u6d17\u724c\u201d\uff0c\u800c\u4e0d\u662f\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u4f5c\u4e3a\u7ed3\u679c\u3002<\/p>\n<h3>\u7ec3\u4e60 1.24\uff1a\u5217\u8868\u8f6c\u5b57\u7b26\u4e32<\/h3>\n<p>\u60f3\u8981\u628a\u4e00\u4e2a\u5305\u542b\u5b57\u7b26\u4e32\u7684\u5217\u8868\u8fde\u63a5\u5230\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u5417\uff1f\u50cf\u4e0b\u9762\u8fd9\u6837\u4f7f\u7528\u5b57\u7b26\u4e32\u7684 <code>join()<\/code> \u65b9\u6cd5\u5b9e\u73b0\uff08\u6ce8\u610f\uff1a\u8fd9\u521d\u770b\u8d77\u6765\u5f88\u6709\u8da3\uff09\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; a = ','.join(symlist) &gt;&gt;&gt; a 'YHOO,RHT,HPQ,GOOG,AIG,AAPL,AA' &gt;&gt;&gt; b = ':'.join(symlist) &gt;&gt;&gt; b 'YHOO:RHT:HPQ:GOOG:AIG:AAPL:AA' &gt;&gt;&gt; c = ''.join(symlist) &gt;&gt;&gt; c 'YHOORHTHPQGOOGAIGAAPLAA' &gt;&gt;&gt; <\/code><\/pre>\n<h3>\u7ec3\u4e60 1.25\uff1a\u5305\u542b\u4efb\u4f55\u5185\u5bb9\u7684\u5217\u8868<\/h3>\n<p>\u5217\u8868\u53ef\u4ee5\u5305\u542b\u4efb\u4f55\u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u5305\u62ec\u5217\u8868\uff08\u793a\u4f8b\uff1a\u5d4c\u5957\u7684\u5217\u8868\uff09\u3002\u8bd5\u8bd5\u770b\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; nums = [101, 102, 103] &gt;&gt;&gt; items = ['spam', symlist, nums] &gt;&gt;&gt; items ['spam', ['YHOO', 'RHT', 'HPQ', 'GOOG', 'AIG', 'AAPL', 'AA'], [101, 102, 103]] <\/code><\/pre>\n<p>\u8bf7\u6ce8\u610f\u4e0a\u9762\u7684\u8f93\u51fa\uff0c<code>items<\/code> \u662f\u4e00\u4e2a\u5305\u542b\u4e09\u4e2a\u5143\u7d20\u7684\u5217\u8868\uff0c\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5176\u5b83\u4e24\u4e2a\u5143\u7d20\u662f\u5217\u8868\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u591a\u4e2a\u7d22\u5f15\u64cd\u4f5c\u6765\u8bbf\u95ee\u5d4c\u5957\u5217\u8868\u7684\u9879\u3002<\/p>\n<pre><code>&gt;&gt;&gt; items[0] 'spam' &gt;&gt;&gt; items[0][0] 's' &gt;&gt;&gt; items[1] ['YHOO', 'RHT', 'HPQ', 'GOOG', 'AIG', 'AAPL', 'AA'] &gt;&gt;&gt; items[1][1] 'RHT' &gt;&gt;&gt; items[1][1][2] 'T' &gt;&gt;&gt; items[2] [101, 102, 103] &gt;&gt;&gt; items[2][1] 102 &gt;&gt;&gt; <\/code><\/pre>\n<p>\u5c3d\u7ba1\u5728\u6280\u672f\u4e0a\u80fd\u591f\u751f\u6210\u975e\u5e38\u590d\u6742\u7684\u5217\u8868\u7ed3\u6784\uff0c\u4f46\u4f5c\u4e3a\u4e00\u822c\u89c4\u5219\uff0c\u4f60\u8fd8\u662f\u5e0c\u671b\u4fdd\u6301\u7b80\u5355\u3002\u901a\u5e38\uff0c\u5217\u8868\u5b58\u50a8\u7c7b\u578b\u76f8\u540c\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5b8c\u5168\u7531\u6570\u5b57\u6216\u8005\u6587\u672c\u5b57\u7b26\u4e32\u6784\u6210\u7684\u5217\u8868\u3002\u5728\u540c\u4e00\u4e2a\u5217\u8868\u4e2d\u6df7\u5408\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5f80\u5f80\u4f1a\u8ba9\u4eba\u611f\u5230\u5934\u75db\uff0c\u56e0\u6b64\u6700\u597d\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u3002<\/p>\n<p>\u6ce8\uff1a\u5b8c\u6574\u7ffb\u8bd1\u89c1 https:\/\/github.com\/codists\/practical-python-zh<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u7ffb\u8bd1\uff1a\u300a\u5b9e\u7528\u7684 Python \u7f16\u7a0b&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/343587"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=343587"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/343587\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=343587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=343587"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=343587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}