{"id":352743,"date":"2021-02-20T14:02:36","date_gmt":"2021-02-20T06:02:36","guid":{"rendered":"http:\/\/4563.org\/?p=352743"},"modified":"2021-02-20T14:02:36","modified_gmt":"2021-02-20T06:02:36","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_02_containers","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=352743","title":{"rendered":"\u7ffb\u8bd1\uff1a\u300a\u5b9e\u7528\u7684 Python \u7f16\u7a0b\u300b02_02_Containers"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u7ffb\u8bd1\uff1a\u300a\u5b9e\u7528\u7684 Python \u7f16\u7a0b\u300b02_02_Containers               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : codists <\/span>  <span><i><\/i> 1<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<h1>2.2 \u5bb9\u5668<\/h1>\n<p> <\/p>\n<p>\u672c\u8282\u8ba8\u8bba\u5217\u8868\uff08 list \uff09\uff0c\u5b57\u5178\uff08 dict \uff09\u548c\u96c6\u5408\uff08 set \uff09\u3002<\/p>\n<h3>\u6982\u8ff0<\/h3>\n<p>\u901a\u5e38\uff0c\u7a0b\u5e8f\u5fc5\u987b\u5904\u7406\u8bb8\u591a\u5bf9\u8c61\u3002<\/p>\n<ul>\n<li>\u80a1\u7968\u7684\u6295\u8d44\u7ec4\u5408<\/li>\n<li>\u80a1\u7968\u4ef7\u683c\u8868<\/li>\n<\/ul>\n<p>\u8fd9\u91cc\u6709\u4e09\u79cd\u4e3b\u8981\u7684\u9009\u62e9\uff08\u8bd1\u6ce8\uff1a\u6570\u636e\u7ed3\u6784\uff09\u53ef\u4ee5\u4f7f\u7528\uff1a<\/p>\n<ul>\n<li>\u5217\u8868\u3002\u6709\u5e8f\u7684\u6570\u636e\u3002<\/li>\n<li>\u5b57\u5178\u3002\u65e0\u5e8f\u7684\u6570\u636e\u3002<\/li>\n<li>\u96c6\u5408\u3002\u4e92\u5f02\u4e14\u65e0\u5e8f\u7684\u6570\u636e\u3002<\/li>\n<\/ul>\n<h3>\u628a\u5217\u8868\u5f53\u4f5c\u5bb9\u5668<\/h3>\n<p>\u5f53\u6570\u636e\u987a\u5e8f\u5f88\u91cd\u8981\u65f6\uff0c\u8bf7\u4f7f\u7528\u5217\u8868\u3002\u8bb0\u4f4f\uff0c\u5217\u8868\u53ef\u4ee5\u5b58\u50a8\u4efb\u4f55\u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u4f8b\u5982\uff0c\u5305\u542b\u5143\u7ec4\u7684\u5217\u8868\uff1a<\/p>\n<pre><code>portfolio = [     ('GOOG', 100, 490.1),     ('IBM', 50, 91.3),     ('CAT', 150, 83.44) ]  portfolio[0]            # ('GOOG', 100, 490.1) portfolio[2]            # ('CAT', 150, 83.44) <\/code><\/pre>\n<h3>\u5217\u8868\u6784\u5efa<\/h3>\n<p>\u4ece\u96f6\u5f00\u59cb\u6784\u5efa\u5217\u8868\u3002<\/p>\n<pre><code>records = []  # Initial empty list  # Use .append() to add more items records.append(('GOOG', 100, 490.10)) records.append(('IBM', 50, 91.3)) ... <\/code><\/pre>\n<p>\u4ece\u6587\u4ef6\u8bfb\u53d6\u8bb0\u5f55\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>records = []  # Initial empty list  with open('Data\/portfolio.csv', 'rt') as f:     next(f) # Skip header     for line in f:         row = line.split(',')         records.append((row[0], int(row[1]), float(row[2]))) <\/code><\/pre>\n<h3>\u628a\u5b57\u5178\u5f53\u4f5c\u5bb9\u5668<\/h3>\n<p>\u5982\u679c\u8981\u5feb\u901f\u968f\u673a\u67e5\u627e\uff08\u901a\u8fc7\u952e\u540d\uff09\uff0c\u90a3\u4e48\u5b57\u5178\u5f88\u6709\u7528\u3002\u4f8b\u5982\uff0c\u80a1\u7968\u4ef7\u683c\u5b57\u5178\uff1a<\/p>\n<pre><code>prices = {    'GOOG': 513.25,    'CAT': 87.22,    'IBM': 93.37,    'MSFT': 44.12 } <\/code><\/pre>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u7b80\u5355\u7684\u67e5\u627e\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; prices['IBM'] 93.37 &gt;&gt;&gt; prices['GOOG'] 513.25 &gt;&gt;&gt; <\/code><\/pre>\n<h3>\u5b57\u5178\u6784\u5efa<\/h3>\n<p>\u4ece\u96f6\u5f00\u59cb\u6784\u5efa\u5b57\u5178\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>prices = {} # Initial empty dict  # Insert new items prices['GOOG'] = 513.25 prices['CAT'] = 87.22 prices['IBM'] = 93.37 <\/code><\/pre>\n<p>\u4ece\u6587\u4ef6\u5185\u5bb9\u586b\u5145\u5b57\u5178\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>prices = {} # Initial empty dict  with open('Data\/prices.csv', 'rt') as f:     for line in f:         row = line.split(',')         prices[row[0]] = float(row[1]) <\/code><\/pre>\n<p>\u6ce8\u610f\uff1a\u5982\u679c\u662f\u5728 <code>Data\/prices.csv<\/code> \u6587\u4ef6\u4e0a\u5c1d\u8bd5\u6b64\u64cd\u4f5c\uff0c\u4f1a\u53d1\u73b0\u51e0\u4e4e\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u2014\u2014\u4f46\u662f\uff0c\u5728\u672b\u5c3e\u6709\u4e00\u4e2a\u7a7a\u884c\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u4e86\u3002\u9700\u8981\u627e\u51fa\u4e00\u4e9b\u65b9\u6cd5\u6765\u4fee\u6539\u4ee3\u7801\u4ee5\u89e3\u51b3\u6b64\u95ee\u9898\uff08\u53c2\u89c1\u7ec3\u4e60 2.6 \uff09\u3002<\/p>\n<h3>\u5b57\u5178\u67e5\u627e<\/h3>\n<p>\u6d4b\u8bd5\u952e\u662f\u5426\u5b58\u5728\uff1a<\/p>\n<pre><code>if key in d:     # YES else:     # NO <\/code><\/pre>\n<p>\u53ef\u4ee5\u67e5\u627e\u53ef\u80fd\u4e0d\u5b58\u5728\u7684\u503c\uff0c\u5e76\u5728\u503c\u4e0d\u5b58\u5728\u7684\u60c5\u51b5\u4e0b\u63d0\u4f9b\u9ed8\u8ba4\u503c\u3002<\/p>\n<pre><code>name = d.get(key, default) <\/code><\/pre>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; prices.get('IBM', 0.0) 93.37 &gt;&gt;&gt; prices.get('SCOX', 0.0) 0.0 &gt;&gt;&gt; <\/code><\/pre>\n<h3>\u7ec4\u5408\u952e<\/h3>\n<p>\u5728 \uff30 ython \u4e2d\uff0c\u51e0\u4e4e\u4efb\u4f55\u7c7b\u578b\u7684\u503c\u90fd\u53ef\u4ee5\u7528\u4f5c\u5b57\u5178\u7684\u952e\u3002\u5b57\u5178\u7684\u952e\u5fc5\u987b\u662f\u4e0d\u53ef\u53d8\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u5143\u7ec4\uff1a<\/p>\n<pre><code>holidays = {   (1, 1) : 'New Years',   (3, 14) : 'Pi day',   (9, 13) : \"Programmer's day\", } <\/code><\/pre>\n<p>\u7136\u540e\u8bbf\u95ee\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; holidays[3, 14] 'Pi day' &gt;&gt;&gt; <\/code><\/pre>\n<p><em>\u5217\u8868\uff0c\u96c6\u5408\u6216\u8005\u5176\u5b83\u5b57\u5178\u90fd\u4e0d\u80fd\u7528\u4f5c\u5b57\u5178\u7684\u952e\uff0c\u56e0\u4e3a\u5217\u8868\u548c\u5b57\u5178\uff08\u8bd1\u6ce8\uff1a\u96c6\u5408\u4e5f\u662f\u4f7f\u7528\u54c8\u5e0c\u6280\u672f\u5b9e\u73b0\u7684\uff09\u662f\u53ef\u53d8\u7684\u3002<\/em><\/p>\n<h3>\u96c6\u5408<\/h3>\n<p>\u96c6\u5408\u662f\u4e92\u5f02\u4e14\u65e0\u5e8f\u7684\u6570\u636e\u3002<\/p>\n<pre><code>tech_stocks = { 'IBM','AAPL','MSFT' } # Alternative syntax tech_stocks = set(['IBM', 'AAPL', 'MSFT']) <\/code><\/pre>\n<p>\u96c6\u5408\u5bf9\u4e8e\u6210\u5458\u5173\u7cfb\u6d4b\u8bd5\u5f88\u6709\u7528\u3002<\/p>\n<pre><code>&gt;&gt;&gt; tech_stocks set(['AAPL', 'IBM', 'MSFT']) &gt;&gt;&gt; 'IBM' in tech_stocks True &gt;&gt;&gt; 'FB' in tech_stocks False &gt;&gt;&gt; <\/code><\/pre>\n<p>\u96c6\u5408\u5bf9\u4e8e\u6d88\u9664\u91cd\u590d\u4e5f\u5f88\u6709\u7528\u3002<\/p>\n<pre><code>names = ['IBM', 'AAPL', 'GOOG', 'IBM', 'GOOG', 'YHOO']  unique = set(names) # unique = set(['IBM', 'AAPL','GOOG','YHOO']) <\/code><\/pre>\n<p>\u5176\u5b83\u96c6\u5408\u64cd\u4f5c\uff1a<\/p>\n<pre><code>names.add('CAT')        # Add an item names.remove('YHOO')    # Remove an item  s1 | s2                 # Set union s1 &amp; s2                 # Set intersection s1 - s2                 # Set difference <\/code><\/pre>\n<h2>\u7ec3\u4e60<\/h2>\n<p>\u5728\u8fd9\u4e9b\u7ec3\u4e60\u4e2d\uff0c\u4f60\u5f00\u59cb\u6784\u5efa\u7684\u7a0b\u5e8f\u662f\u672c\u8bfe\u7a0b\u5269\u4f59\u90e8\u5206\u4f7f\u7528\u7684\u4e3b\u8981\u7a0b\u5e8f\u4e4b\u4e00\u3002\u8bf7\u5728 <code>Work\/report.py<\/code> \u6587\u4ef6\u4e2d\u5de5\u4f5c\u3002<\/p>\n<h3>\u7ec3\u4e60 2.4\uff1a\u5305\u542b\u5143\u7ec4\u7684\u5217\u8868<\/h3>\n<p><code>Data\/portfolio.csv<\/code> \u6587\u4ef6\u5305\u542b\u6295\u8d44\u7ec4\u5408\u4e2d\u7684\u80a1\u7968\u5217\u8868\u3002\u5728 \u7ec3\u4e60 1.30 \u4e2d\uff0c\u4f60\u7f16\u5199\u4e86\u4e00\u4e2a\u8bfb\u53d6\u8be5\u6587\u4ef6\u5e76\u6267\u884c\u7b80\u5355\u8ba1\u7b97\u7684 <code>portfolio_cost(filename)<\/code> \u51fd\u6570\u3002<\/p>\n<p>\u4ee3\u7801\u770b\u8d77\u6765\u5e94\u8be5\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n<pre><code># pcost.py  import csv  def portfolio_cost(filename):     '''Computes the total cost (shares*price) of a portfolio file'''     total_cost = 0.0      with open(filename, 'rt') as f:         rows = csv.reader(f)         headers = next(rows)         for row in rows:             nshares = int(row[1])             price = float(row[2])             total_cost += nshares * price     return total_cost <\/code><\/pre>\n<p>\u8bf7\u4f7f\u7528\u8fd9\u4e9b\u4ee3\u7801\u4f5c\u4e3a\u6307\u5bfc\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6 <code>report.py<\/code> \u3002\u5728 <code>report.py<\/code> \u6587\u4ef6\u4e2d\uff0c\u5b9a\u4e49 <code>read_portfolio(filename)<\/code> \u51fd\u6570\uff0c\u8be5\u51fd\u6570\u6253\u5f00 <code>Data\/portfolio.csv<\/code> \u6587\u4ef6\u5e76\u5c06\u5176\u8bfb\u5165\u5230\u5305\u542b\u5143\u7ec4\u7684\u5217\u8868\u4e2d\u3002\u4e3a\u6b64\uff0c\u4f60\u9700\u8981\u5bf9\u4e0a\u9762\u7684\u4ee3\u7801\u505a\u4e00\u4e9b\u5c0f\u4fee\u6539\u3002<\/p>\n<p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u6700\u521d\u8bbe\u4e3a\u7a7a\u5217\u8868\u7684\u53d8\u91cf\uff0c\u800c\u4e0d\u662f\u5b9a\u4e49 <code>total_cost = 0<\/code>\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>portfolio = [] <\/code><\/pre>\n<p>\u63a5\u7740\uff0c\u628a\u6bcf\u4e00\u884c\u51c6\u786e\u5730\u5b58\u50a8\u5230\u5143\u7ec4\u4e2d\uff08\u5c31\u50cf\u5728\u4e0a\u6b21\u7684\u7ec3\u4e60\u4e2d\u505a\u7684\u90a3\u6837\uff09\uff0c\u7136\u540e\u628a\u5143\u7ec4\u8ffd\u52a0\u5230\u5217\u8868\u4e2d\uff0c\u800c\u4e0d\u662f\u5408\u8ba1\u603b\u7684\u8d39\u7528\u3002<\/p>\n<pre><code>for row in rows:     holding = (row[0], int(row[1]), float(row[2]))     portfolio.append(holding) <\/code><\/pre>\n<p>\u6700\u540e\uff0c\u8fd4\u56de\u5f97\u5230\u7684<code>portfolio<\/code> \u5217\u8868\u3002<\/p>\n<p>\u8bf7\u4ea4\u4e92\u5f0f\u5730\u8bd5\u9a8c\u51fd\u6570\uff08\u63d0\u9192\uff0c\u8981\u6267\u884c\u6b64\u64cd\u4f5c\uff0c\u9996\u5148\u9700\u8981\u5728\u89e3\u91ca\u5668\u8fd0\u884c <code>report.py<\/code> \u7a0b\u5e8f\uff09\u3002<\/p>\n<p><em>\u63d0\u793a\uff1a\u5f53\u5728\u7ec8\u7aef\u6267\u884c\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u8bf7\u4f7f\u7528 <code>-i<\/code> \u53c2\u6570\u3002<\/em><\/p>\n<pre><code>&gt;&gt;&gt; portfolio = read_portfolio('Data\/portfolio.csv') &gt;&gt;&gt; portfolio [('AA', 100, 32.2), ('IBM', 50, 91.1), ('CAT', 150, 83.44), ('MSFT', 200, 51.23),     ('GE', 95, 40.37), ('MSFT', 50, 65.1), ('IBM', 100, 70.44)] &gt;&gt;&gt; &gt;&gt;&gt; portfolio[0] ('AA', 100, 32.2) &gt;&gt;&gt; portfolio[1] ('IBM', 50, 91.1) &gt;&gt;&gt; portfolio[1][1] 50 &gt;&gt;&gt; total = 0.0 &gt;&gt;&gt; for s in portfolio:         total += s[1] * s[2]  &gt;&gt;&gt; print(total) 44671.15 &gt;&gt;&gt; <\/code><\/pre>\n<p>\u521b\u5efa\u7684\u5305\u542b\u5143\u7ec4\u7684\u5217\u8868\u975e\u5e38\u7c7b\u4f3c\u4e8e\u4e8c\u7ef4\uff08 2-\uff24\uff09\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u8bf8\u5982 <code>portfolio[row][column]<\/code> \uff08 <code>row<\/code> \u548c<code>column<\/code> \u662f\u6574\u6570\uff09\u7684\u67e5\u627e\u6765\u8bbf\u95ee\u7279\u5b9a\u7684\u5217\u548c\u884c\u3002<\/p>\n<p>\u4e5f\u5c31\u662f\u8bf4\uff0c\u53ef\u4ee5\u4f7f\u7528\u50cf\u4e0b\u9762\u8fd9\u6837\u7684\u8bed\u53e5\u91cd\u5199\u6700\u540e\u7684 for \u5faa\u73af\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; total = 0.0 &gt;&gt;&gt; for name, shares, price in portfolio:             total += shares*price  &gt;&gt;&gt; print(total) 44671.15 &gt;&gt;&gt; <\/code><\/pre>\n<h3>\u7ec3\u4e60 2.5\uff1a\u5305\u542b\u5b57\u5178\u7684\u5217\u8868<\/h3>\n<p>\u4f7f\u7528\u5b57\u5178\uff08\u800c\u4e0d\u662f\u5143\u7ec4\uff09\u4fee\u6539\u5728\u7ec3\u4e60 2.4 \u4e2d\u7f16\u5199\u7684\u51fd\u6570\u6765\u8868\u793a\u6295\u8d44\u7ec4\u5408\u4e2d\u7684\u80a1\u7968\u3002\u5728\u5b57\u5178\u4e2d\uff0c\u4f7f\u7528\u5b57\u6bb5\u540d &#8220;name&#8221;, &#8220;shares&#8221; \u548c &#8220;price&#8221; \u6765\u8868\u793a\u8f93\u5165\u6587\u4ef6\u4e2d\u7684\u4e0d\u540c\u5217\u3002<\/p>\n<p>\u4ee5\u4e0e\u7ec3\u4e60 2.4 \u4e2d\u76f8\u540c\u7684\u65b9\u5f0f\u8bd5\u9a8c\u8fd9\u4e2a\u65b0\u7684\u51fd\u6570\u3002 <\/p>\n<pre><code>&gt;&gt;&gt; portfolio = read_portfolio('Data\/portfolio.csv') &gt;&gt;&gt; portfolio [{'name': 'AA', 'shares': 100, 'price': 32.2}, {'name': 'IBM', 'shares': 50, 'price': 91.1},     {'name': 'CAT', 'shares': 150, 'price': 83.44}, {'name': 'MSFT', 'shares': 200, 'price': 51.23},     {'name': 'GE', 'shares': 95, 'price': 40.37}, {'name': 'MSFT', 'shares': 50, 'price': 65.1},     {'name': 'IBM', 'shares': 100, 'price': 70.44}] &gt;&gt;&gt; portfolio[0] {'name': 'AA', 'shares': 100, 'price': 32.2} &gt;&gt;&gt; portfolio[1] {'name': 'IBM', 'shares': 50, 'price': 91.1} &gt;&gt;&gt; portfolio[1]['shares'] 50 &gt;&gt;&gt; total = 0.0 &gt;&gt;&gt; for s in portfolio:         total += s['shares']*s['price']  &gt;&gt;&gt; print(total) 44671.15 &gt;&gt;&gt; <\/code><\/pre>\n<p>\u5728\u8fd9\u91cc\u53ef\u4ee5\u770b\u5230\uff0c\u6bcf\u4e2a\u6761\u76ee\u7684\u4e0d\u540c\u5b57\u6bb5\u662f\u901a\u8fc7\u952e\u540d\u6765\u8bbf\u95ee\u7684\uff0c\u800c\u4e0d\u662f\u6570\u5b57\u7c7b\u578b\u7684\u5217\u53f7\u3002\u8fd9\u901a\u5e38\u662f\u9996\u9009\u65b9\u5f0f\uff0c\u56e0\u4e3a\u8fd9\u6837\u5f97\u5230\u7684\u4ee3\u7801\u5728\u4ee5\u540e\u6613\u4e8e\u9605\u8bfb\u3002<\/p>\n<p>\u67e5\u770b\u5927\u578b\u7684\u5b57\u5178\u6216\u8005\u5217\u8868\u53ef\u80fd\u4f1a\u5f88\u6df7\u4e71\u3002\u8981\u4f7f\u8c03\u8bd5\u7684\u8f93\u51fa\u53d8\u5f97\u6574\u6d01\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528 <code>pprint()<\/code> \u51fd\u6570\u3002<\/p>\n<pre><code>&gt;&gt;&gt; from pprint import pprint &gt;&gt;&gt; pprint(portfolio) [{'name': 'AA', 'price': 32.2, 'shares': 100},     {'name': 'IBM', 'price': 91.1, 'shares': 50},     {'name': 'CAT', 'price': 83.44, 'shares': 150},     {'name': 'MSFT', 'price': 51.23, 'shares': 200},     {'name': 'GE', 'price': 40.37, 'shares': 95},     {'name': 'MSFT', 'price': 65.1, 'shares': 50},     {'name': 'IBM', 'price': 70.44, 'shares': 100}] &gt;&gt;&gt; <\/code><\/pre>\n<h3>\u7ec3\u4e60 2.6\uff1a\u628a\u5b57\u5178\u5f53\u4f5c\u5bb9\u5668<\/h3>\n<p>\u5728\u4f7f\u7528\u7d22\u5f15\u800c\u4e0d\u662f\u6570\u5b57\u67e5\u627e\u67d0\u5143\u7d20\u7684\u5730\u65b9\uff0c\u5b57\u5178\u662f\u4e00\u79cd\u7528\u6765\u8ddf\u8e2a\u5143\u7d20\u7684\u5f88\u6709\u7528\u7684\u65b9\u5f0f\u3002\u5728 Python shell \u4e2d\uff0c\u5c1d\u8bd5\u4f7f\u7528\u5b57\u5178\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; prices = { } &gt;&gt;&gt; prices['IBM'] = 92.45 &gt;&gt;&gt; prices['MSFT'] = 45.12 &gt;&gt;&gt; prices ... look at the result ... &gt;&gt;&gt; prices['IBM'] 92.45 &gt;&gt;&gt; prices['AAPL'] ... look at the result ... &gt;&gt;&gt; 'AAPL' in prices False &gt;&gt;&gt; <\/code><\/pre>\n<p>\u8be5 <code>Data\/prices.csv<\/code> \u6587\u4ef6\u5305\u542b\u4e00\u7cfb\u5217\u5e26\u6709\u80a1\u7968\u4ef7\u683c\u7684\u884c\uff0c\u770b\u8d77\u6765\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n<pre><code>\"AA\",9.22 \"AXP\",24.85 \"BA\",44.85 \"BAC\",11.27 \"C\",3.72 ... <\/code><\/pre>\n<p>\u7f16\u5199 <code>read_prices(filename)<\/code>\u51fd\u6570\u5c06\u8bf8\u5982\u6b64\u7c7b\u7684\u4ef7\u683c\u96c6\u5408\u8bfb\u53d6\u5230\u5b57\u5178\u4e2d\uff0c\u5b57\u5178\u7684\u952e\u4ee3\u8868\u80a1\u7968\u7684\u540d\u5b57\uff0c\u5b57\u5178\u7684\u503c\u4ee3\u8868\u80a1\u7968\u7684\u4ef7\u683c\u3002<\/p>\n<p>\u4e3a\u6b64\uff0c\u4ece\u7a7a\u5b57\u5178\u5f00\u59cb\uff0c\u5e76\u4e14\u50cf\u4e0a\u9762\u505a\u7684\u90a3\u6837\u5f00\u59cb\u63d2\u5165\u503c\u3002\u4f46\u662f\uff0c\u73b0\u5728\u6b63\u5728\u4ece\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u503c\u3002<\/p>\n<p>\u6211\u4eec\u5c06\u4f7f\u7528\u8be5\u6570\u636e\u7ed3\u6784\u5feb\u901f\u67e5\u627e\u7ed9\u5b9a\u540d\u79f0\u7684\u80a1\u7968\u7684\u4ef7\u683c\u3002<\/p>\n<p>\u8fd9\u90e8\u5206\u9700\u8981\u4e00\u4e9b\u5c0f\u6280\u5de7\u3002\u9996\u5148\uff0c\u786e\u4fdd\u50cf\u4e4b\u524d\u505a\u7684\u90a3\u6837\u4f7f\u7528 <code>csv<\/code> \u6a21\u5757\u2014\u2014\u65e0\u9700\u5728\u8fd9\u91cc\u91cd\u590d\u53d1\u660e\u8f6e\u5b50\u3002<\/p>\n<pre><code>&gt;&gt;&gt; import csv &gt;&gt;&gt; f = open('Data\/prices.csv', 'r') &gt;&gt;&gt; rows = csv.reader(f) &gt;&gt;&gt; for row in rows:         print(row)   ['AA', '9.22'] ['AXP', '24.85'] ... [] &gt;&gt;&gt; <\/code><\/pre>\n<p>\u53e6\u5916\u4e00\u4e2a\u5c0f\u9ebb\u70e6\u662f <code>Data\/prices.csv<\/code> \u6587\u4ef6\u53ef\u80fd\u6709\u4e00\u4e9b\u7a7a\u884c\u5728\u91cc\u9762\u3002\u6ce8\u610f\u4e0a\u9762\u6570\u636e\u7684\u6700\u540e\u4e00\u884c\u662f\u4e00\u4e2a\u7a7a\u5217\u8868\u2014\u2014\u610f\u5473\u5728\u90a3\u4e00\u884c\u6ca1\u6709\u6570\u636e\u3002<\/p>\n<p>\u8fd9\u6709\u53ef\u80fd\u5bfc\u81f4\u4f60\u7684\u7a0b\u5e8f\u56e0\u4e3a\u5f02\u5e38\u800c\u7ec8\u6b62\u3002\u914c\u60c5\u4f7f\u7528 <code>try<\/code> \u548c <code>except<\/code> \u8bed\u53e5\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\u3002\u601d\u8003\uff1a\u4f7f\u7528 <code>if<\/code> \u8bed\u53e5\u6765\u9632\u8303\u9519\u8bef\u7684\u6570\u636e\u662f\u5426\u4f1a\u66f4\u597d\uff1f<\/p>\n<p>\u7f16\u5199\u5b8c <code>read_prices()<\/code> \u51fd\u6570\uff0c\u8bf7\u4ea4\u4e92\u5f0f\u5730\u6d4b\u8bd5\u5b83\u5e76\u786e\u4fdd\u5176\u6b63\u5e38\u5de5\u4f5c\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; prices = read_prices('Data\/prices.csv') &gt;&gt;&gt; prices['IBM'] 106.28 &gt;&gt;&gt; prices['MSFT'] 20.89 &gt;&gt;&gt; <\/code><\/pre>\n<h3>\u7ec3\u4e60 2.7\uff1a\u770b\u770b\u4f60\u662f\u5426\u53ef\u4ee5\u9000\u4f11<\/h3>\n<p>\u901a\u8fc7\u6dfb\u52a0\u4e00\u4e9b\u8ba1\u7b97\u76c8\u4e8f\u7684\u8bed\u53e5\u5230 <code>report.py<\/code> \u7a0b\u5e8f\uff0c\u5c06\u6240\u6709\u7684\u5de5\u4f5c\u8054\u7cfb\u5230\u4e00\u8d77\u3002\u8fd9\u4e9b\u8bed\u53e5\u5e94\u8be5\u91c7\u7528\u5728\u7ec3\u4e60 2.5 \u4e2d\u5b58\u50a8\u80a1\u7968\u540d\u79f0\u7684\u5217\u8868\uff0c\u4ee5\u53ca\u5728\u7ec3\u4e60 2.6 \u4e2d\u5b58\u50a8\u80a1\u7968\u4ef7\u683c\u7684\u5b57\u5178\uff0c\u5e76\u8ba1\u7b97\u6295\u8d44\u7ec4\u5408\u7684\u5f53\u524d\u503c\u4ee5\u53ca\u76c8\u4e8f\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>3<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5338043\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : oakcdrom <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u54e5\u4eec\uff0c\u5565\u65f6\u5019\u80fd\u6574\u4e2a\u6839\u636e\u9879\u76ee\u6765\u5b66\u4e60 python \u554a\uff0c\u6bd4\u5982\u57fa\u4e8e flask \u5f00\u53d1\u4e2a\u535a\u5ba2\uff0c\u524d\u540e\u7aef\uff0c\u4ece\u5934\u8bb2\u5230\u5c3e\uff0c\u9700\u8981\u54ea\u4e9b\u6a21\u5757\uff0c\u8be5\u600e\u4e48\u8bbe\u8ba1\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5338044\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : codists <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @oakcdrom <br \/>1 \u3001\u6839\u636e\u9879\u76ee\u5b66\u4e60\u786e\u5b9e\u4e5f\u662f\u5f88\u4e0d\u9519\u7684\u4e00\u79cd\u5b66\u4e60\u65b9\u5f0f\u3002\u4f46\u662f\u5982\u679c\u6839\u636e\u9879\u76ee\u540c\u65f6\u8bb2\u89e3 Python, Flask, \u524d\u7aef\u7b49\u7b49\u3002\u53ef\u80fd\u4e0d\u5927\u73b0\u5b9e\uff0c\u56e0\u4e3a\u6240\u6d89\u53ca\u7684\u5185\u5bb9\u592a\u591a\u4e86\u3002<br \/>2 \u3001\u4f60\u662f\u5426\u662f\u60f3\u5b66\u4e60 flask \u6216\u8005\u5176\u5b83\u6846\u67b6\uff1f\u5982\u679c\u662f\u7684\u8bdd\uff0c\u53ef\u4ee5\u770b\u4e0b\u300a Flask Web \u5f00\u53d1\uff1a\u57fa\u4e8e Python \u7684 Web \u5e94\u7528\u5f00\u53d1\u5b9e\u6218\u300b\u8fd9\u672c\u4e66\u3002<br \/>3 \u3001\u65e0\u8bba\u5b66\u4e60\u4ec0\u4e48\uff0c\u9009\u62e9\u4e00\u4e2a\u6559\u7a0b\u5373\u53ef\uff0c\u7136\u540e\u9759\u4e0b\u5fc3\u53bb\u5b66\u4e60\u3002\u5982\u679c\u4f60\u5728\u5b66\u4e60\u7684\u8fc7\u7a0b\u4e2d\u9047\u5230\u95ee\u9898\uff0c\u4e5f\u53ef\u4ee5\u7559\u8a00\uff0c\u5927\u5bb6\u4e00\u8d77\u4ea4\u6d41\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5338045\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : oakcdrom <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @codists \u524d\u7aef\u53ef\u4ee5\u4e0d\u7528\u8bb2\uff0c\u6bd5\u7adf\u6709\u5176\u4ed6\u7684\u6559\u7a0b\uff0c\u5c31\u8bb2\u8bb2\u524d\u540e\u7aef\u600e\u4e48\u914d\u5408\u5c31\u5b8c\u4e86\uff0c\u524d\u7aef\u754c\u9762\u5c55\u793a\uff0c\u540e\u7aef\u529f\u80fd\u5b9e\u73b0\u3002\u3002\u5dee\u4e0d\u591a\u662f\u8fd9\u4e2a\u610f\u601d\uff0c\u54ce\uff0c\u6211\u83dc\u7684\u72e0\uff0c\u5165\u95e8\u6559\u7a0b\u4e5f\u662f\u770b\u7684\u534a\u74f6\u6c34\uff0c\u770b\u4f3c\u61c2\u4e86\u70b9\uff0c\u53c8\u770b\u4f3c\u4ec0\u4e48\u4e5f\u4e0d\u4f1a\u3002\u3002<\/p>\n<p>\u6211\u53bb\u770b\u770b\u8fd9\u4e2a\u4e66\uff0c\u8c22\u8c22                                                            <\/p><\/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\/352743"}],"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=352743"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/352743\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=352743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=352743"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=352743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}