【发布时间】:2019-03-31 16:56:17
【问题描述】:
我有一个列表,其中的列表如下所示:
c = [[1,0,1], [2,3,1], [0,0,0]]
我做了一个函数toss_non_G2(potential_list),看起来像这样:
def toss_non_G2(potential_list):
for list in potential_list:
if any(x not in [0,1] for x in list):
potential_list.remove(list)
return potential_list
我试过了:
print(toss_non_G2(c))
>>> [[1, 0, 1], [0, 0, 0]]
正如我所料。但是,然后我在一些更复杂的东西上对其进行了测试,因此 问题:
import numpy as np
from scipy.linalg import solve
A = [[0.8760162 , 0.0571752 , 0.43403856], [0.04730946, 0.56278686, 0.05767958], [0.95880316, 0.9595227 , 0.02226051]]
b_list = [[1, 0, 1], [1, 1, 1]]
x_list = list()
for b in b_list:
x = solve(A, b)
x_list.append(x)
print(toss_non_G2(x_list))
>>> [array([-0.52792558, 1.49611693, 3.17236923])]
在这种情况下,我期望一个空列表。为什么不是所有值不同于 0 或 1 的列表(如 if 语句)都被删除?它与列表是一个numpy-array有关吗?
【问题讨论】: