Python 类型错误:zip 参数 #2 必须支持迭代
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42943529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: zip argument #2 must support iteration
提问by user21063
I got an error TypeError: zip argument #2 must support iteration.
我收到一个错误类型错误: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)
I think the part of zip(y, array_x, array_y)
is wrong because zip's arguments are not list type,so I wrote
我认为部分zip(y, array_x, array_y)
是错误的,因为 zip 的参数不是列表类型,所以我写了
for i in y:
li_result = []
li_result = zip(y, array_x[i], array_y[i])
but I got an error,
但我有一个错误,
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
So, I cannot understand how to fix this. What should I do?
所以,我无法理解如何解决这个问题。我该怎么办?
回答by Crispin
It sounds like you have three arrays itemNameList
, array_x
, and array_y
听起来您有三个数组itemNameList
, array_x
, 和array_y
Assuming they are all the same shape, you can just do:
假设它们都是相同的形状,你可以这样做:
zipped = zip(itemNameList,array_x,array_y)
li_result = list(zipped)
EDIT
编辑
Your problem is that array_x
and array_y
are not actual numpy.array
objects, but likely numpy.int32
(or some other non-iterable) objects:
您的问题是array_x
并且array_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__'
Perhaps their initialization is not going as expected, or they are being changed from arrays somewhere in your code?
也许它们的初始化没有按预期进行,或者它们是从代码中某处的数组更改的?
回答by hpaulj
Here's how zip
(or list(zip in py3), can turn several lists into a list of tuples:
这是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)]