Reputation: 803
I want to compare 2 lists in different files using python. For this, I am using the following:
truepositives = "a.txt"
with open(truepositives) as file1list:
file1l = file1list.read().splitlines()
basepairout = "b.txt"
with open(basepairout) as templist:
templ = templist.read().splitlines()
set1=file1l
set2=templ
truepositives = []
falsepositives = []
for line2 in set2:
for line1 in set1:
if (line2.find(line1) != -1):
if line2 not in truepositives:
truepositives.append(line2)
else:
if line2 not in falsepositives:
falsepositives.append(line2)
I want to put 'everything that is on set2 but not in set1' allocated to falsepositives. My 'if' function is working just fine, but my 'else' function is returning the entire set2. Do you know why?
Upvotes: 0
Views: 114
Reputation: 157
keep a boolean "exist" before your loops and set it to false... your problem is that you go in the else statement with each iteration in set1 that doesn't respect your conditions
It should look like this:
for line2 in set2:
exist = False
for line1 in set1:
if (line2.find(line1) == 1):
exist = True
break
if exist == False:
falsepositives.append(line2)
else:
truepositives.append(line2)
Upvotes: 2
Reputation: 6181
What about using set difference? http://docs.python.org/2/library/stdtypes.html#set
Upvotes: 0