简体   繁体   English

变量不会在嵌套循环python中更改

[英]variable does not change inside of a nested loop python

Python beginner here. Python初学者在这里。 I need to get each line from first file "src.csv", that has strings like (300 rows) 我需要从第一个文件“ src.csv”中获取每一行,该文件的字符串类似于(300行)

"12345, a, b"
"234567, e, c"

and find string in second file "data.csv" (100k rows) 并在第二个文件“ data.csv”中查找字符串(100k行)

"12345678"
"23456789011248"

by first coloumn of the first file, where digits are substring of one of the string in the secon file. 按第一个文件的第一列,其中数字是secon文件中一个字符串的子字符串。 And then write to output file. 然后写入输出文件。

import sys
import csv



dat_file_name = "data.dat"
src_file_name = "src.csv"
out_file_name = "out.csv"

if (len(sys.argv) == 4):
    dat_file_name = sys.argv[1]
    src_file_name = sys.argv[2]
    out_file_name = sys.argv[3]


out_writer = open(out_file_name, "w")


i = 0
j = 0
with open(src_file_name, "r") as src, open(dat_file_name, 'r') as dat:
    src_reader = csv.reader(src)
    dat_reader = csv.reader(dat)

    for sub_string in src_reader:

        # print sub_string

        for string in dat_reader:

            out_writer.write(sub_string[0])
            out_writer.write("\n")

            print sub_string[0]

            i+=1
        j+=i


out_writer.close()

print i #for debug only
print j #for debug only

But instead of expected value of "sub_string[0]", I have first value of first row of first file... 但是,我没有第一个文件的第一行的第一个值,而不是“ sub_string [0]”的期望值。

12345
12345
...

in each iteration. 在每次迭代中。 And more then that, output file contains 100k rows instead of 30m. 而且,输出文件包含100k行而不是30m行。

My question is why my version of usage of nested loops has unexpected behavior. 我的问题是为什么我的嵌套循环用法版本具有意外行为。 Why variable "substring[0]" does not change inside of the nested loop? 为什么变量“ substring [0]”在嵌套循环内不会更改? I would appreciate any help. 我将不胜感激任何帮助。

Why should it change in the nested loop? 为什么要在嵌套循环中更改它? The inner loop is iterating over dat_reader , but sub_string is the result of the outer iteration, which can't change until the inner loop has completely finished. 内部循环在dat_reader上进行迭代,但是sub_string外部迭代的结果,除非内部循环完全完成,否则外部迭代无法更改。

You don't want a nested loop at all; 您根本不需要嵌套循环; you want to loop over both files at once. 您想一次遍历两个文件。 You can do that with zip : 您可以使用zip来做到这一点:

for sub_string, string in zip(src_reader, dat_reader):
    out_writer.write(sub_string[0])

And you don't need the indexes i and j at all, remove them. 而且您根本不需要索引ij ,将它们删除。

Alright there's a couple things wrong with this code. 好了,这段代码有几处错误。 First you don't even check for substrings and second your loops are backwards. 首先,您甚至不检查子字符串,其次,循环是向后的。

out_file_name = "out.csv"

if (len(sys.argv) == 4):
    dat_file_name = sys.argv[1]
    src_file_name = sys.argv[2]
    out_file_name = sys.argv[3]

with open(src_file_name, "r") as src, open(dat_file_name, 'r') as dat, open(out_file_name, "w") as out_writer:
    src_reader = csv.reader(src)
    dat_reader = csv.reader(dat)

    for string in dat_reader:    
        for sub_string in src_reader:
            if sub_string[0] in string: #Check if substring in string
                out_writer.write(sub_string[0])
                out_writer.write("\n")

                print sub_string[0]
        src.seek(0) #Your file pointer is at the end of the file so move it back to the beginning

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更改要在循环内使用的变量(Python) - Change variable to be used inside loop (Python) Python:在循环内更改迭代变量是否安全? - Python: Is it safe to change iteration variable inside the loop? 在while循环内进行的变量更改未反映在Python 3的循环外 - Change in variable made inside of a while loop is not reflected outside of the loop in Python 3 为什么在循环内部或外部初始化变量会改变代码行为? - Why does initialising the variable inside or outside of the loop change the code behaviour? 为什么变量替换在 Python 嵌套循环中不起作用 - Why variable replacement does not work in Python nested loop 如何使用按钮更改循环内的变量? (Tkinter和python) - How to change a variable inside of loop with button ? (Tkinter and python) 是否可以在 python 中运行时手动更改/更新循环内的变量 - Is it possible to manually change/update a variable inside a loop while it is running in python Python从外部动态更改内部循环中的变量 - Python dynamically change a variable inside loop from exterior python在for循环中自动将变量从列表更改为字符串 - python automatically change a variable from list to string inside a for loop 嵌套的“for”循环不会改变循环内的变量 - Nested "for" loop will not change variable within loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM