K = 3
A = 2
choices = [i for i in range(-1, K)]
list_K_G = np.array([[0, 1, 2], [0, 2, 2]])
print("list_K_G:n{:}".format(list_K_G))
print("---删除已经达到A的选择---")
print("删除前 choices:{:}".format(choices))
for l in choices:
print("l:{:}".format(l))
print("list_K_G[1][{:}]:{:}".format(l, list_K_G[1][l]))
if(l != -1 and list_K_G[1][l] == A):
choices.remove(l)
print("删除后 choices:{:}".format(choices))
print("---删除已经达到A的选择---")
输出: list_K_G: [[0 1 2] [0 2 2]] ---删除已经达到A的选择--- 删除前 choices:[-1, 0, 1, 2] l:-1 list_K_G[1][-1]:2 l:0 list_K_G[1][0]:0 l:1 list_K_G[1][1]:2 删除后 choices:[-1, 0, 2] ---删除已经达到A的选择---
为什么不会遍历l=2呢?因为remove之后索引会加1,原来[-1,0,1,2]中1的索引位置是2,删除以后变成了[-1,0,2],此时索引位置是2的值变成了2,for循环以为索引位置2已经遍历过了。
于是我只能把查找到的需要删除的元素加入一个列表,再根据这个列表删除元素。
K = 3
A = 2
choices = [i for i in range(-1, K)]
list_K_G = np.array([[0, 1, 2], [0, 2, 2]])
print("list_K_G:n{:}".format(list_K_G))
delete_array = []
print("---删除已经达到A的选择---")
print("删除前 choices:{:}".format(choices))
for l in choices:
print("l:{:}".format(l))
print("list_K_G[1][{:}]:{:}".format(l, list_K_G[1][l]))
if(l != -1 and list_K_G[1][l] == A):
delete_array.append(l)
for i in delete_array:
choices.remove(i)
print("删除后 choices:{:}".format(choices))
print("---删除已经达到A的选择---")
输出: list_K_G: [[0 1 2] [0 2 2]] ---删除已经达到A的选择--- 删除前 choices:[-1, 0, 1, 2] l:-1 list_K_G[1][-1]:2 l:0 list_K_G[1][0]:0 l:1 list_K_G[1][1]:2 l:2 list_K_G[1][2]:2 删除后 choices:[-1, 0] ---删除已经达到A的选择---
参考资料:https://www.cnblogs.com/xiaofeng91/p/11968089.html



