{"id":388113,"date":"2021-03-09T07:41:53","date_gmt":"2021-03-08T23:41:53","guid":{"rendered":"http:\/\/4563.org\/?p=388113"},"modified":"2021-03-12T13:46:41","modified_gmt":"2021-03-12T05:46:41","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%8b02_07_objects","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=388113","title":{"rendered":"\u7ffb\u8bd1\uff1a\u300a\u5b9e\u7528\u7684 Python \u7f16\u7a0b\u300b02_07_Objects"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u7ffb\u8bd1\uff1a\u300a\u5b9e\u7528\u7684 Python \u7f16\u7a0b\u300b02_07_Objects               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : codists <\/span>  <span><i><\/i> 4<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<h1>2.7 \u5bf9\u8c61<\/h1>\n<p> <\/p>\n<p>\u672c\u8282\u4ecb\u7ecd\u6709\u5173 Python \u5185\u90e8\u5bf9\u8c61\u6a21\u578b\u7684\u66f4\u591a\u8be6\u7ec6\u4fe1\u606f\uff0c\u5e76\u8ba8\u8bba\u4e00\u4e9b\u4e0e\u5185\u5b58\u7ba1\u7406\uff0c\u62f7\u8d1d\u548c\u7c7b\u578b\u68c0\u67e5\u6709\u5173\u7684\u95ee\u9898\u3002<\/p>\n<h3>\u8d4b\u503c<\/h3>\n<p>Python \u4e2d\u7684\u8bb8\u591a\u64cd\u4f5c\u90fd\u4e0e\u8d4b\u503c\u6216\u8005\u5b58\u50a8\u503c\u6709\u5173\u3002<\/p>\n<pre><code>a = value         # Assignment to a variable s[n] = value      # Assignment to a list s.append(value)   # Appending to a list d['key'] = value  # Adding to a dictionary <\/code><\/pre>\n<p><em>\u8b66\u544a\uff1a\u8d4b\u503c\u64cd\u4f5c<strong>\u6c38\u8fdc\u4e0d\u662f\u503c\u62f7\u8d1d<\/strong>\u3002\u6240\u6709\u7684\u8d4b\u503c\u64cd\u4f5c\u90fd\u662f\u5f15\u7528\u62f7\u8d1d\uff08\u5982\u679c\u4f60\u4e50\u610f\uff0c\u4e5f\u53ef\u4ee5\u8bf4\u662f\u6307\u9488\u62f7\u8d1d\uff09<\/em><\/p>\n<h3>\u8d4b\u503c\u793a\u4f8b<\/h3>\n<p>\u8003\u8651\u8be5\u4ee3\u7801\u7247\u6bb5\uff1a<\/p>\n<pre><code>a = [1,2,3] b = a c = [a,b] <\/code><\/pre>\n<p>\u4ee5\u4e0b\u662f\u5e95\u5c42\u5185\u5b58\u64cd\u4f5c\u56fe\u3002\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u53ea\u6709\u4e00\u4e2a\u5217\u8868\u5bf9\u8c61 <code>[1,2,3]<\/code>\uff0c\u4f46\u662f\u6709\u56db\u4e2a\u4e0d\u540c\u7684\u5f15\u7528\u6307\u5411\u5b83\u3002<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" referrerpolicy=\"no-referrer\" rel=\"noreferrer\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2021\/03\/references.png\" alt=\"\u7ffb\u8bd1\uff1a\u300a\u5b9e\u7528\u7684 Python \u7f16\u7a0b\u300b02_07_Objects\" \/><\/p>\n<p>\u8fd9\u610f\u5473\u7740\u4fee\u6539\u4e00\u4e2a\u503c\u4f1a\u5f71\u54cd\u6240\u6709\u7684\u5f15\u7528\u3002<\/p>\n<pre><code>&gt;&gt;&gt; a.append(999) &gt;&gt;&gt; a [1,2,3,999] &gt;&gt;&gt; b [1,2,3,999] &gt;&gt;&gt; c [[1,2,3,999], [1,2,3,999]] &gt;&gt;&gt; <\/code><\/pre>\n<p>\u8bf7\u6ce8\u610f\uff0c\u539f\u59cb\u5217\u8868\u4e2d\u7684\u66f4\u6539\u662f\u5982\u4f55\u5728\u5176\u5b83\u5730\u65b9\u663e\u793a\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u4ece\u672a\u8fdb\u884c\u4efb\u4f55\u62f7\u8d1d\uff0c\u6240\u6709\u7684\u4e1c\u897f\u90fd\u6307\u5411\u540c\u4e00\u4e2a\u4e1c\u897f\u3002<\/p>\n<h3>\u91cd\u65b0\u8d4b\u503c<\/h3>\n<p>\u91cd\u65b0\u8d4b\u503c<em>\u6c38\u8fdc<\/em>\u4e0d\u4f1a\u91cd\u5199\u4e4b\u524d\u7684\u503c\u6240\u4f7f\u7528\u7684\u5185\u5b58\u3002<\/p>\n<pre><code>a = [1,2,3] b = a a = [4,5,6]  print(a)      # [4, 5, 6] print(b)      # [1, 2, 3]    Holds the original value <\/code><\/pre>\n<p>\u5207\u8bb0\uff1a<strong>\u53d8\u91cf\u662f\u540d\u79f0\uff0c\u4e0d\u662f\u5185\u5b58\u5730\u5740<\/strong><\/p>\n<h3>\u98ce\u9669<\/h3>\n<p>\u5982\u679c\u4f60\u4e0d\u77e5\u9053\u8fd9\u79cd\uff08\u6570\u636e\uff09\u5171\u4eab\uff08\u7684\u65b9\u5f0f\uff09\uff0c\u90a3\u4e48\u5728\u67d0\u4e9b\u65f6\u5019\u4f60\u4f1a\u642c\u8d77\u77f3\u5934\u7838\u81ea\u5df1\u7684\u811a\u3002\u5178\u578b\u60c5\u666f\uff0c\u4f60\u4fee\u6539\u4e86\u4e00\u4e9b\u6570\u636e\uff0c\u4ee5\u4e3a\u5b83\u662f\u81ea\u5df1\u7684\u79c1\u6709\u62f7\u8d1d\uff0c\u4f46\u662f\u5b83\u5374\u610f\u5916\u5730\u635f\u7834\u574f\u4e86\u7a0b\u5e8f\u5176\u5b83\u90e8\u5206\u7684\u67d0\u4e9b\u6570\u636e\u3002<\/p>\n<p><em>\u8bf4\u660e\uff1a\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u539f\u59cb\u6570\u636e\u7c7b\u578b\u662f\u4e0d\u53ef\u53d8\uff08\u53ea\u8bfb\uff09\u7684\u539f\u56e0\u4e4b\u4e00<\/em><\/p>\n<h3>\u6807\u8bc6\u503c\u548c\u5f15\u7528<\/h3>\n<p>\u4f7f\u7528 <code>is<\/code> \u64cd\u4f5c\u7b26\u68c0\u67e5\u4e24\u4e2a\u503c\u662f\u5426\u771f\u7684\u662f\u76f8\u540c\u7684\u5bf9\u8c61\u3002<\/p>\n<pre><code>&gt;&gt;&gt; a = [1,2,3] &gt;&gt;&gt; b = a &gt;&gt;&gt; a is b True &gt;&gt;&gt; <\/code><\/pre>\n<p><code>is<\/code> \u64cd\u4f5c\u7b26\u6bd4\u8f83\u5bf9\u8c61\u7684\u6807\u8bc6\u503c\uff08\u4e00\u4e2a\u6574\u6570\uff09\u3002\u6807\u8bc6\u503c\u53ef\u4ee5\u4f7f\u7528 <code>id()<\/code> \u51fd\u6570\u83b7\u53d6\u3002<\/p>\n<pre><code>&gt;&gt;&gt; id(a) 3588944 &gt;&gt;&gt; id(b) 3588944 &gt;&gt;&gt; <\/code><\/pre>\n<p>\u6ce8\u610f\uff1a\u4f7f\u7528 <code>==<\/code> \u68c0\u67e5\u5bf9\u8c61\u662f\u5426\u76f8\u7b49\u51e0\u4e4e\u603b\u662f\u66f4\u597d\uff0c<code>is<\/code>\u7684\u7ed3\u679c\u901a\u5e38\u4f1a\u51fa\u4e4e\u610f\u6599\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; a = [1,2,3] &gt;&gt;&gt; b = a &gt;&gt;&gt; c = [1,2,3] &gt;&gt;&gt; a is b True &gt;&gt;&gt; a is c False &gt;&gt;&gt; a == c True &gt;&gt;&gt; <\/code><\/pre>\n<h3>\u6d45\u62f7\u8d1d<\/h3>\n<p>\u5217\u8868\u548c\u5b57\u5178\u81ea\u8eab\u5177\u6709\u7528\u4e8e\u62f7\u8d1d\u7684\u65b9\u6cd5\u3002<\/p>\n<pre><code>&gt;&gt;&gt; a = [2,3,[100,101],4] &gt;&gt;&gt; b = list(a) # Make a copy &gt;&gt;&gt; a is b False <\/code><\/pre>\n<p>\u8fd9\u662f\u4e00\u4e2a\u65b0\u5217\u8868\uff0c\u4f46\u662f\u5217\u8868\u4e2d\u7684\u9879\u662f\u5171\u4eab\u7684\u3002<\/p>\n<pre><code>&gt;&gt;&gt; a[2].append(102) &gt;&gt;&gt; b[2] [100,101,102] &gt;&gt;&gt; &gt;&gt;&gt; a[2] is b[2] True &gt;&gt;&gt; <\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u5185\u90e8\u5217\u8868 <code>[100, 101, 102]<\/code> \u6b63\u5728\u5171\u4eab\u3002\u8fd9\u5c31\u662f\u4f17\u6240\u7686\u77e5\u7684\u6d45\u62f7\u8d1d\u3002\u4e0b\u9762\u662f\u56fe\u793a\uff1a<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" referrerpolicy=\"no-referrer\" rel=\"noreferrer\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2021\/03\/shallow.png\" alt=\"\u7ffb\u8bd1\uff1a\u300a\u5b9e\u7528\u7684 Python \u7f16\u7a0b\u300b02_07_Objects\" \/><\/p>\n<h3>\u6df1\u62f7\u8d1d<\/h3>\n<p>\u6709\u65f6\u5019\uff0c\u9700\u8981\u62f7\u8d1d\u4e00\u4e2a\u5bf9\u8c61\u53ca\u5176\u4e2d\u6240\u5305\u542b\u7684\u6240\u6709\u5bf9\u8c61\uff0c\u4e3a\u6b64\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>copy<\/code> \u6a21\u5757\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; a = [2,3,[100,101],4] &gt;&gt;&gt; import copy &gt;&gt;&gt; b = copy.deepcopy(a) &gt;&gt;&gt; a[2].append(102) &gt;&gt;&gt; b[2] [100,101] &gt;&gt;&gt; a[2] is b[2] False &gt;&gt;&gt; <\/code><\/pre>\n<h3>\u540d\u79f0\uff0c\u503c\uff0c\u7c7b\u578b<\/h3>\n<p>\u53d8\u91cf\u540d\u79f0\u6ca1\u6709\u7c7b\u578b\uff0c\u4ec5\u4ec5\u662f\u4e00\u4e2a\u540d\u5b57\u3002\u4f46\u662f\uff0c\u503c\u786e\u5b9e\u5177\u6709\u4e00\u4e2a\u5e95\u5c42\u7684\u7c7b\u578b\u3002<\/p>\n<pre><code>&gt;&gt;&gt; a = 42 &gt;&gt;&gt; b = 'Hello World' &gt;&gt;&gt; type(a) &lt;type 'int'&gt; &gt;&gt;&gt; type(b) &lt;type 'str'&gt; <\/code><\/pre>\n<p><code>type()<\/code> \u51fd\u6570\u5c06\u544a\u8bc9\u4f60\u8fd9\u662f\u4ec0\u4e48\u3002\u7c7b\u578b\u540d\u79f0\u901a\u5e38\u7528\u4f5c\u521b\u5efa\u6216\u5c06\u503c\u8f6c\u6362\u4e3a\u8be5\u7c7b\u578b\u7684\u51fd\u6570\u3002<\/p>\n<h3>\u7c7b\u578b\u68c0\u67e5<\/h3>\n<p>\u5982\u4f55\u5224\u65ad\u5bf9\u8c61\u662f\u5426\u4e3a\u7279\u5b9a\u7c7b\u578b\uff1f<\/p>\n<pre><code>if isinstance(a, list):     print('a is a list') <\/code><\/pre>\n<p>\u68c0\u67e5\u662f\u5426\u662f\u591a\u79cd\u7c7b\u578b\u4e2d\u7684\u4e00\u79cd\uff1a<\/p>\n<pre><code>if isinstance(a, (list,tuple)):     print('a is a list or tuple') <\/code><\/pre>\n<p><em>\u6ce8\u610f\uff1a\u4e0d\u8981\u8fc7\u5ea6\u4f7f\u7528\u7c7b\u578b\u68c0\u67e5\u3002\u8fd9\u4f1a\u5bfc\u81f4\u8fc7\u5ea6\u7684\u4ee3\u7801\u590d\u6742\u6027\u3002\u901a\u5e38\uff0c\u5982\u679c\u8fd9\u6837\u505a\u80fd\u591f\u963b\u6b62\u5176\u4ed6\u4eba\u5728\u4f7f\u7528\u4f60\u7684\u4ee3\u7801\u65f6\u72af\u5e38\u89c1\u9519\u8bef\uff0c\u90a3\u4e48\u5c31\u4f7f\u7528\u7c7b\u578b\u68c0\u67e5\u3002<\/em><\/p>\n<h3>\u4e00\u5207\u7686\u5bf9\u8c61<\/h3>\n<p>\u6570\u5b57\uff0c\u5b57\u7b26\u4e32\uff0c\u5217\u8868\uff0c\u51fd\u6570\uff0c\u5f02\u5e38\uff0c\u7c7b\uff0c\u5b9e\u4f8b\u7b49\u90fd\u662f\u5bf9\u8c61\u3002\u8fd9\u610f\u5473\u7740\u6240\u6709\u53ef\u4ee5\u547d\u540d\u7684\u5bf9\u8c61\u90fd\u53ef\u4ee5\u4f5c\u4e3a\u6570\u636e\u4f20\u9012\u3001\u653e\u7f6e\u5230\u5bb9\u5668\u4e2d\uff0c\u800c\u6ca1\u6709\u4efb\u4f55\u9650\u5236\u3002\u6ca1\u6709\u7279\u6b8a\u7684\u5bf9\u8c61\u3002\u6709\u65f6\uff0c\u53ef\u4ee5\u8fd9\u6837\u8bf4\uff0c\u6240\u6709\u7684\u5bf9\u8c61\u90fd\u662f\u201c\u4e00\u7b49\u5bf9\u8c61\u201d\u3002<\/p>\n<p>\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; import math &gt;&gt;&gt; items = [abs, math, ValueError ] &gt;&gt;&gt; items [&lt;built-in function abs&gt;,   &lt;module 'math' (builtin)&gt;,   &lt;type 'exceptions.ValueError'&gt;] &gt;&gt;&gt; items[0](-45) 45 &gt;&gt;&gt; items[1].sqrt(2) 1.4142135623730951 &gt;&gt;&gt; try:         x = int('not a number')     except items[2]:         print('Failed!') Failed! &gt;&gt;&gt; <\/code><\/pre>\n<p>\u5728\u8fd9\u91cc\uff0c<code>items<\/code> \u662f\u4e00\u4e2a\u5305\u542b\u51fd\u6570\uff0c\u6a21\u5757\u548c\u5f02\u5e38\u7684\u5217\u8868\u3002\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5217\u8868\u4e2d\u7684\u9879\u4ee3\u66ff\u539f\u59cb\u540d\u79f0\u3002<\/p>\n<pre><code>items[0](-45)       # abs items[1].sqrt(2)    # math except items[2]:    # ValueError <\/code><\/pre>\n<p>\u6743\u5229\u8d8a\u5927\uff0c\u8d23\u4efb\u8d8a\u5927\u3002\u53ea\u662f\u56e0\u4e3a\u4f60\u53ef\u4ee5\u505a\uff0c\u4f46\u5e76\u610f\u5473\u8fd9\u4f60\u5e94\u8be5\u8fd9\u6837\u505a\u3002<\/p>\n<h2>\u7ec3\u4e60<\/h2>\n<p>\u5728\u8fd9\u7ec4\u7ec3\u4e60\u4e2d\uff0c\u6211\u4eec\u6765\u770b\u770b\u6765\u81ea\u4e00\u7b49\u5bf9\u8c61\u7684\u5a01\u529b\u3002<\/p>\n<h3>\u7ec3\u4e60 2.24\uff1a\u4e00\u7b49\u6570\u636e<\/h3>\n<p>\u5728 <code>Data\/portfolio.csv<\/code> \u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u628a\u6709\u7ec4\u7ec7\u7684\u6570\u636e\u8bfb\u53d6\u4e3a\u5217\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code>name,shares,price \"AA\",100,32.20 \"IBM\",50,91.10 ... <\/code><\/pre>\n<p>\u5728\u4e4b\u524d\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>csv<\/code> \u6a21\u5757\u8bfb\u53d6\u6587\u4ef6\uff0c\u4f46\u662f\u4ecd\u5fc5\u987b\u624b\u52a8\u6267\u884c\u7c7b\u578b\u8f6c\u6362\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>for row in rows:     name   = row[0]     shares = int(row[1])     price  = float(row[2]) <\/code><\/pre>\n<p>\u4e5f\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u5217\u8868\u57fa\u672c\u64cd\u4f5c\u4ee5\u66f4\u5de7\u5999\u7684\u65b9\u5f0f\u6765\u6267\u884c\u8fd9\u79cd\u8f6c\u6362\u3002<\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u8f6c\u6362\u51fd\u6570\u540d\u79f0\u7684 Python \u5217\u8868\uff0c\u8fd9\u4e9b\u51fd\u6570\u7528\u6765\u628a\u6bcf\u4e00\u5217\u8f6c\u6362\u6210\u9002\u5f53\u7684\u7c7b\u578b\u3002<\/p>\n<pre><code>&gt;&gt;&gt; types = [str, int, float] &gt;&gt;&gt; <\/code><\/pre>\n<p>\u53ef\u4ee5\u521b\u5efa\u8fd9\u6837\u7684\u5217\u8868\u662f\u56e0\u4e3a\u5728 Python \u4e2d\u4e00\u5207\u7686\u4e00\u7b49\u5bf9\u8c61\u3002\u6240\u4ee5\uff0c\u5982\u679c\u60f3\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u5217\u8868\uff0c\u4e5f\u662f\u53ef\u4ee5\u7684\u3002\u5217\u8868\u4e2d\u521b\u5efa\u7684\u9879\u7528\u4e8e\u5c06\u503c <code>x<\/code> \u8f6c\u6362\u4e3a\u7ed9\u5b9a\u7684\u7c7b\u578b\uff08\u5982\uff1a<code>str(x)<\/code>, <code>int(x)<\/code>, <code>float(x)<\/code>\uff09\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u4ece\u4e0a\u9762\u6587\u4ef6\u7684\u6570\u636e\u4e2d\u8bfb\u53d6\u4e00\u884c\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; import csv &gt;&gt;&gt; f = open('Data\/portfolio.csv') &gt;&gt;&gt; rows = csv.reader(f) &gt;&gt;&gt; headers = next(rows) &gt;&gt;&gt; row = next(rows) &gt;&gt;&gt; row ['AA', '100', '32.20'] &gt;&gt;&gt; <\/code><\/pre>\n<p>\u5982\u524d\u6240\u8ff0\uff0c\u8be5\u884c\u4e0d\u8db3\u4ee5\u8fdb\u884c\u8ba1\u7b97\uff0c\u56e0\u4e3a\u7c7b\u578b\u662f\u9519\u8bef\u7684\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; row[1] * row[2] Traceback (most recent call last):   File \"&lt;stdin&gt;\", line 1, in &lt;module&gt; TypeError: can't multiply sequence by non-int of type 'str' &gt;&gt;&gt; <\/code><\/pre>\n<p>\u4f46\u662f\uff0c\u4e5f\u8bb8\u6570\u636e\u53ef\u4ee5\u4e0e\u5728 <code>types<\/code> \u4e2d\u6307\u5b9a\u7684\u7c7b\u578b\u914d\u5bf9\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; types[1] &lt;type 'int'&gt; &gt;&gt;&gt; row[1] '100' &gt;&gt;&gt; <\/code><\/pre>\n<p>\u5c1d\u8bd5\u8f6c\u6362\u5176\u4e2d\u4e00\u4e2a\u503c\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; types[1](row[1])     # Same as int(row[1]) 100 &gt;&gt;&gt; <\/code><\/pre>\n<p>\u5c1d\u8bd5\u8f6c\u6362\u53e6\u4e00\u4e2a\u503c\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; types[2](row[2])     # Same as float(row[2]) 32.2 &gt;&gt;&gt; <\/code><\/pre>\n<p>\u5c1d\u8bd5\u4f7f\u7528\u8f6c\u6362\u540e\u7684\u503c\u8fdb\u884c\u8ba1\u7b97\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; types[1](row[1])*types[2](row[2]) 3220.0000000000005 &gt;&gt;&gt; <\/code><\/pre>\n<p>\u4f7f\u7528 zip() \u51fd\u6570\u5c06\u5b57\u6bb5\u7ec4\u5408\u5230\u4e00\u8d77\uff0c\u5e76\u4e14\u67e5\u770b\u7ed3\u679c\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; r = list(zip(types, row)) &gt;&gt;&gt; r [(&lt;type 'str'&gt;, 'AA'), (&lt;type 'int'&gt;, '100'), (&lt;type 'float'&gt;,'32.20')] &gt;&gt;&gt; <\/code><\/pre>\n<p>\u6ce8\u610f\u770b\uff0c\u8fd9\u4f1a\u5c06\u7c7b\u578b\u8f6c\u6362\u51fd\u6570\u540d\u79f0\u4e0e\u503c\u914d\u5bf9\u3002\u4f8b\u5982\uff0c<code>int<\/code> \u548c <code>'100'<\/code>\u914d\u5bf9\u3002<\/p>\n<p>\u5982\u679c\u8981\u4e00\u4e2a\u63a5\u4e00\u4e2a\u5730\u5bf9\u6240\u6709\u503c\u8fdb\u884c\u8f6c\u6362\uff0c\u90a3\u4e48\u5408\u5e76\u540e\u7684\u5217\u8868\u5f88\u6709\u7528\u3002\u8bf7\u5c1d\u8bd5\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; converted = [] &gt;&gt;&gt; for func, val in zip(types, row):           converted.append(func(val)) ... &gt;&gt;&gt; converted ['AA', 100, 32.2] &gt;&gt;&gt; converted[1] * converted[2] 3220.0000000000005 &gt;&gt;&gt; <\/code><\/pre>\n<p>\u786e\u4fdd\u4f60\u7406\u89e3\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u6240\u53d1\u751f\u7684\u4e8b\u60c5\u3002\u5728\u5faa\u73af\u4e2d\uff0c<code>func<\/code> \u53d8\u91cf\u662f\u7c7b\u578b\u8f6c\u6362\u51fd\u6570\uff08\u5982<code>str<\/code>, <code>int<\/code>\u7b49 \uff09\u4e4b\u4e00\u4e14 <code>val<\/code> \u53d8\u91cf\u662f\u503c\uff08<code>'AA'<\/code>, <code>'100'<\/code>\uff09\u4e4b\u4e00\u3002\u8868\u8fbe\u5f0f <code>func(val)<\/code>\u8f6c\u6362\u4e00\u4e2a\u503c\uff08\u7c7b\u4f3c\u4e8e\u7c7b\u578b\u8f6c\u6362\uff09\u3002<\/p>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u8f6c\u6362\u4e3a\u5355\u4e2a\u5217\u8868\u63a8\u5bfc\u5f0f\u3002<\/p>\n<pre><code>&gt;&gt;&gt; converted = [func(val) for func, val in zip(types, row)] &gt;&gt;&gt; converted ['AA', 100, 32.2] &gt;&gt;&gt; <\/code><\/pre>\n<h3>\u7ec3\u4e60 2.25\uff1a\u521b\u5efa\u5b57\u5178<\/h3>\n<p>\u8fd8\u8bb0\u5f97\u5982\u679c\u6709\u4e00\u4e2a\u952e\u548c\u503c\u7684\u5e8f\u5217\uff0c\u5982\u4f55\u4f7f\u7528<code>dict()<\/code> \u51fd\u6570\u8f7b\u677e\u5730\u521b\u5efa\u5b57\u5178\u5417\uff1f\u8ba9\u6211\u4eec\u4ece\u5217\u6807\u9898\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\u5427\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; headers ['name', 'shares', 'price'] &gt;&gt;&gt; converted ['AA', 100, 32.2] &gt;&gt;&gt; dict(zip(headers, converted)) {'price': 32.2, 'name': 'AA', 'shares': 100} &gt;&gt;&gt; <\/code><\/pre>\n<p>\u5f53\u7136\uff0c\u5982\u679c\u4f60\u7cbe\u901a\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u4e00\u6b65\u5b8c\u6210\u6574\u4e2a\u8f6c\u6362\u3002<\/p>\n<pre><code>&gt;&gt;&gt; { name: func(val) for name, func, val in zip(headers, types, row) } {'price': 32.2, 'name': 'AA', 'shares': 100} &gt;&gt;&gt; <\/code><\/pre>\n<h3>\u7ec3\u4e60 2.26\uff1a\u5168\u5c40<\/h3>\n<p>\u4f7f\u7528\u672c\u7ec3\u4e60\u4e2d\u7684\u6280\u672f\uff0c\u53ef\u4ee5\u7f16\u5199\u8bed\u53e5\uff0c\u8f7b\u677e\u5730\u5c06\u51e0\u4e4e\u4efb\u4f55\u9762\u5411\u5217\u7684\u6570\u636e\u6587\u4ef6\u4e2d\u7684\u5b57\u6bb5\u8f6c\u6362\u4e3a Python \u5b57\u5178\u3002<\/p>\n<p>\u4e3a\u4e86\u8bf4\u660e\uff0c\u5047\u8bbe\u4f60\u50cf\u4e0b\u9762\u8fd9\u6837\u4ece\u4e0d\u540c\u7684\u6570\u636e\u6587\u4ef6\u8bfb\u53d6\u6570\u636e\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; f = open('Data\/dowstocks.csv') &gt;&gt;&gt; rows = csv.reader(f) &gt;&gt;&gt; headers = next(rows) &gt;&gt;&gt; row = next(rows) &gt;&gt;&gt; headers ['name', 'price', 'date', 'time', 'change', 'open', 'high', 'low', 'volume'] &gt;&gt;&gt; row ['AA', '39.48', '6\/11\/2007', '9:36am', '-0.18', '39.67', '39.69', '39.45', '181800'] &gt;&gt;&gt; <\/code><\/pre>\n<p>\u8ba9\u6211\u4eec\u4f7f\u7528\u7c7b\u4f3c\u7684\u6280\u5de7\u6765\u8f6c\u6362\u5b57\u6bb5\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; types = [str, float, str, str, float, float, float, float, int] &gt;&gt;&gt; converted = [func(val) for func, val in zip(types, row)] &gt;&gt;&gt; record = dict(zip(headers, converted)) &gt;&gt;&gt; record {'volume': 181800, 'name': 'AA', 'price': 39.48, 'high': 39.69, 'low': 39.45, 'time': '9:36am', 'date': '6\/11\/2007', 'open': 39.67, 'change': -0.18} &gt;&gt;&gt; record['name'] 'AA' &gt;&gt;&gt; record['price'] 39.48 &gt;&gt;&gt; <\/code><\/pre>\n<p>\u9644\u52a0\u9898\uff1a\u5982\u4f55\u4fee\u6539\u672c\u793a\u4f8b\u4ee5\u8fdb\u4e00\u6b65\u89e3\u6790 <code>date<\/code> \u6761\u76ee\u5230\u5143\u7ec4\u4e2d\uff0c\u5982<code>(6, 11, 2007)<\/code>\uff1f<\/p>\n<p>\u8bf7\u82b1\u4e00\u4e9b\u65f6\u95f4\u4ed4\u7ec6\u601d\u8003\u4f60\u5728\u7ec3\u4e60\u4e2d\u6240\u505a\u7684\u4e8b\u60c5\u3002\u6211\u4eec\u7a0d\u540e\u4f1a\u518d\u6b21\u8ba8\u8bba\u8fd9\u4e9b\u60f3\u6cd5\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>1<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5395466\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : sugarkeek2 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5b66\u4e60\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\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\/388113"}],"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=388113"}],"version-history":[{"count":1,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/388113\/revisions"}],"predecessor-version":[{"id":388116,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/388113\/revisions\/388116"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=388113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=388113"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=388113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}