两个for循环,第二个只在第一次迭代时执行python [英] two for loops, second only executes on first iteration python

查看:414
本文介绍了两个for循环,第二个只在第一次迭代时执行python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个蟒蛇noob,我试图比较两个文件中的行之间的值,并输出行名称,如果该行在第二个文件中,然后输出1,如果该行从第二个文件。第一次迭代返回1,因为该行在第二个文件中,但对于重新生成> 1,000行,它们都返回0,不管它们是否在第二个列表中。好像第二个for循环只在第一次迭代时执行。任何想法为什么?这里是我的代码:

$ pre $ $ code $ import $
$ b $ file1 = sys.argv [1]

file2 = sys.argv [2]

name = str(file2)
$ b f1 = open(file1,'r')
f2 = open(file1,'r')
o1 = open((name +'1.txt'),'w')

for line in f1:
name = line.strip('\r\\\
')
count = 0
for line1 in f2:
if name == line1.strip('\r\\\
') :
count + = 1
print(str(name)+'\ t'+ str(1))
o1.write(str(name)+'\ t'+ str(1)+'\r\\\
')
if count == 0:
print(str(name)+'\ t'+ str(0))
o1.write(str(name)+'\ t'+ str(0)+'\r\\\
')

f1.close()
f2.close )
o1.close()
非常感谢任何帮助!

经过一些更改后,这就是我所拥有的,它只返回'1s'

  f1 = open(file1,'r')#opens用于读取
的文件f2 = open(file2,'r')
o1 = open((name +'1.txt'),'w')

f2s = {line.strip('\ n')for line in f2}

作为f1中的行:
line = line.strip('\\\
')
count = 0
if line in f2s:
count + = 1
print(str(line)+'\ t'+ str(1))
o1.write(str(line)+'\t'+ str(1)+'\\\
' )
if count == 0:
print(str(line)+'\ t'+ str(0))
o1.write(str(line)+'\t '+ str(0)+'\\\
')

尴尬,我打开同一个文件两次。新手。

解决方案 是第二个文件的迭代器,当你已经读完了,它就已经用完了。



可以重置迭代器 f2.seek(0,0) ,但这并不是最好的选择。

最好把所有的值放在 f2 放入一个 set ,然后遍历 f1 一次:

  f2s = {line.strip('\\\
')for line in f2}

for line in f1:
name = line.strip('\\\
')#不需要\r\\\

如果名字在f2s中:
#等等

如果您需要从 f1 中的每行的出现次数计入 f2 ,那么您可以使用 <$ c

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'计数器(林$ f





$如果名字是$在f2c中:
count = f2c [name]


I am a python noob, and I am attempting to compare values between lines in two files and output the "line name" followed by a 1 if the line is in the second file and a 0 if the line is missing from the second file. The first iteration returns a 1, because that line is in the second file, but for the remaning > 1,000 lines, they all return a 0 regardless of whether they are in the second list or not. It seems as though the second "for loop" only executes on the first iteration. Any ideas on why? Here is my code:

    import sys  

    file1 = sys.argv[1] 

    file2 = sys.argv[2]

    name = str(file2)

    f1 = open(file1, 'r') 
    f2 = open(file1, 'r')
    o1 = open((name + '1.txt'), 'w')

    for line in f1:
        name = line.strip('\r\n')
        count = 0
        for line1 in f2:
            if name == line1.strip('\r\n'):
                count += 1
                print (str(name) + '\t' + str(1))
                o1.write(str(name) + '\t' + str(1) + '\r\n')
        if count == 0:
            print (str(name) + '\t' + str(0))
            o1.write(str(name) + '\t' + str(0) + '\r\n')

    f1.close()
    f2.close()
    o1.close()
Any help is very much appreciated!

After some changes, this is what I have and it only returns '1s'

f1 = open(file1, 'r') #opens files for reading
f2 = open(file2, 'r')
o1 = open((name + '1.txt'), 'w')

f2s = {line.strip('\n') for line in f2}

for line in f1:
    line = line.strip('\n')
    count = 0
    if line in f2s:
        count += 1
        print (str(line) + '\t' + str(1))
        o1.write(str(line) + '\t' + str(1) + '\n')
    if count == 0:
        print (str(line) + '\t' + str(0))
        o1.write(str(line) + '\t' + str(0) + '\n')

Embarrassing, I was opening the same file twice. Rookie.

f2 is an iterator over your second file, and when that has been read, it's exhausted.

You can reset the iterator f2.seek(0, 0), but that's not really the best way to go.

Better put all the values from f2 into a set and then iterate over f1 only once:

f2s = {line.strip('\n') for line in f2}

for line in f1:
    name = line.strip('\n') # No need for \r\n
    if name in f2s:
        # etc.

If you need to count the number of occurences of each line from f1 in f2, then you can use a Counter:

from collections import Counter
f2c = Counter(line.strip('\n') for line in f2)

for line in f1:
    name = line.strip('\n')
    if name in f2c:
        count = f2c[name]

这篇关于两个for循环,第二个只在第一次迭代时执行python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆