我试图弄清楚如何从压缩后的内容中查看数据。该方案在代码中如下所示:
import numpy as np
x=[1,2,3]
y=[4,5,6]
z=[7,8,9]
data=np.array(zip(x,y,z))
print (data)
输出
array(, dtype=object)
但我想查看zip内的数据,所以当我说
print(data)
表明
现在人们在关于D_stacking的类似帖子中进行讨论。
np.dstack(data)
但它显示输出为错误
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in ()
----> 1 np.dstack(data)
C:\ProgramData\Anaconda3\lib\site-packages\numpy\lib\shape_base.py in dstack(tup)
407
408 """
--> 409 return _nx.concatenate([atleast_3d(_m) for _m in tup], 2)
410
411 def _replace_zero_by_x_arrays(sub_arys):
TypeError: iteration over a 0-d array
0维..?这是什么意思..?对于其他我找到的解压缩zip来说,它说的也是一样
a,b,c=zip(*data)
print('x=',a)
print('y=',b)
print('z=',c)
错误
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in ()
----> 1 a,b,c=zip(*data)
2 print('x=',a)
3 print('y=',b)
4 print('z=',c)
TypeError: iteration over a 0-d array
然后我去了programiz.com上的节制zip()功能,在那里我找到了set()功能。
result = set(data)
print (result)
错误
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in ()
----> 1 result = set(data)
2 print (result)
TypeError: iteration over a 0-d array
我相信我在基本的x,y,z数据上做得不好。.但是我的知识仅限于您对我的看法。
您能帮我解决我所缺少的事情吗,并解释我错过的重要内容。
当我搜寻TypeError: iteration over a 0-d array
人们谈论json我除了FC3之外还有0个想法,对此表示抱歉。
解决方案
zip列表元组是列表转置的一种:
In [83]: x=[1,2,3]
...: y=[4,5,6]
...: z=[7,8,9]
...:
In [84]: zip(x,y,z)
Out[84]:
但是在Py3中,zip是生成器之类的;您必须将结果包装在其中list(或对其进行迭代)以获取列表:
In [85]: list(zip(x,y,z))
Out[85]: [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
您可以从这样的列表中创建一个数组
In [86]: np.array(list(zip(x,y,z)))
Out[86]:
array([[1, 4, 7],
[2, 5, 8],
[3, 6, 9]])
尝试从中zip生成一个数组,将生成一个包含指向this的指针的单元素数组0dzip object。这样做毫无意义。
In [87]: np.array(zip(x,y,z))
Out[87]: array(, dtype=object)
但是,要从这些列表中获取2d矩阵,使用某种版本的会更容易,或者至少更直接,它使用concatenate数组或列表的列表。stack例如,您可以指定一个新的轴来连接事物:
In [88]: np.stack((x,y,z)) # same as np.array((x,y,z))
Out[88]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
In [89]: np.stack((x,y,z),axis=1)
Out[89]:
array([[1, 4, 7],
[2, 5, 8],
[3, 6, 9]])
A common use of zip is to iterate on several lists at once:
In [92]: for i,j,k in zip(x,y,z):
...: print(i,j,k,i+j+k)
...:
1 4 7 12
2 5 8 15
3 6 9 18
From the comments you seem to be focused on fetching values from this array:
In [97]: data = np.array(zip(x,y,z))
In [98]: data
Out[98]: array(, dtype=object)
You can pull an item out of a 0d array with a [()] index. data.item() also works:
In [99]: data[()]
Out[99]:
Now you can apply list and see the items:
In [100]: list(data[()])
Out[100]: [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
zip doesn't actually contain its arguments. Rather it has references to the original lists. That's true of the data arrays as well.