2017-07-19 1 views
1

Hallo Ich möchte zwei Listen, die unterschiedliche Längen haben, vergleichen und eine sortierte Tabelle mit Artikeln drucken, die in jeder Tabelle fehlen. Ich bin teilweise in der Lage, dies zu erreichen und die Werte zu drucken, die in list_2 fehlen. Aber ich kann die Werte, die in list_1 fehlen, auch nicht aus list_2, dem Buchstaben "z", drucken. Wie kann ich dies durchführen, um die gewünschte Ausgabe zu erhalten?Python Vergleich zweier Listen mit verschiedenen Längen

list_1 = ['a', 'b', 'c', 'd', 'e', 'f'] 
list_2 = ['b', 'c', 'f', 'z'] 

table_format = '{:<10} {:<10}' 
print(table_format.format('list_1', 'list_2')) 
print('-' * 20) 
for x in list_1: 
    for y in list_2: 
     if x in y: 
      print(table_format.format(x, y)) 
      break 
    else: 
     print(table_format.format(x,'Missing')) 

Stromausgang:

list_1  list_2  
-------------------- 
a   Missing 
b   b   
c   c   
d   Missing 
e   Missing 
f   f   

gewünschte Ausgabe:

list_1  list_2  
-------------------- 
a   Missing 
b   b   
c   c   
d   Missing 
e   Missing 
f   f  
Missing z 
+0

Haben Sie darüber nachgedacht, mit [ 'Set'] (https://docs.python.org/3/tutorial/datastructures.html#sets)? Versuchen Sie 'set (list_2) - set (list_1)' –

+0

Das liefert den fehlenden Wert, aber nicht sicher, wie ich diesen Teil der Tabelle während der for-Schleife machen würde. – MBasith

Antwort

2

Eine Lösung könnte eine dritte Liste verwenden, die alle Elemente der beiden ursprünglichen Listen enthält. Dann könnten wir die neue Liste sortieren, und während wir darüber iterieren, könnten wir die Existenz von Elementen der dritten Liste in den ursprünglichen Listen überprüfen. Es wäre besser, die dritte Liste zu einem Set zu machen. Nach dem Vorschlag von Patrick Haugh sollten wir die ursprünglichen Listen vor der Iteration in Sätze umwandeln. Dadurch wird der Prozess effizienter. Warum? Folge diesem Beitrag. Which is faster and why? Set or List?

list_1 = set(['a', 'b', 'c', 'd', 'e', 'f']) # Or list_1 = {'a', 'b', 'c', 'd', 'e', 'f'} 
list_2 = set(['b', 'c', 'f', 'z']) # list_2 = {'b', 'c', 'f', 'z'} 

list_3 = set(list_1 | list_2) 
table_format = '{:<10} {:<10}' 
print(table_format.format('list_1', 'list_2')) 
print('-' * 20) 
for elem in sorted(list_3): 
    if elem in list_1: 
     if elem in list_2: 
      print(table_format.format(elem, elem)) 
     else: 
      print(table_format.format(elem, 'Missing')) 
    else: 
     print(table_format.format('Missing', elem)) 

Ausgang:

list_1  list_2  
-------------------- 
a   Missing 
b   b   
c   c   
d   Missing 
e   Missing 
f   f   
Missing z 
+1

Eine kleine Verbesserung, aber wenn Sie 'set (list_1)' und 'set (list_2)' speichern, können Sie Zeit sparen, wenn Sie später nachschlagen. –

+1

Das hat super funktioniert. Danke für die Hilfe und den Tipp! – MBasith

+0

@MBasith Dies sortiert Ihre Daten. Bist du sicher, dass du das willst? –

1

Mit Hilfe eines OrderedDict die Arbeit zu tun scheint:

from collections import OrderedDict 

list_1 = ['a', 'b', 'c', 'd', 'e', 'f'] 
list_2 = ['b', 'c', 'f', 'z'] 

mapping = OrderedDict() 
for x in list_1: 
    mapping[x] = x if x in list_2 else 'Missing' 

for x in list_2: 
    mapping[x] = x if x in list_1 else 'Missing' 

table_format = '{:<10} {:<10}' 
print(table_format.format('list_1', 'list_2')) 
print('-' * 20) 

for k in mapping: 
    if k in list_1: 
     print(table_format.format(k, mapping[k])) 
    else: 
     print(table_format.format(mapping[k], k)) 

Ausgang:

list_1  list_2  
-------------------- 
a   Missing 
b   b   
c   c   
d   Missing 
e   Missing 
f   f   
Missing z  
0

Sie können die Verwendung derselben Liste Verständlichkeit tun auf!

>>> print "\n".join(map(str,['list_1\tlist_2\n---------------']+[(each if each in list_1 else 'missing')+'\t'+(each if each in list_2 else 'missing') for each in sorted(set(list_1+list_2))])) 
list_1 list_2 
--------------- 
a  missing 
b  b 
c  c 
d  missing 
e  missing 
f  f 
missing z 

Ich habe das Verständnis für ein besseres Verständnis gebrochen!

>>> [each if each in list_1 else 'missing' for each in sorted(set(list_1+list_2))] 
['a', 'b', 'c', 'd', 'e', 'f', 'missing'] 

>>> [each if each in list_2 else 'missing' for each in sorted(set(list_1+list_2))] 
['missing', 'b', 'c', 'missing', 'missing', 'f', 'z'] 

>>> [(each if each in list_1 else 'missing',each if each in list_2 else 'missing') for each in sorted(set(list_1+list_2))] 
[('a', 'missing'), ('b', 'b'), ('c', 'c'), ('d', 'missing'), ('e', 'missing'), ('f', 'f'), ('missing', 'z')] 

>>> [['list_1','list_2']]+[(each if each in list_1 else 'missing',each if each in list_2 else 'missing') for each in sorted(set(list_1+list_2))] 
[['list_1', 'list_2'], ('a', 'missing'), ('b', 'b'), ('c', 'c'), ('d', 'missing'), ('e', 'missing'), ('f', 'f'), ('missing', 'z')] 

>>> print "\n".join(map(str,[['list_1','list_2']]+[(each if each in list_1 else 'missing',each if each in list_2 else 'missing') for each in sorted(set(list_1+list_2))])) 
['list_1', 'list_2'] 
('a', 'missing') 
('b', 'b') 
('c', 'c') 
('d', 'missing') 
('e', 'missing') 
('f', 'f') 
('missing', 'z') 

>>> print "\n".join(map(str,['list_1\tlist_2\n---------------']+[(each if each in list_1 else 'missing')+'\t'+(each if each in list_2 else 'missing') for each in sorted(set(list_1+list_2))])) 
list_1 list_2 
--------------- 
a  missing 
b  b 
c  c 
d  missing 
e  missing 
f  f 
missing z 
Verwandte Themen