問題描述
你有一個字典或實體的序列,然后你想根據某個特定欄位來分組迭代訪問,
解決方案
itertools.groupby()函式對于這樣的資料分組操作非常實用:
from itertools import groupby
from operator import itemgetter
rows = [
{'address': '5412 N CLARK', 'date': '07/01/2012'},
{'address': '5148 N CLARK', 'date': '07/02/2012'},
{'address': '5148 N CLARK', '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'}
]
# 先看一下直接對rows使用groupby()函式的效果
for date, item in groupby(rows):
print(date)
for i in item:
print(' ', i)
"""輸出結果:
{'address': '5412 N CLARK', 'date': '07/01/2012'}
{'address': '5412 N CLARK', 'date': '07/01/2012'}
{'address': '5148 N CLARK', 'date': '07/02/2012'}
{'address': '5148 N CLARK', 'date': '07/02/2012'}
{'address': '5148 N CLARK', 'date': '07/02/2012'}
{'address': '2122 N CLARK', 'date': '07/03/2012'}
{'address': '2122 N CLARK', 'date': '07/03/2012'}
{'address': '5645 N RAVENSWOOD', 'date': '07/02/2012'}
{'address': '5645 N RAVENSWOOD', 'date': '07/02/2012'}
{'address': '1060 W ADDISON', 'date': '07/02/2012'}
{'address': '1060 W ADDISON', 'date': '07/02/2012'}
{'address': '4801 N BROADWAY', 'date': '07/01/2012'}
{'address': '4801 N BROADWAY', 'date': '07/01/2012'}
{'address': '1039 W GRANVILLE', 'date': '07/04/2012'}
{'address': '1039 W GRANVILLE', 'date': '07/04/2012'}
"""
# 添加key引數指定回傳值
for date, item in groupby(rows, key=itemgetter('date')):
print(date)
for i in item:
print(' ', i)
"""輸出結果:
07/01/2012
{'address': '5412 N CLARK', 'date': '07/01/2012'}
07/02/2012
{'address': '5148 N CLARK', 'date': '07/02/2012'}
{'address': '5148 N CLARK', 'date': '07/02/2012'}
07/03/2012
{'address': '2122 N CLARK', 'date': '07/03/2012'}
07/02/2012
{'address': '5645 N RAVENSWOOD', 'date': '07/02/2012'}
{'address': '1060 W ADDISON', 'date': '07/02/2012'}
07/01/2012
{'address': '4801 N BROADWAY', 'date': '07/01/2012'}
07/04/2012
{'address': '1039 W GRANVILLE', 'date': '07/04/2012'}
"""
# 將rows排序后,再通過key引數指定回傳值
rows.sort(key=itemgetter('date'))
for date, item in groupby(rows, key=itemgetter('date')):
print(date)
for i in item:
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': '5148 N CLARK', 'date': '07/02/2012'}
{'address': '5148 N CLARK', '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': '1039 W GRANVILLE', 'date': '07/04/2012'}
"""
討論
groupby()函式掃描整個序列并且查找連續相同的值(或者根據指定key函式回傳值相同)的元素序列,在每次迭代的時候,它會回傳一個值和一個迭代器物件,這個迭代器物件可以生成該回傳值所在的物件,
如果你只是想將根據date欄位將資料分組到一個大的資料結構中,并且允許隨機訪問,那么可以使用defaultdict()構建一個多值字典(關于多值字典在1.6小節已經做過介紹),比如:
from collections import defaultdict
rows_by_date = defaultdict(list)
for row in rows:
rows_by_date[row['date']].append(row)
for r in rows_by_date['07/01/2012']:
print(r)
"""輸出結果:
{'address': '5412 N CLARK', 'date': '07/01/2012'}
{'address': '4801 N BROADWAY', 'date': '07/01/2012'}
"""
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/397177.html
標籤:其他
上一篇:JAVA:聊聊JAVA基礎(2)- 基本語法(待續)
下一篇:python回呼函式能做什么?
