[Tutor] itertools.izip question?

Kent Johnson kent37 at tds.net
Mon Oct 15 17:41:38 CEST 2007


Iyer wrote:
> 
> 
> I have a dictionary
> 
> d = {0: array([0, 0], dtype=uint16), 1: array([1, 1], dtype=uint16), 2: 
> array([2, 2], dtype=uint16), 3: array([3, 3], dtype=uint16)}
> or
> d = {0:d0,1:d1,2:d2,3:d3}
> 
> 
> d0, d1, d2 and d3 are numpy.ndarray type
> 
> I wish to get the interleaved data by using  
> 
> outputdata = numpy.array([item for items in itertools.izip(d.values()) 
> for item in items])

Try itertools.izip(*d.values())

What you are doing is passing a single list to izip(), just as if you 
said itertools.izip([d0,d1,d2,d3]). (Note the extra brackets compared to 
your sample below.)

The *d.values() means, use this list as the parameter list (instead of 
as a single parameter).

Kent
> 
> But I get outputdata = array([[0, 0],       [1, 1],      [2, 2],       
> [3, 3]], dtype=uint16)
> 
> which is different from the desired answer, which can be obtained by
> 
> outputdata = numpy.array([item for items in itertools.izip(d0,d1,d2,d3) 
> for item in items])
> 
> outputdata = array([0, 1, 2, 3, 0, 1, 2, 3], dtype=uint16)


More information about the Tutor mailing list