-1

I have 2 list of binary numbers and I want to compare all elements in one list to all elements in another list and see if they have only one difference in one position put an X on that position for example :

list1 = ['0000']
list2 = ['0100','0101']

there is only one difference between list1 item and first element of list2, so we put an X on difference position like:

'0X00'

How could I find out that there is only one difference between two item and change it?

More example:

list1 = ['0101','0011']
list2 = ['1010','1101']

every item of these two list have more than one difference so ignore it but first element of list1 and second element of list2 only have one difference on the first character so put an X in that position :

'X101'
Roxana
  • 41
  • 6
  • With what part exactly are you having problems? – Tomerikoo Nov 19 '20 at 11:25
  • 1
    Does this answer your question? [All combinations of a list of lists](https://stackoverflow.com/questions/798854/all-combinations-of-a-list-of-lists) – Tomerikoo Nov 19 '20 at 11:25
  • @Tomerikoo to find out if there is only one difference put an X on that position – Roxana Nov 19 '20 at 11:29
  • So can you update your question with your current code and explain the specific part you're struggling with? and give some more example inputs and outputs. What if there is more then one element in `list1`? – Tomerikoo Nov 19 '20 at 11:32
  • Do you want to compare first element of list1 to first, second to second or first element of list one to all elements of list two and so on? – Assad Ali Nov 19 '20 at 11:37
  • Can you now update the question with your code and focus it more? If you're having trouble looping all the couples of string - ask about that. If you're having trouble finding the differences between two strings - ask about that... And again, post your current code and what's wrong with it. Right now it might appear as if you're hoping someone will provide you with homework solutions – Tomerikoo Nov 19 '20 at 11:51

1 Answers1

0
l1 = ['0000','1110']
l2 = ['0101','1111']
new = []
for elem1 in l1:#each element in l1
  for elem2 in l2:#each element in l2
    #elem1 and elem2 are of type str
    only1 = 0 #will be used to count the changings
    temp = '' #str containing X
    for i in range(len(elem1)):#considering elem1 and elem2 are equal length
      if elem1[i] != elem2[i]:
        only1+=1
        temp += 'X'
      else:
        temp += elem1[i]
    if only1 == 1:
      new.append(temp)

print(new)
abdullahQureshee
  • 322
  • 2
  • 12
  • A bit more Pythonic to do `for char1, char2 in zip(elem1, elem2)` instead of using `for i in range(...)` – Tomerikoo Nov 19 '20 at 11:48
  • Notice the loop I am talking about is the one of the characters, not the one of the lists... So the first loop can be `for elem1, elem2 in itertools.product(l1, l2)` and the second `for char1, char2 in zip(elem1, elem2)` – Tomerikoo Nov 19 '20 at 11:53
  • got it, missed it before – abdullahQureshee Nov 19 '20 at 11:54