In [1]:
    
prices = {
    'ACME': 45.23,
    'AAPL': 612.78,
    'IBM': 205.55,
    'HPQ': 37.20,
    'FB': 10.75
}
# Make a dictionary of all prices over 200
p1 = {key: value for key, value in prices.items() if value > 200}
# Make a dictionary of tech stocks
tech_names = {'AAPL', 'IBM', 'HPQ', 'MSFT'}
p2 = {key: value for key, value in prices.items() if key in tech_names}
    
In [2]:
    
print(p1,'\n',p2)
    
    
In [4]:
    
p3 = dict((key, value) for key, value in prices.items() if value > 200)
print(p3)
    
    
In [8]:
    
# Make a dictionary of tech stocks
tech_names = {'AAPL', 'IBM', 'HPQ', 'MSFT'}
p4 = {key: prices[key] for key in prices.keys() & tech_names}
print(p4)
    
    
In [10]:
    
# p4 = {key: prices[key] for key in prices.keys() and tech_names}
    
In [11]:
    
prices.keys()
    
    Out[11]:
In [12]:
    
tech_names
    
    Out[12]:
In [16]:
    
type(prices.keys()) == type(tech_names)
    
    Out[16]:
In [17]:
    
prices.keys() & tech_names
    
    Out[17]:
In [18]:
    
type(prices.keys() & tech_names)
    
    Out[18]:
In [1]:
    
from collections import namedtuple
Subsciber = namedtuple('Subscriber',['addr','joined'])
sub = Subsciber('jonesy@exit.com','2012-10-19')
sub
    
    Out[1]:
In [2]:
    
sub.addr
    
    Out[2]:
In [6]:
    
sub.joined
    
    Out[6]:
In [7]:
    
len(sub)
    
    Out[7]:
In [9]:
    
addr, joined = sub
print(addr,'\n',joined)
    
    
In [11]:
    
def compute_cost(records):
    total = 0.0
    for rec in records:
        total += rec[1] * rec[2]
    return total
    
In [12]:
    
Stock = namedtuple('Stock',['name','shares','price'])
def compute_cost2(records):
    total = 0.0
    for rec in records:
        s = Stock(*rec)
    total += s.shares * s.price
    return total
    
In [13]:
    
s = Stock('Ace',100,98.9)
s
    
    Out[13]:
In [14]:
    
s.shares
    
    Out[14]:
In [15]:
    
s.shares = 98
    
    
In [16]:
    
s2 = s._replace(shares=98)
    
In [17]:
    
print(s,'\n',s2)
    
    
In [21]:
    
# Create a ST type
ST = namedtuple('ST',['name','share','price','date','time'])
# Create a prototype instance
ST_prototype = ST('', 0, 0.0, None, None)
# Function to convert a dictionary to a ST
def dict_to_ST(s):
    return ST_prototype._replace(**s)
    
In [22]:
    
a = ('hi',1,12,'2016-09-10','18:19:18')
dict_to_ST(a)
    
    
In [23]:
    
a = {'name':'hi','share':1,'price':12,'date':'2016-09-10','time':'18:19:18'}
dict_to_ST(a)
    
    Out[23]:
In [1]:
    
# want to 平方和
nums = [1,2,3,4,5,6]
s = sum(x * x for x in nums)
    
In [2]:
    
s
    
    Out[2]:
In [8]:
    
# Determine if any .py files exist in a directory
# 判断 python 文件是否存在此目录中
# 只要有一个py file 存在 any() return True
import os
files = os.listdir('f:\Save\python')
if any(name.endswith('.py') for name in files):
    print('There be python file!')
else:
    print('Sorry no python.')
# Output a tuple as CSV
s = ('ACME',50,123.34)
print(','.join(str(x) for x in s))
# Data reduction across fileds of a data structure
portfolio = [
    {'name':'GOOG','share':50},
    {'name':'Yahoo','share':75},
    {'name':'ALO','share':20},
    {'name':'CSX','share':85}
]
min_share = min(s['share'] for s in portfolio)
    
    
In [7]:
    
min_share
    
    Out[7]:
In [10]:
    
s = sum((x * x for x in nums)) # 显示的传递一个生成器表达式对象
s = sum(x * x for x in nums) # 更加优雅的实现方式 省略了括号
    
In [11]:
    
s = sum([x * x for x in nums])
s
    
    Out[11]:
In [12]:
    
# Odiginal : Return 20
min_s1 = min(s['share'] for s in portfolio)
# Alternative : Return ['name':'AOL,'share':20]
min_s2 = min(portfolio, key=lambda s:s['share'])
print(min_s1,'\n',min_s2)