4

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?

6
  • 5
    As a counter question, how can you print a list in sorted order without sorting it? Commented Apr 16, 2015 at 13:02
  • so it's not really sorted. Sorting means, for me, I can change the order of the elements within the list, but this I don't want to do(or: I must not do). This is a example of the output ;) Commented Apr 16, 2015 at 13:04
  • 2
    But you did change the order of elements in the example. Commented Apr 16, 2015 at 13:05
  • edited, this was a mistake of myself Commented Apr 16, 2015 at 13:07
  • 1
    I'm still uncertain what the desired behavior is. What should happen if the input is: List1 = [A,B,C,H,G,D,F,]; List2 = [C,D,E,H,A]; ? Commented Apr 16, 2015 at 13:35

3 Answers 3

4
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
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, I saw it thanks. But I'll be changing it soon.
Its k , then reject my change and change it by yourself :)
1

You should use difflib.SequenceMatcher instead of difflib.Differ if you want to align something else than strings.

import difflib
def align(a, b):
    return sum((zip(a[i1:i2], b[j1:j2]) if tag == 'equal' 
                  else map(None, a[i1:i2], []) + map(None, [], b[j1:j2])
                        for tag, i1, i2, j1, j2 
                        in difflib.SequenceMatcher(None, a, b).get_opcodes()), [])

Example with integers:

list_a = [1, 2,    4, 5,    7]
list_b = [1, 2, 3, 4,    6, 7]

for (a,b) in align(list_a, list_b):
    print '{0:^5}|{1:^5}'.format(a or '',b or '')

Results:

  1  |  1  
  2  |  2  
     |  3  
  4  |  4  
  5  |     
     |  6  
  7  |  7  

Comments

-1

A simple way to do this.

List1 = ['A','B','C','D','F','H','G']
List2 = ['A','C','D','E','H']

List3 = set(List1 + List2)
for i in sorted(List3, key=lambda item: item):
    if i in List1 and i in List2:
            print('{0} | {0}'.format(i))
    elif i in List1:
        print('{0} | '.format(i))
    elif i in List2:
        print('  | {0}'.format(i))

A | A
B | 
C | C
D | D
  | E
F | 
G | 
H | H

3 Comments

Your key function is entirely unnecessary. sorted would produce exactly the same output without it. Also, your testing of membership inside a for loop delivers quadratic performance. Not good.
It just for result in alphabet order.
Let me make this clear: sorted(List3, key=lambda item: item) == sorted(List3) evaluates to True. But that's not the reason for my downvote. The downvote is for the unnecessarily inefficient algorithm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.