Verwenden Sie itertools.combinations. In diesem Fall wird der Code viel einfacher. Sie benötigen nur eine Liste, combinations
erzeugt automatisch nur eindeutige Kombinationen. Sie müssen also nicht "AA" oder "CB" filtern, wenn Sie nur "BC" passieren.
>>> from itertools import combinations
>>> l=['A','B','C','D','E','F']
list(combinations(l, 2))
[('A', 'B'), ('A', 'C'), ('A', 'D'), ('A', 'E'), ('A', 'F'),
('B', 'C'), ('B', 'D'), ('B', 'E'), ('B', 'F'),
('C', 'D'), ('C', 'E'), ('C', 'F'),
('D', 'E'), ('D', 'F'),
('E', 'F')]
>>> # or with joined strings
>>> [' and '.join(x) for x in combinations(l, 2)]
['A and B', 'A and C', 'A and D', 'A and E', 'A and F',
'B and C', 'B and D', 'B and E', 'B and F',
'C and D', 'C and E', 'C and F',
'D and E', 'D and F',
'E and F']
Ich habe Ausgabe formatiert, um zu sehen, dass Sie sauberes Ergebnis haben.
Dies ist ein Fall eines XY-Problems. Sie brauchen nicht zwei identische Listen, nur eine Liste und 'iterations.combinations'. –
Wollen Sie auch CA, CB, etc? –
Rechts @JoelCornett der eigentliche Code besteht nur aus einer einzigen Liste und es ist ein Fall von Kombinationen. Dies ist das schnelle Beispiel, das ich mir ausgedacht habe, übersprang meine Meinung, dass;) – Sailesh