I have two lists (list1 and list2) in python filled with an own datatype.
I want to compare these to lists and give all elements of these lists to stdout(or somewhere else), but in a specific order(without sorting the lists in any way).
List1 and List2 can have elements which are not in the other list, but can also have elements which be in the other list. These elements, beeing in both lists, should output at the same line. But the elements beeing only in one list, should be in the right order too, at the end.
Example:
List1 = [A,B,C,D,F,H,G];
List2 = [A,C,D,E,H];
output should be:
List1 |List2
A A
B
C C
D D
E
F
H H
G
How can I "sort" in these way?
解决方案import difflib, re
list_a = ['A', 'B', 'C', 'D', 'F', 'H', 'G']
list_b = ['A', 'C', 'D', 'E', 'H']
for i in difflib.Differ().compare(list_a, list_b):
differ_char, letter = re.match(r'([\s\-+]) ([A-Z])', i).groups()
choices = [' ' + letter, letter + ' ', letter + ' ' + letter]
print choices[['+', '-', ' '].index(differ_char)] # print lines