无法从嵌套for循环访问父for循环

时间:2017-12-16 03:33:48

标签: python python-2.7 loops scope

我试图比较两个词典数组之间的索引值。为了实现这一点,我使用了嵌套的for循环。虽然循环正确迭代但是存在firstDataSetNumber没有在嵌套for循环内迭代的问题(由第二个print语句指示)。第一个print语句显示for循环正在迭代。什么可能导致从嵌套for循环中的第二个firstDataSetNumber打印的值永远不会改变,即使它在第一个print语句中的值显示它确实正在迭代正确?

def processCommandCenterFile(data):
    firstDataSet = data["FirstDataSet"]
    secondDataSet = data["SecondDataSet"]

    # Go through every First Data Set Record
    for firstDataSetRecord in firstDataSet:
        firstDataSetNumber = firstDataSetRecord["KeyOne"].strip()
        matchingSecondDataSetRecord = None

        print(firstDataSetNumber) # Always iterates properly throughout the application

        # Find the Second Data Set record with the KeyTwo number
        for secondDataSetRecord in secondDataSet:
            print(firstDataSetNumber) # Never iterates past the first value
            if secondDataSetRecord["KeyTwo"].strip() == firstDataSetNumber:
                matchingSecondDataSetRecord = secondDataSetRecord

data = {
    "FirstDataSet": CsvToDictionary("first_data_set.csv"),
    "SecondDataSet": CsvToDictionary("second_data_set.csv")
}

processCommandCenterFile(data)

我希望两个print语句的输出相同。但是,当我运行它时,第一个print语句是索引中的每个项目,但是第二个print语句中的数据卡在列表中的第一个项目上。

FirstDataSet和SecondDataSet键存储以下函数的输出,该函数加载CSV并将其转换为字典,其中CSV标题为键。

import csv

def CsvToDictionary(path):
    file = open(path, 'r')
    rawCsvArray = csv.reader(file, delimiter=',', quotechar='|')
    headers = rawCsvArray.next()
    dataDictionary = csv.DictReader(file, fieldnames=headers)

    return dataDictionary

用于制作FirstDataSet

的CSV
KeyOne
143739629
143739629
143750196
143750196
143739646
143739646
143739661
143739661
143739718

然后是用于制作SecondDataSet

的CSV
KeyTwo
143739629
143739629
143750196
143750196
143739646
143739646
143739661
143739661
143739718

3 个答案:

答案 0 :(得分:1)

柯克兰回答了这个问题的实际解决方案。如果有人在这里结束,这里有一些背景。

问题在于if(1 < 2) { if(2 < 3) { // This second if is called a nested if because it's run inside another // if statement } } if(1 < 2 && 2 < 3) { // This is also a form of nested if because it has the same effect as running // the code above } 会生成csv.DictReader()类型的实例,按the docs,“就像普通读者一样运作。”
这意味着从DictReader读取值会耗尽实例的内容 在第一轮内循环之后,DictReader中没有任何内容。在未来的迭代中,内部循环没有任何迭代的元素。

可以通过向secondDataSet语句添加标记来验证这一点:

print

输出:

def processCommandCenterFile(data):
    ...
    for firstDataSetRecord in firstDataSet:
        ...
        print("first print:", firstDataSetNumber) 

    for secondDataSetRecord in secondDataSet:
        print("second print:", firstDataSetNumber) 
        ...

processCommandCenterFile(data)

正如柯克兰指出的那样,最好的选择是将('first print:', '143739629') ('second print:', '143739629') ('second print:', '143739629') ('second print:', '143739629') ('second print:', '143739629') ('second print:', '143739629') ('second print:', '143739629') ('second print:', '143739629') ('second print:', '143739629') ('second print:', '143739629') ('first print:', '143739629') ('first print:', '143750196') ('first print:', '143750196') ('first print:', '143739646') ('first print:', '143739646') ('first print:', '143739661') ('first print:', '143739661') ('first print:', '143739718') 转换为词典列表,例如,

DictReader

(或者只是使用Pandas。)

答案 1 :(得分:0)

我解决了这个问题(感谢Blckknght),但是将csv.DictReader的输出转换为如下列表:

import csv

def CsvToDictionary(path):
    file = open(path, 'r')
    rawCsvArray = csv.reader(file, delimiter=',', quotechar='|')
    headers = rawCsvArray.next()
    dataDictionary = csv.DictReader(file, fieldnames=headers)

    return list(dataDictionary)

答案 2 :(得分:-1)

第一个数据集编号看起来在第二个数据集开始之前声明。然后第二组中的每一个都应该查看firstdataset