TypeError:zip参数#2必须支持迭代

时间:2017-03-22 05:35:46

标签: python numpy matplotlib scipy

我收到错误TypeError:zip参数#2必须支持迭代。

data = libraries.pd.read_csv('a.csv',header=1, parse_dates=True)
datas = DataCleaning.DataCleaning(data)
datas.cleaning(media)

calDf = datas.getDatas()

array_x = libraries.np.int32(libraries.np.zeros(len(calDf)))
array_y = libraries.np.int32(libraries.np.zeros(len(calDf)))


if len(calDf) > 1:
    for num in range(len(calDf)):
        array_x[num] = calDf.iloc[num,0]
        array_y[num] = calDf.iloc[num,1]

    def nonlinear_fit(x,a,b):
        return  b * libraries.np.exp(x / (a+x))

    prameter_initial = libraries.np.array([0,0])

    try:
        param, cov = libraries.curve_fit(nonlinear_fit, array_x, array_y, maxfev=5000)

    except RuntimeError:
        print("Error - curve_fit failed")

li_result = []
li_result = zip(y, array_x, array_y)

我认为zip(y, array_x, array_y)的部分是错误的,因为zip的参数不是列表类型,所以我写了

for i in y:
 li_result = []
 li_result = zip(y, array_x[i], array_y[i])

但我收到了错误,

li_result = zip(y, array_x[i], array_y[i])
IndexError: only integers, slices (`:`), ellipsis (`...`),
numpy.newaxis (`None`) and integer or boolean arrays are valid indices

所以,我无法理解如何解决这个问题。 我该怎么办?

2 个答案:

答案 0 :(得分:1)

听起来你有三个数组itemNameListarray_xarray_y

假设它们的形状相同,你可以这样做:

zipped = zip(itemNameList,array_x,array_y)
li_result = list(zipped)

修改

您的问题是array_xarray_y不是实际的numpy.array个对象,但可能是numpy.int32(或其他一些不可迭代的)对象:

array_x = np.int32(np.zeros(None))
array_x.shape
# ()
array_x.__iter__
# AttributeError: 'numpy.int32' object has no attribute '__iter__'

也许他们的初始化没有按预期进行,或者它们是从代码中某处的数组更改的?

答案 1 :(得分:0)

这里有zip(或列表(py3中的zip))如何将多个列表转换为元组列表:

In [76]: y = ['item1', 'item2','item3']  # list of strings
In [77]: xdata = [100,200,300]         # list of numbers
In [78]: ydata = np.array([1000,2000,3000])  # equivalent array of numbers
In [79]: list(zip(y,xdata,ydata))
Out[79]: [('item1', 100, 1000), ('item2', 200, 2000), ('item3', 300, 3000)]