gpt4 book ai didi

python - 根据字典的键打破字典列表而不丢失顺序

转载 作者:行者123 更新时间:2023-11-28 20:56:06 25 4
gpt4 key购买 nike

考虑一个列表

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

关于python - 根据字典的键打破字典列表而不丢失顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55123601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com