Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

comparing two lists and finding indices of changes

Tags:

python

list

I'm trying to compare two lists and find the position and changed character at that position. For example, these are two lists:

list1 = ['I', 'C', 'A', 'N', 'R', 'U', 'N']
list2 = ['I', 'K', 'A', 'N', 'R', 'U', 'T']

I want to be able to output the position and change for the differences in the two lists. As you can see, a letter can be repeated multiple times at a different index position. This is the code that I have tried, but I can't seem to print out the second location accurately.

for indexing in range(0, len(list1)):
    if list1[indexing] != list2[indexing]:
        dontuseindex = indexing
        poschange = indexing + 1
        changecharacter = list2[indexing]
for indexingagain in range(dontuseindex + 1, len(list1)):
    if list1[indexingagain] != list2[indexingagain]:
        secondposchange = indexingagain + 1
        secondchangecharacter = list2[indexingagain]

Is there a better way to solve this problem or any suggestions to the code I have?

My expected output would be:

2    K
7    T
like image 562
interstellar Avatar asked Mar 15 '16 22:03

interstellar


People also ask

How do you compare elements of two different lists?

sort() and == operator. The list. sort() method sorts the two lists and the == operator compares the two lists item by item which means they have equal data items at equal positions. This checks if the list contains equal data item values but it does not take into account the order of elements in the list.

How do you compare two lists efficiently?

counter() method can be used to compare lists efficiently. The counter() function counts the frequency of the items in a list and stores the data as a dictionary in the format <value>:<frequency>. If two lists have the exact same dictionary output, we can infer that the lists are the same.

How do I compare two lists of different sizes Python?

Use the zip() function to pair up the lists, counting all the differences, then add the difference in length. The sum() sums up True and False values; this works because Python's boolean type is a subclass of int and False equals 0 , True equals 1 .


Video Answer


2 Answers

for index, (first, second) in enumerate(zip(list1, list2)):
    if first != second:
        print(index, second)

Output:

1 K
6 T

If you want the output you gave, we need to count from 1 instead of the usual 0:

for index, (first, second) in enumerate(zip(list1, list2), start=1):
like image 150
Peter Wood Avatar answered Sep 20 '22 17:09

Peter Wood


Another possibility to save all the not-equal elements with the index is with a list comprehensions:

list1 = ['I', 'C', 'A', 'N', 'R', 'U', 'N']
list2 = ['I', 'K', 'A', 'N', 'R', 'U', 'T']

# Append index, element1 and element2 as tuple to the list if they are not equal
changes = [(i, list1[i], list2[i]) for i in range(len(list1)) if list1[i] != list2[i]]
print(changes)
#prints [(1, 'C', 'K'), (6, 'N', 'T')]

Not exactly what you specified as output but it's close.

You could print the specified output with a loop:

for i in changes:
    print(i[0] + 1, i[1])
# 2 K
# 7 T

In the comments several alternative ways of designing the list comprehension were suggested:

  • Using enumerate and zip:

    changes = [(i, e1, e2) for i, (e1, e2) in enumerate(zip(list1, list2)) if e1 != e2]
    
  • Using enumerate with start index and zip:

    changes = [(i, e1, e2) for i, (e1, e2) in enumerate(zip(list1, list2), 1)  if e1 != e2]
    
  • Using zip and itertools.count:

    import itertools
    changes = [(i, e1, e2) for i, e1, e2 in zip(itertools.count(), list1, list2)) if e1 != e2]
    
  • Using zip and itertools.count with start-index:

    changes = [(i, e1, e2) for i, e1, e2 in zip(itertools.count(1), list1, list2)) if e1 != e2]
    

All of them producing the same result as the original but using different (better) python features.

like image 37
MSeifert Avatar answered Sep 19 '22 17:09

MSeifert