[Numpy-discussion] convert array to tuples?

Christopher Barker Chris.Barker at noaa.gov
Tue Apr 5 13:02:05 EDT 2011


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



More information about the NumPy-Discussion mailing list