Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
索引迭代错误Python_Python - Fatal编程技术网

索引迭代错误Python

索引迭代错误Python,python,Python,我正在寻找一些关于我的代码的帮助,这是一个简单的python代码,但我在这方面是新手,所以这对我来说有点棘手 我想做的只是取一个.txt文件,读取它并将其与一些字符串进行比较,然后说.txt文件中的哪些单词没有我要问的字符串,例如: txt文件: ABCD 亚行 DDCA CDAA CDAB EEGF GFFE 还有我的字符串限制: S=['AA','BB','CC',DD'] 因此,在输出中应该是这样的: ABCD CDAB EEGF GFFE 无法显示其他字符串,因为它们与S中的一个或多个字

我正在寻找一些关于我的代码的帮助,这是一个简单的python代码,但我在这方面是新手,所以这对我来说有点棘手

我想做的只是取一个.txt文件,读取它并将其与一些字符串进行比较,然后说.txt文件中的哪些单词没有我要问的字符串,例如:

txt文件: ABCD 亚行 DDCA CDAA CDAB EEGF GFFE

还有我的字符串限制: S=['AA','BB','CC',DD']

因此,在输出中应该是这样的: ABCD CDAB EEGF GFFE

无法显示其他字符串,因为它们与S中的一个或多个字符串匹配。 现在,我的代码和我的问题

我有以下代码:

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
以及以下txt文件: AAC 基础知识 行政协调会 添加 联邦航空局

我得到的结果是: 在:AAC 不在:ABC 不在:ACC 不在:添加 美国联邦航空局

我可以从中看到,我的代码没有按应该的方式迭代,因此没有打印正确的答案

我已经尝试了很多方法来修复它,但我就是无法找到解决方案,所以如果有人能帮我解决这个问题,我将非常感激


非常感谢

问题在于,假设f会随着j的更改而自动更新

试着改变

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


此外,您可能还应该有一个循环来更改i。

问题在于,您假设f会在更改j时自动更新

试着改变

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


此外,您可能还应该有一个循环来更改i。

问题在于,您假设f会在更改j时自动更新

试着改变

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


此外,您可能还应该有一个循环来更改i。

问题在于,您假设f会在更改j时自动更新

试着改变

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

此外,您可能还应该有一个循环来更改i。

尝试以下方法:

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
试试这个:

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
试试这个:

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
试试这个:

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

您的代码有几个问题。在检查所有AA、BB、CC、EE值之前更新i。这就是为什么你不能把所有的限制都落实到位

我写了下面的代码,我想这可能是你需要的。试试看,如果你需要我进一步解释,请告诉我

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

您的代码有几个问题。在检查所有AA、BB、CC、EE值之前更新i。这就是为什么你不能把所有的限制都落实到位

我写了下面的代码,我想这可能是你需要的。试试看,如果你需要我进一步解释,请告诉我

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

您的代码有几个问题。在检查所有AA、BB、CC、EE值之前更新i。这就是为什么你不能把所有的限制都落实到位

我写了下面的代码,我想这可能是你需要的。试试看,如果你需要我进一步解释,请告诉我

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

您的代码有几个问题。在检查所有AA、BB、CC、EE值之前更新i。这就是为什么你不能把所有的限制都落实到位

我写了下面的代码,我想这可能是你需要的。试试看,如果你需要我进一步解释,请告诉我

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

通过避免显式循环索引,并使用
中的
替换对
find
的调用,您可以编写相当紧凑的代码:

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

通过避免显式循环索引,并使用
中的
替换对
find
的调用,您可以编写相当紧凑的代码:

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

通过避免显式循环索引,并使用
中的
替换对
find
的调用,您可以编写相当紧凑的代码:

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

通过避免显式循环索引,并使用
中的
替换对
find
的调用,您可以编写相当紧凑的代码:

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

我可以帮你做家庭作业,但是一次考试(prueba)有点超越了道德界限。你不把你的测试文件叫做test.txt吗?(怀疑的好处,在这里,这就是我的假设…)我可以帮你做家庭作业,但是考试(prueba)有点违反道德。你不把你的测试文件叫做test.txt吗?(怀疑的好处,在这里,这就是我的假设…)我可以帮你做家庭作业,但是考试(prueba)有点违反道德。你不把你的测试文件叫做test.txt吗?(怀疑的好处,在这里,这就是我的假设…)我可以帮你做家庭作业,但是考试(prueba)有点违反道德。你不把你的测试文件叫做test.txt吗?(怀疑的好处,这里,这就是我的假设…)