要求的输出

时间:2017-06-13 22:22:40

标签: python python-2.7

我有一个像这样的列表

[[{1:"one",2:"two"},{1:"one"}],[{3:"three",4:"four"},{3:"three"}]]

必需的输出:

[{1:"one",2:"two"},{1:"one"},{3:"three",4:"four"},{3:"three"}]

有人可以告诉我如何继续吗?

2 个答案:

答案 0 :(得分:1)

迭代列表的列表,将其添加到另一个列表中。

list_1 = [[{1:"one",2:"two"},{1:"one"}],[{3:"three",4:"four"},{3:"three"}]]
list_2 = []
for list in list_1:
    for dictionary in list:
        list_2.append(dictionary)

print(list_2)  # [{1: 'one', 2: 'two'}, {1: 'one'}, {3: 'three', 4: 'four'}, {3: 'three'}]

答案 1 :(得分:0)

你可以试试这个:

from itertools import chain

l = [[{1:"one",2:"two"},{1:"one"}],[{3:"three",4:"four"},{3:"three"}]]

new_l = list(chain(*l))

最终输出:

[{1: 'one', 2: 'two'}, {1: 'one'}, {3: 'three', 4: 'four'}, {3: 'three'}]