我有這樣需要的輸出
[[{1:"one",2:"two"},{1:"one"}],[{3:"three",4:"four"},{3:"three"}]]
所需的輸出列表:
[{1:"one",2:"two"},{1:"one"},{3:"three",4:"four"},{3:"three"}]
有人能告訴我如何進行?
我有這樣需要的輸出
[[{1:"one",2:"two"},{1:"one"}],[{3:"three",4:"four"},{3:"three"}]]
所需的輸出列表:
[{1:"one",2:"two"},{1:"one"},{3:"three",4:"four"},{3:"three"}]
有人能告訴我如何進行?
迭代列表的列表以將其添加到另一個列表。
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'}]
不!得到這個作爲輸出:
@ArohiGupta我的錯誤;我編輯了答案,再試一次。 –
任何想法如何從輸出中刪除「unicode」? –
你可以試試這個:
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'}]
這就是所謂扁平化的列表。請參閱https://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python,https://stackoverflow.com/questions/406121/flattening-a -shallow一覽中-蟒?noredirect = 1&LQ = 1。一個好的答案是'[子列表中項目的L子項列表]' – Stuart