Need help with an array problem.
Robert Kern
robert.kern at gmail.com
Mon Oct 2 21:35:32 EDT 2006
SpreadTooThin wrote:
> Robert Kern wrote:
>> jakobsg at gmail.com wrote:
>>> To your question on casting long to short. This is how:
>>> a=1234L # long
>>> b=int(a) # int (short)
>> No, a Python int is a C long. A Python long is an arbitrary-precision number and
>> does not correspond to any C type.
>
> So there is no short(number) casting?
Not in core Python, no, since C short ints have no Python type directly
corresponding to them. As John Machin pointed out, if you had tried what you
proposed, it would have worked just fine.
If you find that you keep needing to deal with the various C integer and
floating point types (and arrays of such), you might want to consider using numpy.
http://numpy.scipy.org
In [15]: import numpy
In [16]: a = numpy.array([65537], dtype=numpy.uint32)
In [17]: a
Out[17]: array([65537], dtype='uint32')
In [18]: b = a.astype(numpy.uint16)
In [19]: b
Out[19]: array([1], dtype='uint16')
In [20]: numpy.uint16(a[0])
Out[20]: 1
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
More information about the Python-list
mailing list