0

Say I have two lists:

list1=[a,b,c,d,e,f,a,b,c]
list2=[a,c,d]

How could I compare list1 and list2 such that I would get an output like:

list3=[match,-,match,match,-,-,-,-,-]
So that it goes in order
Here's my code:

h=[]
for i in range(len(change2)):
	for j in range(len(atom)):
		if atom[j]==atom[i]:
			h.append("H")
		else:
			h.append("-")
print h

Both change2 and atom have letters with a particular order. atom has 479 letters and change2 has 188 letters. I want an output that is a list with 479 elements. I want to scan through atom until the first element is found in change2. I want the list to have elements "H" if they match and "-" if they do not. My code is giving me a huge list! I'm not sure how to make it ordered.

Thanks!

4
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by Gribouillis
0

Add a print statement so you know what is being compared, and hence if the append is correct or not. Also, you should first check that both lists are the same length.

h=[]
for i in range(len(change2)):
	for j in range(len(atom)):
                print "comparing" atom[j], atom[i]
		if atom[j]==atom[i]:
			h.append("H")
		else:
			h.append("-")
print h

Edited by woooee: n/a

0

Add a print statement so you know what is being compared, and hence if the append is correct or not. Also, you should first check that both lists are the same length.

h=[]
for i in range(len(change2)):
	for j in range(len(atom)):
                print "comparing" atom[j], atom[i]
		if atom[j]==atom[i]:
			h.append("H")
		else:
			h.append("-")
print h

Both lists are not the same length. I want to compare the first element in the list change2 to the elements in list atom until a match is found. I want the match to append "H" to a new list (the same length as atoms) and those that don't match append "-". Then go on to the next element in list change2.

0

I suggest

#python 2 and 3

change2 = list('abcdefabc')
atom = list('acd')

atomset = set(atom)
h = ['H' if x in atomset else '-' for x in change2]

assert(''.join(h) == 'H-HH--H-H')

Edited by Gribouillis: n/a

0

I suggest

#python 2 and 3

change2 = list('abcdefabc')
atom = list('acd')

atomset = set(atom)
h = ['H' if x in atomset else '-' for x in change2]

assert(''.join(h) == 'H-HH--H-H')

That's not working either. What is the assert doing?

1
found = - 1
for search in list2:
    if seach in list1[found+1:]:
        found = list1.index(search)
       #handle correct case
    else:
       # not found case

Show effort by finishing the code. This code as is would start again from begining if there is not matching letter in list2.

0

That's not working either. What is the assert doing?

why do you say it's not working ? It's working for me and python 2.6 or 3.1. The assert checks that the resulting list is H - H H - H - H.

Edited by Gribouillis: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.