True/false if 语句和 for 循环中的语句

True/false statements within if statements and for loops

我在一个列表中有多个列表,我想检查多个内容并形成一个具有 if 条件和 TrueFalse 的新列表。我的代码:

import re
runlist = [['229', '134', '9'], ['229', '137', '5'], ['629', '140', '5'], ['631', '140', '5'], ['632', '140', '5'], ['633', '140', '8'], ['422', '141', '5'], ['628', '141', '9'], ['629', '141', '11']], [['629', '145', '8'], ['630', '145', '8'], ['631', '145', '8'], ['632', '145', '8'], ['632', '146', '5']], ...]
goodrunslist = []
for i in range(len(runlist)):
    for k in range (0, len(runlist[i])):
        if re.match('[2][0-5][0-9]', runlist[i][k][0]):
            ch2 = True
    for m in range (0, len(runlist[i])):
        if re.match('[6][0-4][0-9]', runlist[i][m][0]):
            ch4 = True
    if ch2 and ch4:
        goodrunslist.append(runlist[i])

然而,在最后的第一个for循环下,它给了我一个NameErrorch2ch4没有定义,所以它不能将任何内容附加到新列表。我该如何解决这个问题?

编辑:我已经通过以下代码修复了 NameError,但现在我的问题是新列表没有返回任何内容(它正在返回 []),即使我可以看到它应该有一些元素。新代码如下。任何修复或建议表示赞赏;谢谢!

import re
runlist = [['229', '134', '9'], ['229', '137', '5'], ['629', '140', '5'], ['631', '140', '5'], ['632', '140', '5'], ['633', '140', '8'], ['422', '141', '5'], ['628', '141', '9'], ['629', '141', '11']], [['629', '145', '8'], ['630', '145', '8'], ['631', '145', '8'], ['632', '145', '8'], ['632', '146', '5']], ...]
goodrunslist = []
ch2, ch4 = False, False
for i in range(len(runlist)):
    for k in range (0, len(runlist[i])):
        if re.match('[2][0-5][0-9]', runlist[i][k][0]):
            ch2 = True
        else:
            ch2 = False
    for m in range (0, len(runlist[i])):
        if re.match('[6][0-4][0-9]', runlist[i][m][0]):
            ch4 = True
        else:
            ch4 = False
    if ch2 and ch4:
        goodrunslist.append(runlist[i])

这一行:

if ch2 and ch4:

您在第一个 for 循环的范围内引用了 ch2ch4,但它们是在内部 for 循环的范围内定义的。为了解决这个问题,首先在第一个 for 循环的范围内声明它们,方法是添加以下行:

ch2, ch4 = False, False

低于第一个 for 循环的开始。

更新:我建议更实用地执行此操作。这避免了使用突变创建局部变量。它还避免了嵌套循环和 ifs。这是编写代码的另一种方法:

# This function checks each list within the list to see if it should belong in goodrunslist
def checkRunListItem(runListItem):
      # this generator takes each item in the list and turns it into the boolean that your if statement
      # would use to decide to return true or false

      # any checks if any item in a list is true
      ch2 = any(re.match('[2][0-5][0-9]', item[0]) for item in runListItem)
      ch4 = any(re.match('[6][0-4][0-9]', item[0]) for item in runListItem)
      return ch2 and ch4
goodrunslist = filter(checkRunListItem, runlist)

但是你的情况看起来很奇怪,我不确定你想要达到什么目的。您在示例中提供的列表中的括号不匹配。此代码只会向 goodrunslist 添加一个项目,前提是该项目是列表列表的列表,其中基本列表中列表的第一个元素与每个正则表达式至少有一个匹配项。您能否更具体地说明您要解决的问题?

您的内部循环在完全相同的范围内,并且您对列表的索引相同,因此可以将它们合并。

此外,您不需要 range(),因为您可以直接遍历对象。

更重要的是,您正在检查列表的第一个元素(都是三位数字)以查看它们是否匹配 both 正则表达式,这永远不会是真的(数字不能同时以 26 开头)。我认为您打算使用 or

import re
runlist = [[['229', '134', '9'], ['229', '137', '5'], ['629', '140', '5'], ['631', '140', '5'], ['632', '140', '5'], ['633', '140', '8'], ['422', '141', '5'], ['628', '141', '9'], ['629', '141', '11']], [['629', '145', '8'], ['630', '145', '8'], ['631', '145', '8'], ['632', '145', '8'], ['632', '146', '5']]]
goodrunslist = []
for run in runlist:
    ch2, ch4 = False, False
    for r in run:
        ch2 = re.match('2[0-5][0-9]', r[0])
        ch4 = re.match('6[0-4][0-9]', r[0])
    if ch2 or ch4:
        goodrunslist.append(run)
print(goodrunslist)

但是,我认为这里的缩进是关闭的。您仅在 扫描完所有 i in len(runlist[i])val in run 之后附加 。因此,条件仅适用于 runlist

的最后一个元素

解决方案是进行检查并添加内部列表,而不是完整的运行列表

goodrunslist = []
for run in runlist:
    for r in run:
        ch2 = bool(re.match('2[0-5][0-9]', r[0]))
        ch4 = bool(re.match('6[0-4][0-9]', r[0]))
        if ch2 or ch4:
          goodrunslist.append(r)
print(goodrunslist)