user2976764
user2976764

Reputation: 9

Index Iteration error Python

i'm looking for some help with my code, it's a simple python code, but i'm new at this so it's been a little tricky for me..

What i want to do is just to take a .txt file, read it and compare it to some strings and say which words of the .txt file don't have the strings I'm asking, for example:

txt file: ABCD AABC DDCA CDAA CDAB EEGF GFFE

And my string restricctions: S= ['AA', 'BB', 'CC', DD']

so in the output should go something like: ABCD CDAB EEGF GFFE

the other ones can't be shown because they match with one or more of the strings in S. Now, my code and my problem.

I have the following code:

import string

ins = open( "prueba.txt", "r" )
array = []

for line in ins:
    array.append( line )
ins.close()

s = ''.join(array)
a= s.split()
c = ['AA', 'BB','CC', 'DD','EE', 'FF','GG', 'HH','II', 'JJ','KK', 'LL', 'MM', 'NN','OO', 'PP','QQ', 'RR','SS', 'TT','UU', 'VV', 'WW', 'XX','YY', 'ZZ']

i=0
j=0
f= c[j]

for j in range(0,len(a)):
     if a[i].find(f) != -1:
        print 'Is in:' , a[i]
        i=i+1
     else:
        print 'Is not in:' , a[i]
        i=i+1

And the following txt file: AAC ABC ACC ADD FAA

The output i'm having is: Is in: AAC Is not in: ABC Is not in: ACC Is not in: ADD Is in: FAA

What I can see from this, is that my code isn't iterating how it should, so it's not printing the right answer.

I've been trying a lot of things to fix it, but I just can't get to the solution, so if anyone can help me with this I'll really appreciate it!

Thanks a lot!

Upvotes: 0

Views: 75

Answers (4)

damienfrancois
damienfrancois

Reputation: 59190

You can write rather compact code by avoiding explicit loop indices and replacing the call to find with the use of in:

$ cat t.py
ins = open( "prueba.txt", "r" ).read()
res = ['AA', 'BB','CC', 'DD']

for i in ins.split():
    if all([r not in i for r in res]):
        print i


$ cat prueba.txt 
ABCD AABC DDCA CDAA CDAB EEGF GFFE
$ python t.py
ABCD
CDAB
EEGF
GFFE
$ 

Upvotes: 3

sir_k
sir_k

Reputation: 332

Couple of issues with your code. You update i before checking it against all the AA,BB,CC,EE values. This is why you would not get all the restrictions in place.

I wrote the following code and I think it might be what you need. Try it and let me know if you need me to explain it more.

import string

ins = open( "prueba.txt", "r" )
array = []

for line in ins:
    array.append( line )
ins.close()

s = ''.join(array)
a= s.split()
c = ['AA', 'BB','CC', 'DD','EE', 'FF','GG', 'HH','II', 'JJ','KK', 'LL', 'MM', 'NN','OO', 'PP','QQ', 'RR','SS', 'TT','UU', 'VV', 'WW', 'XX','YY', 'ZZ']

i=0
j=0


for i in range(0,len(a)): #go through all the items from the input list
  found=False
  for j in range(0,len(c)): #check every item against every restriction
       f=c[j]
       if a[i].find(f) != -1:
          print 'Is in:' , a[i]
          found=True #remember if we found a restriction and stop the loop
          break
  if(found==False): print 'Is not in:' , a[i] #if by the end of the loop no restriction was found tell me that nothing was found

Upvotes: 0

Christian Tapia
Christian Tapia

Reputation: 34146

Try this:

s = ['AA', 'BB', 'CC', 'DD']
mInput = ['ABCD','AABC','DDCA','CDAA','CDAB','EEGF','GFFE']

anti_res = []

for e in mInput:
    for i in s:
        print i
        if i in e:
            if e not in anti_res:
                anti_res.append(e)

res = [e for e in mInput if e not in anti_res]
print res

Upvotes: 0

Sorin
Sorin

Reputation: 11968

The problem is that you assume f automatically updates as you change j.

Try changing

 if a[i].find(f) != -1:

to

 if a[i].find(c[j]) != -1:

Also you should probably have a loop to change i.

Upvotes: 0

Related Questions