Hello everyone, How do I know which item in the list is not the same as another item in another list? And if the elements are unequal, find the position of the elements that are unequal

a = [u'120 devon 15.61', u'126 korn05000000000000 12.96', u'128 itswat 20.58',] b = [u'120', u'devon', u'15.61', u'126', u'korn05', u'12.96', u'128', u'itswat', u'20.58'] if(u' '.join(b) == u' '.join(a)): pass else: ? 

    4 answers 4

     a = [u'120 devon 15.61', u'126 korn05000000000000 12.96', u'128 itswat 20.58',] b = [u'120', u'devon', u'15.61', u'126', u'korn05', u'12.96', u'128', u'itswat', u'20.58'] x = u' '.join(a) y = u' '.join(b) if(x == y): pass else: a1 = x.split(' ') b1 = y.split(' ') for index, value in enumerate(a1): if (b1[index] != value): print(index) 
       a = [u'120 devon 15.61', u'126 korn05000000000000 12.96', u'128 itswat 20.58',] b = [u'120', u'devon', u'15.61', u'126', u'korn05', u'12.96', u'128', u'itswat', u'20.58'] x = u' '.join(a).split() y = u' '.join(b).split() for index, value in enumerate(x): if (y[index] != value): print(index) 
         a = ' '.join(a).split() b = ' '.join(b).split() index_list = [a.index(x) for x, y in zip(a, b) if not x == y] 

        if the length of the lists is different

         a = iter(' '.join(a).split()) b = iter(' '.join(b).split()) for i, x in enumerate(a): try: if not x == next(b): print(i) except StopIteration: # len(a) > len(b) print(i) try: while next(b): # len(a) < len(b) i += 1 print(i) except StopIteration:pass 

          To find positions in which items with the same index differ in a pair of lists:

           a = [1, 2, 3, 4] b = [1, 20, 30] indices_uniq = [i for i, (x, y) in enumerate(zip(a, b)) if x != y] # -> [1, 2] 

          Result [1, 2] as the second and third elements are different. Only i < min(len(a), len(b)) itertools.zip_longest(a, b, fill_value=sentinel) are itertools.zip_longest(a, b, fill_value=sentinel) , since there is no match for the shorter list of elements (otherwise, itertools.zip_longest(a, b, fill_value=sentinel) be used instead of zip(a,b) ).

          For the literal input in the question, the result is [0,1,2] , that is, all elements are distinguished in pairs in the given lists. If you want to compare the input as a sequence of words separated by a space, you first need to normalize the input ( get_words() ), and then compare the corresponding words ( unequal_indicies(a, b) ):

           def get_words(chunks): return (word for chunk in chunks for word in chunk.split()) def unequal_indices(a, b): return (i for i, (x, y) in enumerate(zip(a, b)) if x != y) a = ["120 devon 15.61", "126 korn05000000000000 12.96", "128 itswat 20.58"] b = ["120", "devon", "15.61", "126", "korn05", "12.96", "128", "itswat", "20.58"] print(list(unequal_indices(get_words(a), get_words(b)))) # -> [4] 

          In this case (when comparing by words) the result [4] , that is, only the fifth words in a given pair of texts differ.


          In tests , you can use self.assertEqual(a, b) , which will show the differences between lists (Python 3), if any:

           Traceback (most recent call last): File "./__main__.py", line 13, in test self.assertEqual(a, b) AssertionError: Lists differ: [1, 2, 3, 4] != [1, 20, 30] First differing element 1: 2 20 First list contains 1 additional elements. First extra element 3: 4 - [1, 2, 3, 4] ? ^^^ + [1, 20, 30] ? + ^ 

          To compare sequences containing text, you can difflib to try :

           >>> print(*difflib.ndiff(a, b), sep='\n') - 120 devon 15.61 - 126 korn05000000000000 12.96 - 128 itswat 20.58 + 120 + devon + 15.61 + 126 + korn05 + 12.96 + 128 + itswat + 20.58 

          The output shows that all elements are different.

          If you normalize input, then only one word is expected to be different:

           >>> print(*difflib.ndiff(list(get_words(a)), list(get_words(b))), sep='\n') 120 devon 15.61 126 - korn05000000000000 + korn05 12.96 128 itswat 20.58