压缩两个和两个以上列表之间有什么区别吗?

Is any difference between zip two and more than two lists?

我认为这是一个非常微妙的问题,可能是 Python2.7 中的一个未知错误。我正在制作一个交互式应用程序。它应该使 WLS(加权线性回归)模型适合点云。在开始时,脚本从文本文件中读取数据(只是一个简单的 table,其中包含每个点的索引、值和错误)。但在数据中可能有一些点带有 nocompl=99.9999 标记的 NULL 值。在脚本开始拟合之前,我必须知道这些点是拒绝它们的哪些点。我通过以下方式执行此操作:

# read the data from input file
Bunchlst = [Bunch(val1 = D[:,i], err_val1 = D[:,i+1], val2 = D[:,i+2], err_val2 = D[:,i+3]) for i in range(1, D.shape[1] - 1, 4)]

# here is the problem
for b in Bunchlst:
    b.compl = list(np.logical_not([1 if nocompl in [im,ie,sm,se] else 0 for v1,e1,v2,e2 in zip(b.val1,b.err_val1,b.val2,b.err_val2)]))

# fit the model to the "good" points
wls = sm.WLS(list(compress(b.val1,b.compl)), sm.add_constant(list(compress(b.val2,b.compl)), prepend=False), weights=[1.0/i for i in list(compress(b.err_val2,b.compl))]).fit()

WLS model implemented in Python.compress() 允许过滤数据(忽略 NULL 值)。但是这种情况会产生错误:

wls = sm.WLS(...).fit() AttributeError: 'numpy.float64' object has no attribute 'WLS'

我进行了调查,当我只压缩两个列表时,问题消失了,WLS 模型自身计算正确:

for b in Bunchlst:
    b.compl = list(np.logical_not([1 if nocompl in [v1,v2] else 0 for v1,v2 in zip(b.val1,b.val2)]))

我写道,这可能是一个错误,因为我在这两种情况下都检查了 b.compl。始终存在具有 TrueFalse 值的相同列表(取决于输入文件中的数据)。此外,简单的考虑导致它必须适用于许多列表:

>>> K = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
>>> L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> M = [21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
>>> N = [32, 33, 34, 35, 36, 37, 38, 39, 40, 32]

>>> [1 if 26 in [k,l,m,n] else 0 for k,l,m,n in zip(K,L,M,N)]
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0]

祝一切顺利,

贝内克

不,zip() 处理 2 个或更多列表的方式没有区别。相反,您的列表理解分配给循环中的名称 sm,同时您使用名称 sm 来引用 statsmodels 模块。

你的简单的双列表版本不会这样做,所以名字 sm 不会反弹,你不会 运行 进入这个问题。

在Python2中,列表理解中使用的名称是局部范围的一部分:

>>> foo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'foo' is not defined
>>> [foo for foo in 'bar']
['b', 'a', 'r']
>>> foo
'r'

这里的名字foo是在list comprehension的for循环中设置的,循环后名字仍然可用

重命名您的导入,或重命名您的循环变量。