0

I am trying to figure out how to see the the data from the zipped content. The scenario is as follows in the code:

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)

output

array(<zip object at 0x00000166568AE648>, dtype=object)

but i want to see the data inside the zip so when i say

print(data)

it shows

<zip object at 0x00000166568AE648>

now people are discussing in similar posts about D_stacking like..

np.dstack(data)

but it shows the output as a error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-20-2126862e2c70> in <module>()
----> 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 dimension..? what does this mean..? it says the same for other possible solution i found as Decompressing zip

a,b,c=zip(*data)
print('x=',a)
print('y=',b)
print('z=',c)

error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-41-6ad67a911952> in <module>()
----> 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

then i went thrugh zip() function in programiz.com there i found set() function..

result = set(data)
print (result)

error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-39-2e1af52686b4> in <module>()
----> 1 result = set(data)
      2 print (result)

TypeError: iteration over a 0-d array

i believe I'm doing something bad in basic x,y,z data.. but my knowledge is limited to the level of whatever you think about me..

could you please help me with things I'm missing and also explain that lil important point i missed.


When I search for TypeError: iteration over a 0-d array

people talk about json which I have 0 idea other then FC3, sorry for that.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • 2
    The `zip` function in Python has nothing whatsoever to do with compression. – user2357112 Oct 10 '17 at 17:59
  • What were you actually trying to do? Did you *want* to compress something? Why were you calling `zip` at all? What's your desired output? – user2357112 Oct 10 '17 at 18:01
  • You seem to be under the misapprehension that `zip` is compressing your data. It is *not*. It is performing the [functional programming operation](https://stackoverflow.com/questions/1115563/what-is-zip-functional-programming) "zip". This takes two sequences and returns a pair of each of their elements at the same position. Like a "zipper". It has nothing to do with the .zip archive file format. – juanpa.arrivillaga Oct 10 '17 at 18:05
  • now that helped me a lot @juanpa.arrivillaga, –  Oct 10 '17 at 18:25
  • @AhmedAbdulSalam yes, generally, you need to *materialize* the objects into something that numpy will understand, ideally, a sequence of sequences, like a `list` of `lists` if you are passing the `np.array` constructor something. A `zip` object is not something it will understand, and indeed, a `zip` object is potentially an infinite iterator. – juanpa.arrivillaga Oct 10 '17 at 18:29
  • Can you explain me about iteration over a 0 d array..? –  Oct 10 '17 at 18:40
  • Also in Py3, `zip` returns a `zip object`, not a list. It's a generator. `list(zip(...))` is required if you want a list. – hpaulj Oct 10 '17 at 19:36

2 Answers2

2

zip of a tuple of lists is a kind of list transpose:

In [83]: x=[1,2,3]
    ...: y=[4,5,6]
    ...: z=[7,8,9]
    ...: 
In [84]: zip(x,y,z)
Out[84]: <zip at 0xaf79f84c>

But in Py3, zip is generator like; you have to wrap the result in list (or iterate on it) to get a list:

In [85]: list(zip(x,y,z))
Out[85]: [(1, 4, 7), (2, 5, 8), (3, 6, 9)]

So far I'm not using numpy, just plain Python. zip documentation: https://docs.python.org/3/library/functions.html#zip

You can make an array from such a list

In [86]: np.array(list(zip(x,y,z)))
Out[86]: 
array([[1, 4, 7],
       [2, 5, 8],
       [3, 6, 9]])

Trying to make an array from the zip produces a single element array, 0d, that contains a pointer to this zip object. There's little point doing anything more with this.

In [87]: np.array(zip(x,y,z))
Out[87]: array(<zip object at 0xaf75d8cc>, dtype=object)

But to get a 2d matrix from these lists it is easier, or at least more direct, to use some version of concatenate, which takes a list of arrays or lists. stack for example lets you specify a new axis to join things:

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(<zip object at 0xabee412c>, dtype=object)

You can pull an item out of a 0d array with a [()] index. data.item() also works:

In [99]: data[()]
Out[99]: <zip at 0xabee412c>

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.

hpaulj
  • 221,503
  • 14
  • 230
  • 353
0

Do you need to zip the data? You can pass NumPy a list of lists, e.g.:

import numpy as np
x=[1,2,3]
y=[4,5,6]
z=[7,8,9]
data=np.array([x,y,z])
print (data)

output:

[[1 2 3]
 [4 5 6]
 [7 8 9]]
Evan
  • 2,121
  • 14
  • 27
  • point here is i executed the code data=np.array(zip(x,y,z)) now i want to see the data that is stored in that 'data' how can i see..? –  Oct 10 '17 at 18:12
  • @AhmedAbdulSalam there **is no data stored there**. You've made a `numpy` array with `dtype=object` that contains a `zip` object. That *is the data in the array*. – juanpa.arrivillaga Oct 10 '17 at 18:16
  • you have cleared my confusion by far to a level.. could you please consider giving me a reference link through which i can understand the terms you are talking about..? YT link would be helpful. Ty. –  Oct 10 '17 at 18:27
  • https://stackoverflow.com/questions/31683959/the-zip-function-in-python-3 That may be a helpful intro to zip(). – Evan Oct 10 '17 at 18:36
  • If only you could guide me.. I only have the above main code.. can u please tell me what knowledge am I supposed to be achieving? A goal? –  Oct 10 '17 at 18:41