[英]Break list of dictionaries depending upon the keys of dictionary without lossing order
考慮清單
temp=[
{'white': ['BlackRock Institutional Trust Company, N.A. 400 Howard Street San Francisco, CA 94105-2618', ' ', '1,741,814', '', ' ', ' ', ' 6.85%', ' ']},
{'white': ['The Banc Funds Co, LLC 20 North Wacker Drive Suite 3300 Chicago, IL 60606-3105', ' ', '1,447,529', '', ' ', ' ', ' 5.69%', ' ']},
{'blue': ['James B. Miller, Jr.', ' ', '3,413,249', '', '(1) ', ' ', '13.40%', ' ']},
{'blue': ['Major General (Ret) David R. Bockel', ' ', '41,471', '', '(2) ', ' ', ' *', ' ']},
{'white': ['Wm. Millard Choate', ' ', '221,581', '', '(3) ', ' ', ' *', ' ']},
{'white': ['Dr. Donald A. Harp, Jr.', ' ', '40,892', '', '(4) ', ' ', ' *', ' ']},
{'white': ['Kevin S. King', ' ', '53,124', '', '(5) ', ' ', ' *', ' ']},
{'white': ['William C. Lankford, Jr.', ' ', '32,043', '', '(6) ', ' ', ' *', ' ']},
{'white': ['H. Palmer Proctor, Jr.', ' ', '309,384', '', '(7) ', ' ', '1.22%', ' ']},
{'white': ['W. Clyde Shepherd III', ' ', '349,450', '', '(8) ', ' ', '1.37%', ' ']},
{'white': ['Rankin M. Smith, Jr.', ' ', '303,768', '', '(9) ', ' ', '1.19%', ' ']},
{'white': ['Stephen H. Brolly', ' ', '48,958', '', ' ', ' ', ' *', ' ']},
{'blue': ['David Buchanan', ' ', '278,601', '', ' ', ' ', '1.10%', ' ']},
{'blue': ['All directors and executive officers as a group (11 persons)', ' ', '5,092,521', '', '(10) ', ' ', '19.93%', ' ']}
]
每當字典的鍵更改時,我都希望將列表分成不同的列表。 所需的輸出將是
[{'white': ['BlackRock Institutional Trust Company, N.A. 400 Howard Street San Francisco, CA 94105-2618', ' ', '1,741,814', '', ' ', ' ', ' 6.85%', ' ']}, {'white': ['The Banc Funds Co, LLC 20 North Wacker Drive Suite 3300 Chicago, IL 60606-3105', ' ', '1,447,529', '', ' ', ' ', ' 5.69%', ' ']}]
[{'blue': ['James B. Miller, Jr.', ' ', '3,413,249', '', '(1) ', ' ', '13.40%', ' ']}, {'blue': ['Major General (Ret) David R. Bockel', ' ', '41,471', '', '(2) ', ' ', ' *', ' ']}]
[{'white': ['Wm. Millard Choate', ' ', '221,581', '', '(3) ', ' ', ' *', ' ']}, {'white': ['Dr. Donald A. Harp, Jr.', ' ', '40,892', '', '(4) ', ' ', ' *', ' ']}, {'white': ['Kevin S. King', ' ', '53,124', '', '(5) ', ' ', ' *', ' ']}, {'white': ['William C. Lankford, Jr.', ' ', '32,043', '', '(6) ', ' ', ' *', ' ']}, {'white': ['H. Palmer Proctor, Jr.', ' ', '309,384', '', '(7) ', ' ', '1.22%', ' ']}, {'white': ['W. Clyde Shepherd III', ' ', '349,450', '', '(8) ', ' ', '1.37%', ' ']}, {'white': ['Rankin M. Smith, Jr.', ' ', '303,768', '', '(9) ', ' ', '1.19%', ' ']}, {'white': ['Stephen H. Brolly', ' ', '48,958', '', ' ', ' ', ' *', ' ']}]
[{'blue': ['David Buchanan', ' ', '278,601', '', ' ', ' ', '1.10%', ' ']}, {'blue': ['All directors and executive officers as a group (11 persons)', ' ', '5,092,521', '', '(10) ', ' ', '19.93%', ' ']}]
密鑰可以大於兩個(即白色和藍色)
現在,我想出了這個邏輯,但是有沒有簡單或簡短的方法可以做到這一點。
def format(temp):
i=0
tmp_list = []
while i<len(temp):
found=False
for color1 in temp[i]:
if i+1<len(temp):
for color2 in temp[i+1]:
if color1!=color2:
tmp_list.append(temp[i])
tmp_list.append("changed")
found=True
if found==False:
tmp_list.append(temp[i])
i=i+1
final_list = []
another_lis = []
for tl in tmp_list:
if tl!='changed':
another_lis.append(tl)
else:
final_list.append(another_lis)
another_lis = []
return final_list
whole_list = format(temp)
for wl in whole_list:
print(wl)
一個很好的方法是使用itertools.groupby
:
from itertools import groupby
temp = [...]
data = [list(g) for _, g in groupby(temp, key=dict.keys)]
但是,正如Eli Korvigo指出的那樣,此解決方案僅適用於Python 3.x中的多鍵字典,因為在Python 2.x上, dict.keys()
返回一個列表對象,在比較時該對象是順序敏感的。 如Eli所述,在Python 2.x中使用的合適替代品將是數據結構,例如set
。
我喜歡有一個自定義方法,如下面的自定義方法,可在需要在連續元素之間進行條件切片時使用。
def chunk_while(predicate, iterable):
i, x, size = 0, 0, len(iterable)
while i < size-1:
if not predicate(iterable[i], iterable[i+1]):
yield iterable[x:i+1]
x = i + 1
i += 1
yield iterable[x:size]
在這種情況下,可以通過以下方式使用它:
slices = chunk_while(lambda x,y: list(x) == list(y), temp)
結果是嵌套數組的生成器:
print(list(slices))
# [
# [{'white': ['BlackRock Institutional ...', '..']}, {'white': ['The Banc Funds ...', '..']}],
# [{'blue': ['James B. Miller, Jr.', '..']}, {'blue': ['Major General (Ret) ...']}],
# [{'white': ['Wm. Millard Choate', '..']}, {'white': ['Dr. Donald A. Harp, Jr.', '..']}, {'white': ['Kevin S. King', '..']}, {'white': ['William C. Lankford, Jr.', '..']}, {'white': ['H. Palmer Proctor, Jr.', '..']}, {'white': ['W. Clyde Shepherd III', '..']}, {'white': ['Rankin M. Smith, Jr.', '..']}, {'white': ['Stephen H. Brolly', '..']}],
# [{'blue': ['David Buchanan', '..']}, {'blue': ['All directors and executive ...', '..']}]
# ]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.