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
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):
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments