Python 根据字典的键在不丢失顺序的情况下断开字典列表
考虑一份清单Python 根据字典的键在不丢失顺序的情况下断开字典列表,python,list,dictionary,Python,List,Dictionary,考虑一份清单 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
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)
def格式(临时):
i=0
tmp_列表=[]
而我一个很好的方法是使用:
然而,正如Eli Korvigo所指出的,这个解决方案只适用于Python3.x中的多键字典,因为在Python2.x上,dict.keys()
返回一个列表对象,在进行比较时它是顺序敏感的。正如Eli所说,在Python 2.x中使用的一个合适的替代方法是数据结构,例如集我希望有一个像下面这样的自定义方法,在需要在连续元素之间进行切片时使用
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]
结果是嵌套数组的生成器:
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 ...', '..']}]
# ]
我不明白字典的键什么时候变。你能更清楚地解释一下你是如何将输入与输出联系起来的吗?如果我读对了他的问题,他想按照每个字典的第一个键对数据进行分组@BoarGulesLets举个例子。当您进行第三次迭代时,您会发现键为“蓝色”->,与前一个键“白色”不匹配。所以我将从这一点开始打破这个列表。注意->任何键都可以在那里。谢谢,非常感谢您的时间如果可以有多个密钥,那么解决方案是否应该考虑所有密钥?也就是说,你想在一组键中的任何键发生变化时引入断点吗?谢谢,真的很好,兄弟。我喜欢一些itertools。不过,不需要将dict_key
对象转换为列表。它们实现\uuuu eq\uuuu
,即您只需传递key=dict.keys
,而不是key=lambda…
。不客气。您还可以补充,此解决方案仅适用于Python3.x上的多键词典,因为在Python2.x上,dict.keys
返回一个列表,并且列表比较是顺序敏感的。您以前的解决方案甚至在Py3.x上也存在多键dict问题(由于转换为list)。在Python2上,可以执行key=set
。这也适用于Py3,但在这种情况下,dict.key
解决方案更有效,因为dict_keys
对象是(计算上便宜的)视图。嘿,当我运行它时,它会给出结果,但不知怎的,当我在代码中实现它时,我得到了这个错误->“TypeError:keys()不带参数(1给定)”。在执行这一行之前,我已经检查了输入(temp)。我正在使用python 2。7@user3809411你能显示你的groupby
通话吗?如果您在groupby
调用中传递key=dict.keys
并得到此错误,则很可能是您通过一个实例跟踪了内置名称dict
:如果没有很好的理由,千万不要这样做。此外,在Py 2.7上,您应该改用key=set
。Iterable
对象是不可下标的,因此您的参数名称具有误导性,甚至不安全。订阅操作(在函数中使用的范围内)是由符合顺序的类实现的。
ABC.@EliKorvigo我认为这是可以的,因为。但也许我误解了什么。你能帮我理解为什么方法不起作用,为什么参数不安全吗?你可能已经注意到,你引用的代码段中没有订阅。itertools
中的所有函数都设计用于通用Iterable
对象。这些对象只能实现一种方法:\uuuuu iter\uuu
。您的解决方案依赖于订阅和切片:这两个都是由所有内置的序列
类型的\uuu getitem\uuuu
实现的,尽管用户定义的序列
不会强制实现切片。
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 ...', '..']}]
# ]