在我将 if 语句设置为 true 后,我的循环不断中断
My loop keeps breaking after I hit the if statement as true
我是 运行 这段代码,出于某种原因,当数组中有 2 个元素应该命中 "if not inc" 语句时,它只弹出 1 个元素。
我已经使用 prints 进行调试,似乎循环在第一次 "if not inc" == true 之后中断,我不希望这种情况发生。它应该继续循环并再次返回那里。
已尝试通过并继续,但仍然没有预期的结果
def readFile():
with open('incidents.txt') as f:
x = f.read().splitlines()
print(x)
i = 0
for inc in x:
print(i)
if not inc:
x.pop(i)
print("if command")
pass
i = i + 1
print(x)
y = x
return y
Original Array -
['INC123123123', 'INC222222222', 'INC333333333', 'INC444444444', 'INC555555555', '', '']
Expected result is -
['INC123123123', 'INC222222222', 'INC333333333', 'INC444444444', 'INC555555555']
Actual Result is -
['INC123123123', 'INC222222222', 'INC333333333', 'INC444444444', 'INC555555555', '']
使用像 i
这样的计数器变量,并执行 i + 1
,在 Python 中不是一个好的做法。阅读 Ned Batchelder 的 Loop Like A Native。每当您编写 Python(尤其是循环)时,看看是否有一种 惯用的 方法来实现它。这就是像 Python 这样的语言真正的美。另外,避免在循环时修改列表的大小。
如果您只需要删除空字符串,只需使用 filter(None, x)
。
xStr = ['INC123123123', 'INC222222222', 'INC333333333',
'INC444444444', 'INC555555555', '', '']
print(list(filter(None, xStr)))
filter
的签名是(function_to_apply, list_of_inputs)
。所以,如果你使用
lambda x: x != ''
那么它只会 select 非空字符串。与使用 None
相比,这是一种更冗长的方式。如果要反转它,请使用
lambda x: x == ''`
因此,如果您使用 list(filter(lambda x: x == '', xStr))
,它只会 select 空字符串。
这个怎么样?它过滤掉所有长度为 0
的字符串
def foo():
x = ['INC123123123', 'INC222222222', 'INC333333333', 'INC444444444', 'INC555555555', '', '']
x = list(filter(len, x))
y = x
return y
print(foo())
您试图在遍历列表时修改列表,这会导致问题。相反,您应该执行以下操作:
def readFile():
with open('incidents.txt') as f:
x = f.read().splitlines()
print(x)
y = [a for a in x if a]
return y
也许您应该改用 if inc == ''
。
使用列表理解的一行代码:
xStr = ['INC123123123', 'INC222222222', 'INC333333333', 'INC444444444',
'INC555555555', '', '']
print([x for x in xStr if x])
输出:
['INC123123123', 'INC222222222', 'INC333333333', 'INC444444444', 'INC555555555']
您将使用过滤函数来删除空白字符串。
def readFile():
with open('try.txt') as f:
x = f.read().splitlines()
print(x)
i = 0
y = list(filter(None, x))
return y
使用过滤器删除您要删除的特定类型的字符串。您可以使用 lambda 和过滤器来添加条件以删除任何特定类型的字符串。
line = ['INC123123123', 'INC222222222', 'INC333333333',
'INC444444444', 'INC555555555', '', '']
line_mod = filter(lambda x: x != '', line)
print line_mode
输出:
['INC123123123', 'INC222222222', 'INC333333333', 'INC444444444', 'INC555555555']
在迭代列表时从列表中删除元素不是一个好主意。
尝试:
def readFile():
with open('incidents.txt') as f:
result = []
for line in f:
line = line.strip()
if line:
result.append(line)
return result
我是 运行 这段代码,出于某种原因,当数组中有 2 个元素应该命中 "if not inc" 语句时,它只弹出 1 个元素。
我已经使用 prints 进行调试,似乎循环在第一次 "if not inc" == true 之后中断,我不希望这种情况发生。它应该继续循环并再次返回那里。
已尝试通过并继续,但仍然没有预期的结果
def readFile():
with open('incidents.txt') as f:
x = f.read().splitlines()
print(x)
i = 0
for inc in x:
print(i)
if not inc:
x.pop(i)
print("if command")
pass
i = i + 1
print(x)
y = x
return y
Original Array -
['INC123123123', 'INC222222222', 'INC333333333', 'INC444444444', 'INC555555555', '', '']
Expected result is -
['INC123123123', 'INC222222222', 'INC333333333', 'INC444444444', 'INC555555555']
Actual Result is -
['INC123123123', 'INC222222222', 'INC333333333', 'INC444444444', 'INC555555555', '']
使用像 i
这样的计数器变量,并执行 i + 1
,在 Python 中不是一个好的做法。阅读 Ned Batchelder 的 Loop Like A Native。每当您编写 Python(尤其是循环)时,看看是否有一种 惯用的 方法来实现它。这就是像 Python 这样的语言真正的美。另外,避免在循环时修改列表的大小。
如果您只需要删除空字符串,只需使用 filter(None, x)
。
xStr = ['INC123123123', 'INC222222222', 'INC333333333',
'INC444444444', 'INC555555555', '', '']
print(list(filter(None, xStr)))
filter
的签名是(function_to_apply, list_of_inputs)
。所以,如果你使用
lambda x: x != ''
那么它只会 select 非空字符串。与使用 None
相比,这是一种更冗长的方式。如果要反转它,请使用
lambda x: x == ''`
因此,如果您使用 list(filter(lambda x: x == '', xStr))
,它只会 select 空字符串。
这个怎么样?它过滤掉所有长度为 0
的字符串def foo():
x = ['INC123123123', 'INC222222222', 'INC333333333', 'INC444444444', 'INC555555555', '', '']
x = list(filter(len, x))
y = x
return y
print(foo())
您试图在遍历列表时修改列表,这会导致问题。相反,您应该执行以下操作:
def readFile():
with open('incidents.txt') as f:
x = f.read().splitlines()
print(x)
y = [a for a in x if a]
return y
也许您应该改用 if inc == ''
。
使用列表理解的一行代码:
xStr = ['INC123123123', 'INC222222222', 'INC333333333', 'INC444444444',
'INC555555555', '', '']
print([x for x in xStr if x])
输出:
['INC123123123', 'INC222222222', 'INC333333333', 'INC444444444', 'INC555555555']
您将使用过滤函数来删除空白字符串。
def readFile():
with open('try.txt') as f:
x = f.read().splitlines()
print(x)
i = 0
y = list(filter(None, x))
return y
使用过滤器删除您要删除的特定类型的字符串。您可以使用 lambda 和过滤器来添加条件以删除任何特定类型的字符串。
line = ['INC123123123', 'INC222222222', 'INC333333333',
'INC444444444', 'INC555555555', '', '']
line_mod = filter(lambda x: x != '', line)
print line_mode
输出:
['INC123123123', 'INC222222222', 'INC333333333', 'INC444444444', 'INC555555555']
在迭代列表时从列表中删除元素不是一个好主意。
尝试:
def readFile():
with open('incidents.txt') as f:
result = []
for line in f:
line = line.strip()
if line:
result.append(line)
return result