bettas
bettas

Reputation: 315

how to change index value in python lists

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' 

Upvotes: 2

Views: 31758

Answers (5)

Robert Jacobs
Robert Jacobs

Reputation: 3360

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']

Upvotes: 0

wflynny
wflynny

Reputation: 18521

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'

Upvotes: 4

corvid
corvid

Reputation: 11187

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]]

Upvotes: 1

stbamb
stbamb

Reputation: 197

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.

Upvotes: 0

mbomb007
mbomb007

Reputation: 4251

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']

Upvotes: 0

Related Questions