[Numpy-discussion] convert array to tuples?

Mark S Niemczyk mniemczyk at mac.com
Tue Apr 5 14:49:53 EDT 2011


Just a suggestion (I am very new to Numpy), but wouldn't

	draw.line(tuple(points.tolist()))

accomplish what you want.

Based on my reading, the ndarray.tolist method would return a Python list of elements; then Python's built-in tuple function would complete the conversion.


Regards,

	Mark


On Apr 5, 2011, at 12:02 PM, Christopher Barker wrote:

> HI folks,
> 
> What's the fastest way to convert a numpy array to tuples?
> 
> Unfortunately, not all packages take arbitrary sequences when they are 
> expecting a list of tuples. In this case, I'm using PIL's ImageDraw, and 
> want to do:
> 
> draw.line(points)
> 
> (points is an Nx2 numpy array of ints)
> 
> but if I pass in an array, I get, oddly enough, nothing. NO error, just 
> nothing drawn.
> 
> if I do:
> 
> draw.line(points.tolist())
> 
> SystemError: new style getargs format but argument is not a tuple
> 
> So I seem to need tuples. This works:
> 
> draw.line([tuple(p) for p in points.tolist()])
> 
> but that seems like it would be slower than it should be (to be honest, 
> not profiled).
> 
> Is there a faster way?
> 
> Maybe numpy should have a ndarray.totuple() method.
> 
> -Chris
> 
> 
> 
> 
> 
> 
> -- 
> Christopher Barker, Ph.D.
> Oceanographer
> 
> Emergency Response Division
> NOAA/NOS/OR&R            (206) 526-6959   voice
> 7600 Sand Point Way NE   (206) 526-6329   fax
> Seattle, WA  98115       (206) 526-6317   main reception
> 
> Chris.Barker at noaa.gov
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion




More information about the NumPy-Discussion mailing list