2015-04-18 100 views
0
#!/usr/bin/python 
# 1.15. Grouping Records Together Based on a Field 
# Problem: You have a sequence of dictionaries or instances and you want to iterate over the data 
# in groups based on the value of a particular field, such as date. 

from operator import itemgetter 
from itertools import groupby 

# To iterate over the data in chunks grouped by date. 
# First, sort by the desired field (in this case, date) and 
# then use itertools.groupby(): 

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'}, 
] 

# Sort by the desired field first 
rows.sort(key=itemgetter('date')) 
print (rows) 


for date, items in groupby(rows, key=itemgetter('date')): 
    print(date) 
    for i in items: 
     print(' ', i) 

上述代碼的輸出的一個列表中的順序是這樣的:鍵的字典中

[{'date': '07/01/2012', 'address': '5412 N CLARK'}, {'date': '07/01/2012', 'address': '4801 N BROADWAY'}, {'date': '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/03/2012', 'address': '2122 N CLARK'}, {'date': '07/04/2012', 'address': '5148 N CLARK'}, {'date': '07/04/2012', 'address': '1039 W GRANVILLE'}] 
07/01/2012 
    {'date': '07/01/2012', 'address': '5412 N CLARK'} 
    {'date': '07/01/2012', 'address': '4801 N BROADWAY'} 
07/02/2012 
    {'date': '07/02/2012', 'address': '5800 E 58TH'} 
    {'date': '07/02/2012', 'address': '5645 N RAVENSWOOD'} 
    {'date': '07/02/2012', 'address': '1060 W ADDISON'} 
07/03/2012 
    {'date': '07/03/2012', 'address': '2122 N CLARK'} 
07/04/2012 
    {'date': '07/04/2012', 'address': '5148 N CLARK'} 
    {'date': '07/04/2012', 'address': '1039 W GRANVILLE'} 

「日期」是「地址」的前面。 然而,如果我通過只是在第24行加print (rows)如下改變代碼:

#!/usr/bin/python 
# 1.15. Grouping Records Together Based on a Field 
# Problem: You have a sequence of dictionaries or instances and you want to iterate over the data 
# in groups based on the value of a particular field, such as date. 

from operator import itemgetter 
from itertools import groupby 

# To iterate over the data in chunks grouped by date. 
# First, sort by the desired field (in this case, date) and 
# then use itertools.groupby(): 

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'}, 
] 

print (rows) 
# Sort by the desired field first 
rows.sort(key=itemgetter('date')) 
print (rows) 


for date, items in groupby(rows, key=itemgetter('date')): 
    print(date) 
    for i in items: 
     print(' ', i) 

上述代碼的輸出是這樣的:

[{'address': '5412 N CLARK', 'date': '07/01/2012'}, {'address': '4801 N BROADWAY', 'date': '07/01/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'}, {'address': '2122 N CLARK', 'date': '07/03/2012'}, {'address': '5148 N CLARK', 'date': '07/04/2012'}, {'address': '1039 W GRANVILLE', 'date': '07/04/2012'}] 
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'} 

「地址」是在前面「日期」。

爲什麼鍵的順序會改變?

+4

有一個原因,但從根本上說,你的問題是毫無意義的......詞典沒有排序。沒關係。 –

+2

@mu無:依靠或期待任何命令是明顯錯誤的。 –

+0

這就是我想說的。 *誰在乎* ......它不應該*不重要*。 –

回答

6

順序並不是因爲您添加了一行代碼,而是因爲hash randomization。實現哈希隨機化減少了DoS攻擊,使用在數據中散列爲相同值的成千上萬個值的破碎序列。一個HTTP POST請求。

+0

是的。確實是隨機的。我已經嘗試了幾次相同的代碼,並打印出不同的輸出。 – fluency03

2

如果您希望訂單保持不變,您需要使用OrderedDictcollections

from collections import OrderedDict 

row = OrderedDict([('address', '5412 N CLARK'), ('date', '07/01/2012')]) 

>>> row 
OrderedDict([('address', '5412 N CLARK'), ('date', '07/01/2012')]) 

>>> rows.keys() 
['address', 'date']