On 11/11/10 11:48 PM, Jonathan Hartley wrote:
> What is the fastest way in python to build a c array from a list of
> tuples of floats? The context: my Python code pass arrays of 2D vertices
> to OpenGL.
This seems like an obvious use for numpy:
> points = [(random(), random()) for _ in xrange(1000)]
def array_numpy(points):
return np.array(points, dtype=float32)
And you can save the overhead of the function call, since it's a
one-liner anyway. Of course, if you're using numpy, you probably never
would have had all those points in tuples in the first place -- you'd
just use numpy arrays from the start.
> Any other alternatives?
There's also array.array:
def array_array(points):
a = array.array('f')
[a.extend(p) for p in points]
return a
Which brings up a question I have: is there way to write a "nothing
comprehension"? -- I use the list comprehension syntax, as it's a nice
clean way to process all the items in a list -- but I don't need the
resulting list -- it seems there should be a way to write that without
the overhead of creating a useless list.
-Chris
> -----
> http://stackoverflow.com/q/4156872/10176
>
>
> He's already chosen one answer, but I think that was premature, and some
> of you folks might have substantially better ideas. I'm thinking of a
> fully static-typed Cython function, but I apparently lack the smarts to
> get it working over breakfast, and now I have to go to work.
>
> Jonathan
>
--
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
Chr...@no...
|