如果全局不起作用,在Python中访问for循环之外的变量?

问题描述 投票:0回答:2

我试图在字典中的两个数据集之间找到类似的记录,以便进行进一步的比较。

我已经通过print语句确认它正在查找匹配的数据集(因此最终if语句之前的所有代码都正常工作)。然而,由于某种原因,它没有设置matchingSet2Record。这会导致最终的if语句始终运行,即使它找到了匹配项。将变量声明为在全局变量范围内不起作用。是什么导致这种情况发生?如何将第一个mathingSet2Record设置为for循环中的已发现记录?

我对这段代码唯一的问题是,即使matchingSet2Record被正确设置为找到的记录,当它在最终的if语句中进行比较时,它仍然具有值None。比较逻辑运行正常。

我有以下功能:

def processFile(data):
    # Go through every Record
    for set1Record in data["Set1"]:
        value1 = set1Record["Field1"].strip()
        matchingSet2Record = None

        # Find the EnergyIP record with the meter number
        for set2Record in data["Set2"]:
            if set2Record["Field2"].strip() == value1:
                global matchingSet2Record 
                matchingSet2Record = set2Record 

        # If there was no matching Set2 record, report it
        if matchingSet2Record == None:
            print "Missing"

每个答案/评论更新的代码(仍然显示相同的问题)

def processFile(data):
    # Go through every Record
    for set1Record in data["Set1"]:
        value1 = set1Record["Field1"].strip()
        matchingSet2Record = None

        # Find the EnergyIP record with the meter number
        for set2Record in data["Set2"]:
            if set2Record["Field2"].strip() == value1:
                matchingSet2Record = set2Record 

        # If there was no matching Set2 record, report it
        if matchingSet2Record == None:
            print "Missing"

“data”是字典的字典。代码的这一部分正常工作。当我在for循环中打印matchingSet2Record并将其设置为匹配记录时,它表明变量设置正确,但是当我在for循环之外执行它时,它显示的值为None。这是我正在使用此代码探索的问题。该问题与查找匹配记录的代码无关。

python scope global
2个回答
1
投票

这不是最终的答案,但是发表评论太过分了。

我尝试用data的实际词典重现你的问题。但是你的代码确实有效。还需要

  • qazxsw poi的一些特点(例如,我在迭代中迭代两次时看到了奇怪的效果,因为迭代已经被“消耗”了)
  • 或者它实际上不匹配,因为在Field1和Field2中的两个字符串之间存在一些不可见的差异。

这有效:

data

它打印def processFile(data): # Go through every Record for set1Record in data["Set1"]: value1 = set1Record["Field1"].strip() matchingSet2Record = None # Find the EnergyIP record with the meter number for set2Record in data["Set2"]: if set2Record["Field2"].strip() == value1: matchingSet2Record = set2Record # If there was no matching Set2 record, report it if matchingSet2Record == None: print("Missing") else: print("Found") if __name__ == '__main__': data = dict(Set1=[dict(Field1='value1')], Set2=[dict(Field2='value1')]) processFile(data)

编辑:

如果你正在学习python,那么你可以写上面这样的短片:

Found

最后一行是一个生成器:data = dict(Set1=[dict(Field1='value1')], Set2=[dict(Field2='value1 ')]) for value1 in [v['Field1'].strip() for v in data['Set1']]: try: matchingSet2Record = (v for v in data['Set2'] if v['Field2'].strip() == value1).next() print("found {}".format(matchingSet2Record)) except StopIteration: print('Missing') 创建一个生成器,(. for . in .)使它生成,直到找到第一个匹配。如果你错过了,你将会遇到next()异常。

或者,如果您只是想知道Set1和Set2之间是否存在重叠,您可以这样做:

StopIteration

请参阅data = dict(Set1=[dict(Field1='value1')], Set2=[dict(Field2='value1')]) field1 = [a['Field1'].strip() for a in data['Set1']] field2 = [a['Field2'].strip() for a in data['Set2']] if not set(field1).isdisjoint(field2): print('there is at least 1 common element between Set1 and Set2') 以了解有关this answer部分的更多信息。


2
投票

请勿在此处使用isdisjoint关键字。您实际上想要设置局部变量global,而不是全局变量。

您拥有的代码实际上是在全局范围内设置变量的值,这实际上使局部变量matchingSet2Record保持不变。这会导致if语句的条件始终计算为matchingSet2Record,因为True的值从未更新为非matchingSet2Record

© www.soinside.com 2019 - 2024. All rights reserved.