2016-09-08 3 views
2

Hier habe ich eine Liste von Wörterbüchern, mein Ziel ist es, über die Liste zu iterieren und wenn es 2 oder mehr Listen gibt, möchte ich sie zusammenführen und in eine Ausgabeliste einfügen, und wenn es nur eine Liste gibt so wie es gespeichert ist.Vorgehensweise Listenabflachung innerhalb der Schleife mit Listenverständnis?

data = [ 
      [[{'font-weight': '1'},{'font-weight': '1'}],[{'font-weight': '2'},{'font-weight': '2'}]], 
      [{'font-weight': '3'},{'font-weight': '3'},{'font-weight': '3'}], 
      [[{'font-weight': '1'},{'font-weight': '1'}],[{'font-weight': '2'},{'font-weight': '2'}]], 
      [{'font-weight': '3'},{'font-weight': '3'}] 
     ] 

I Liste Abflachung für bestimmte Element data[0]

print([item for sublist in data[0] for item in sublist]) 
[{'font-weight': '1'}, {'font-weight': '1'}, {'font-weight': '2'}, {'font-weight': '2'}] 

Erwartete Ausgabe kann:

data = [ 
      [{'font-weight': '1'},{'font-weight': '1'},{'font-weight': '2'},{'font-weight': '2'}], 
      [{'font-weight': '3'},{'font-weight': '3'},{'font-weight': '3'}], 
      [{'font-weight': '1'},{'font-weight': '1'},{'font-weight': '2'},{'font-weight': '2'}] 
      [{'font-weight': '3'},{'font-weight': '3'}] 
     ] 

Antwort

5

Sie conditional list comprehension mit itertools.chain für diese Elemente verwenden könnte, die Abflachung müssen:

In [54]: import itertools 

In [55]: [list(itertools.chain(*l)) if isinstance(l[0], list) else l for l in data] 
Out[55]: 
[[{'font-weight': '1'}, 
    {'font-weight': '1'}, 
    {'font-weight': '2'}, 
    {'font-weight': '2'}], 
[{'font-weight': '3'}, {'font-weight': '3'}, {'font-weight': '3'}], 
[{'font-weight': '1'}, 
    {'font-weight': '1'}, 
    {'font-weight': '2'}, 
    {'font-weight': '2'}], 
[{'font-weight': '3'}, {'font-weight': '3'}]] 
+0

Perfect! Wie ein Chef @Ami, danke –

+0

Ich bevorzuge diese Antwort nur Nice Arbeit, –

+0

@Rahul Danke! Ich mag deine Antwort auch. –

3

die Sie interessieren,

result = [] 
for item in data: 
    result.append([i for j in item for i in j]) 

Einzeiliger Code mit Liste Verständnis,

[[i for j in item for i in j] for item in data] 

Alternative Methode,

import numpy as np 
[list(np.array(i).flat) for i in data] 

Ergebnis

[[{'font-weight': '1'}, 
    {'font-weight': '1'}, 
    {'font-weight': '2'}, 
    {'font-weight': '2'}], 
[{'font-weight': '3'}, {'font-weight': '3'}, {'font-weight': '3'}], 
[{'font-weight': '1'}, 
    {'font-weight': '1'}, 
    {'font-weight': '2'}, 
    {'font-weight': '2'}], 
[{'font-weight': '3'}, {'font-weight': '3'}]] 
0

iterieren Sie durch die Liste und prüfen Sie, ob es sich bei jedem Element um Listen von Listen handelt. Wenn es so flach ist.


data = [ 
     [[{'font-weight': '1'},{'font-weight': '1'}],[{'font-weight': '2'},{'font-weight': '2'}]], 
     [{'font-weight': '3'},{'font-weight': '3'},{'font-weight': '3'}], 
     [[{'font-weight': '1'},{'font-weight': '1'}],[{'font-weight': '2'},{'font-weight': '2'}]], 
     [{'font-weight': '3'},{'font-weight': '3'}] 
     ] 
for n, each_item in enumerate(data): 
    if any(isinstance(el, list) for el in each_item): 
     data[n] = sum(each_item, []) 

print data 
Verwandte Themen