我在一个列表中有多个列表,并且希望检查多个事情,并使用if条件和True或False形成一个新列表。我的代码:
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循环中,它给了我一个ch2和ch4未定义的NameError,因此它不能在新列表中追加任何内容。我怎么才能解决这个问题?
编辑:我已经通过下面的代码修复了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])发布于 2018-02-21 06:14:10
在这条线上:
if ch2 and ch4:
您可以在第一个for循环的作用域中引用ch2和ch4,但它们是在内部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添加一个项,其中基列表中的列表的第一个元素至少匹配每个正则表达式。你能更具体地说明你想要解决的问题吗?
发布于 2018-02-21 07:43:32
您的内部循环位于完全相同的范围内,并且您对列表进行了相同的索引,因此可以将它们组合起来。
另外,您不需要range(),因为您可以直接迭代对象。
更重要的是,您正在检查列表的前几个元素(全部为三位数),以确定它们是否匹配--这两个正则表达式都不是真(数字不能同时以2和6开头)。我想你是想用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)https://stackoverflow.com/questions/48899123
复制相似问题