根据字典的键中断字典列表而不会丢失顺序

时间:2019-03-12 14:13:52

标签: 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, 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)

1 个答案:

答案 0 :(得分:5)

一个不错的方法是使用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之类的数据结构。