rows = [
{'address': '5412 N CLARK', 'date': '07/01/2012'},
{'address': '5148 N CLARK', 'date': '07/04/2012'},
{'address': '5800 E 58TH', 'date': '07/02/2012'},
{'address': '2122 N CLARK', 'date': '07/03/2012'},
{'address': '5645 N RAVENSWOOD', 'date': '07/02/2012'},
{'address': '1060 W ADDISON', 'date': '07/02/2012'},
{'address': '4801 N BROADWAY', 'date': '07/01/2012'},
{'address': '1039 W GRANVILLE', 'date': '07/04/2012'},
]
from itertools import groupby
from operator import itemgetter
rows.sort(key=itemgetter('date'))
for date,items in groupby(rows,key=itemgetter('date')):
print(date)
for i in items:
print(' ',i)
07/01/2012
{'address': '5412 N CLARK', 'date': '07/01/2012'}
{'address': '4801 N BROADWAY', 'date': '07/01/2012'}
07/02/2012
{'address': '5800 E 58TH', 'date': '07/02/2012'}
{'address': '5645 N RAVENSWOOD', 'date': '07/02/2012'}
{'address': '1060 W ADDISON', 'date': '07/02/2012'}
07/03/2012
{'address': '2122 N CLARK', 'date': '07/03/2012'}
07/04/2012
{'address': '5148 N CLARK', 'date': '07/04/2012'}
{'address': '1039 W GRANVILLE', 'date': '07/04/2012'}
from collections import defaultdict
row_by_date = defaultdict(list)
for row in rows:
row_by_date[row['date']].append(row)
for r in row_by_date['07/01/2012']:
print (r)
{'address': '5412 N CLARK', 'date': '07/01/2012'}
{'address': '4801 N BROADWAY', 'date': '07/01/2012'}
row_by_date
defaultdict(list,
{'07/01/2012': [{'address': '5412 N CLARK', 'date': '07/01/2012'},
{'address': '4801 N BROADWAY', 'date': '07/01/2012'}],
'07/02/2012': [{'address': '5800 E 58TH', 'date': '07/02/2012'},
{'address': '5645 N RAVENSWOOD', 'date': '07/02/2012'},
{'address': '1060 W ADDISON', 'date': '07/02/2012'}],
'07/03/2012': [{'address': '2122 N CLARK', 'date': '07/03/2012'}],
'07/04/2012': [{'address': '5148 N CLARK', 'date': '07/04/2012'},
{'address': '1039 W GRANVILLE', 'date': '07/04/2012'}]})
1.16过滤序列元素
问题:有一个序列,想利用一些规则提取出需要的值或者缩短序列
方案:最近简单的是使用列表推导
缺陷:会产生一个较大的结果集,占用内存
my_list = [1,4,2,-4,8,0,2,-11,20]
[n for n in my_list if n>0]
addresses = [
'5412 N CLARK',
'5148 N CLARK',
'5800 E 58TH',
'2122 N CLARK',
'5645 N RAVENSWOOD',
'1060 W ADDISON',
'4801 N BROADWAY',
'1039 W GRANVILLE',
]
counts = [0,3,10,4,1,7,6,1]
from itertools import compress
r = compress(addresses,[n>5for n in counts])
r
<itertools.compress at 0xa253b0>
list(r)
['5800 E 58TH', '1060 W ADDISON', '4801 N BROADWAY']
# 普通代码,假设记录的第2列是单价,第3列是数量defcompute_cost(records):
total = 0.0for rec in records:
total += rec[1]*rec[2]
return total
# 使用命名元组,假设记录的第2列是单价,第3列是数量
stock = namedtuple('Stock',['name','price','shares'])
defcompute_cost(records):
total = 0.0for rec in records:
s = stock(*rec)
total += s.shares * s.price
return total
# 确定某个文件下是否存在.py文件import os
files = os.listdir('F:/ML')
if any(name.endswith('.py') for name in files):
print('Yes,exist python file')
else:
print('sorry,no python file')
s = ('ACME',50,123.33)
print(','.join(str(x) for x in s))
portfolio = [
{'name':'GOOG', 'shares': 50},
{'name':'YHOO', 'shares': 75},
{'name':'AOL', 'shares': 20},
{'name':'SCOX', 'shares': 65}
]
min_shares = min(s['shares'] for s in portfolio)
min_shares
Yes,exist python file
ACME,50,123.33
20
1.19合并多个字典或者映射
问题:现在有多个字典或者映射,将其合并为一个并可以执行某些操作
方案:可以使用collections模块中的ChainMap类
a = {'x':1,'z':3}
b = { 'y':2,'z':4}
from collections import ChainMap
c = ChainMap(a,b)
print(c['x'])
print(c['y'])
print(c['z'])
如果出现重复键,那么第一次出现的映射值会被返回。因此,例子程序中的 c[‘z’]总是会返回字典 a 中对应的值,而不是 b 中对应的值。对于字典的更新或删除操作总是影响的是列表中第一个字典。
c['z'] = 10
c['w'] = 20del c['x']
c
ChainMap({'z': 10, 'w': 20}, {'y': 2, 'z': 4})
a
{'z': 10, 'w': 20}
del c['y']
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
d:\program filles\python\lib\collections\__init__.py in __delitem__(self, key)
933 try:
--> 934 del self.maps[0][key]
935 except KeyError:
KeyError: 'y'
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
<ipython-input-122-1b93a26af00d> in <module>()
----> 1 del c['y']
d:\program filles\python\lib\collections\__init__.py in __delitem__(self, key)
934 del self.maps[0][key]
935 except KeyError:
--> 936 raise KeyError('Key not found in the first mapping: {!r}'.format(key))
937
938 def popitem(self):
KeyError: "Key not found in the first mapping: 'y'"