2

I am trying to check the elements of two lists in python to see if they contain the same values in the same position (indexes) of them. If an element let's say in position 0 of list A is not the same with the element in position 0 of list B I want to extract that value from list A and start again the comparison.

My program is below:

 listA = ["book","2","stage","me","you"]
 listB = ["stage","me","you"]

listB is always a sublist of listA!!

  diff_list = []
  for n in range(0, len(listA)):
     for k in range(0, len(listB)):
         if n == k:
            if listA[n] != listB[k]:
                 rm_item = listA.pop(n)
                 diff_list.append(rm_item)
                 k==0
                 print(k)

In my terminal first k = 0 then k = 1. Is there a way to remove the item from listA and then start the comparison again?

Thanks for the help guys! To be honest....What I wish to do is to get the difference between two strings. I have two texts where text B is always subtext of text A. So I used splitlines() to split the two texts and then I want compare the two lists to get what I want! Sorry but I am new to python and still can't figure out how a lot of things are done!

So I have

   textA='The first paragraph of the book is written well' 

and

   textB = 'the book is written well'

the result should be

  text_diff ='the first paragraph of' 
bettas
  • 195
  • 1
  • 2
  • 11
  • 1
    Why are you using two for-loops and then comparing to see if the values are equal? That can clearly be reduced to a single for loop. – Shashank May 13 '15 at 15:53
  • How should I do this in one loop? – bettas May 13 '15 at 15:59
  • 5
    I'm confused. You want to remove items from A until it looks like B? Why not just use B? – tdelaney May 13 '15 at 16:02
  • 4
    This looks like an [X/Y problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What is the actual task you're trying to complete? – wflynny May 13 '15 at 16:03
  • 1
    @bettas I don't think we're quite sure what you're trying to do. What is the expected output? – mbomb007 May 13 '15 at 16:05
  • @bettas Easy..take out the 2nd for loop and the 1st if. Then replace all k's with n's. Not that it would solve your problem, but using two loops is making something O(n^2) when it only needs to be O(n). – Shashank May 13 '15 at 16:09
  • 1
    can you please include your intended output, OP? Would help. – corvid May 13 '15 at 16:10
  • 1
    @bettas, Is what you want the indexes where the text differs from the subtext? Or just the additional words not found in the subtext? **Be specific**. – wflynny May 13 '15 at 16:11
  • I edited my question. The result should be diff_list = ["book","2"] – bettas May 13 '15 at 16:13
  • 1
    Put that desired result **in the question**. – Stefan Pochmann May 13 '15 at 16:16
  • @bettas Is it a [substring](http://en.wikipedia.org/wiki/Substring) or a [subsequence](http://en.wikipedia.org/wiki/Subsequence)? And what do you mean you're trying to find the difference? You want the first word where they differ? – Shashank May 13 '15 at 16:20
  • Please check again I have edited the question :) – bettas May 13 '15 at 16:21
  • @bettas See this answer from another question. http://stackoverflow.com/a/1157132/2415524 – mbomb007 May 13 '15 at 16:21
  • possible duplicate of [Remove all occurences of a value from a Python list](http://stackoverflow.com/questions/1157106/remove-all-occurences-of-a-value-from-a-python-list) – mbomb007 May 13 '15 at 16:22
  • @mbomb007 no this is not what I would like to do...check again my question please! – bettas May 13 '15 at 16:24
  • Do you want the output to be "The first paragraph of"? Notice the case difference in "The" – Robert Jacobs May 13 '15 at 16:24
  • 1
    Judging by the new example, you're looking for something like `textA.replace(textB, '')`. Are you joking? – Stefan Pochmann May 13 '15 at 16:24
  • You really should get clear if you are woring on a string or a list. For strings, there might be easier and faster ways to do this. – too honest for this site May 13 '15 at 16:25
  • Your example is nothing like your question! `textA[:textA.index(textB)].strip()` should do it. – tdelaney May 13 '15 at 16:26

5 Answers5

4

It seems to me that if you have two strings stringA and stringB (presumably lines from a larger text split on \n) such that stringB is always a substring of stringA, and you want to find the extra stuff in stringA, then it'd be easiest to do something like

stringA = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus aliquam leo odio, ut posuere dui rutrum ac. Nulla facilisi."
stringB = "ut posuere dui rutrum ac"

print stringA.replace(stringB, '')
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus aliquam leo odio, . Nulla facilisi."

Edit: Using your example text from the updated OP, you could do

textA = 'The first paragraph of the book is written well' 
textB = 'the book is written well'
text_diff = textA.lower().replace(textB.lower(), '')

which yields

'the first paragraph of'
wflynny
  • 18,065
  • 5
  • 46
  • 67
  • In the end, this is the correct answer. :) Nice job noticing the likelihood of the XY problem. – Shashank May 13 '15 at 16:26
  • Oh my god @Shashank! it was indeed too easy...I feel stupid to have asked such a question! – bettas May 13 '15 at 16:26
  • @Shashank, indeed. Anytime there's that much confusion over what exactly OP wants, it's most likely XY. – wflynny May 13 '15 at 16:28
  • 1
    @bettas To avoid further chaos, make sure to read about the [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). If you can learn from your experiences, you aren't stupid. – Shashank May 13 '15 at 16:31
1

Try zip

>>> listA = ['Caesar', 'Pompey', 'Krassus']
>>> listB = ['Augustus', 'Pompey']
>>> for x,y in zip(listA, listB):
      print(x == y)

If you want to get all the values in listA not in listB, where order matters.

a_unique = [x[0] for x in zip(listA, listB) if x[0] != x[1]]
too honest for this site
  • 12,050
  • 4
  • 30
  • 52
corvid
  • 10,733
  • 11
  • 61
  • 130
  • If I understand this correctly, it's to _find all elements in A and B such that A[index] == B[index]_ and... exclude them? List comprehension added for the effect – corvid May 13 '15 at 16:09
  • There is a fundamental problem here because `zip` keeps pairing the i'th elements when you want to remove from A without advancing B. – tdelaney May 13 '15 at 16:10
  • Read the last sentence of the first paragraph of the question of this thread of this forum of ... – too honest for this site May 13 '15 at 16:11
  • @tdelaney: hmm.. I understood "extract" such as to get the element, not to _delete_ it. But you might be right; the TO is inexact on that. – too honest for this site May 13 '15 at 16:12
  • @bettas: So I did not missinterpret your intention? Well, if I had known that ... ;-) Not that the lists must have equal lenght, otherwise you must `a_unique.extend(listA[len(listB):])` (len(A) > len(B); if listB is longer, nothing changes. – too honest for this site May 13 '15 at 16:13
0

If order is not important and you want to see the set difference of the two lists, you can use this:

print(list(set(listA) - set(listB)))

This outputs:

['2', 'book']
mbomb007
  • 3,788
  • 3
  • 39
  • 68
0

Assuming that the sublist B is in the same order as list A you can do this:

listA = ["blabla", "book", "2", "stage", "me", "you"]
listB = ["stage", "me", "you"]

for i in range(len(listB)):
    if listA[i] != listB[i]:
        del listA[0]

print listA

And you don't really need to have a diff_list, at the end list A will be the same as list B.

stbamb
  • 179
  • 2
  • 2
  • 13
0

You can remove an element in the list by using remove. Use a try block in case it doesn't exist. The while True loop is to find duplicates in listA. Since the question has been clarified to find elements in A that are not in B, I have modified this.

diff_list = []
listA = ["book","2","stage","me","you"]
listB = ["stage","me","you"]
for k in listB:
    try:
        while True:
            listA.remove(k)
            print k
    except:
        diff_list.append(k)
print listA
print diff_list

Output

stage
me
you
['book', '2']
['stage', 'me', 'you']
Robert Jacobs
  • 3,266
  • 1
  • 20
  • 30
  • What if some of the words are repeated? `remove` may get rid of a word that passed a previous compare. – tdelaney May 13 '15 at 16:14
  • I thought you wanted to remove all elements in A that matched B. including duplicates. If that is not the case, then remove the while True loop. It will then get rid of only the first match in A that is in B for all in list B. – Robert Jacobs May 13 '15 at 16:17
  • Are you trying to remove elements in A that do NOT match an element in B? – Robert Jacobs May 13 '15 at 16:19
  • @RobertJacobs please check again I have edited the question :) – bettas May 13 '15 at 16:22