Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Integer From A Float List?!?

Reply
Thread Tools

Re: Integer From A Float List?!?

 
 
Bill Mill
Guest
Posts: n/a
 
      03-04-2005
On Fri, 4 Mar 2005 22:35:48 +0100, http://www.velocityreviews.com/forums/(E-Mail Removed)
<(E-Mail Removed)> wrote:
> Hello NG,
>
> I was wondering if there is a way to obtain, from a list of floats,
> a list of integers without loops. Probably is a basic question, but I can't
> find an answer... I have had my eyes blinded by Matlab for years, but now
> that I discovered Python+wxPython there seems to be no limit on what one
> can do with these 2 tools. Anyway, following the Matlab style, I would like
> to do something like this:
>
> matrix = [1.5, 4.3, 5.5]
> integer_matrix = int(matrix) (float for Matlab)


You're going to have to use loops. I don't know how Matlab can do it
without them, unless it maintains the matrix as a list of floats and
simply *views* it as a list of ints. More likely, it simply hides the
loop away from you. Anyway, here's some ways to do it:

preferable: int_matrix = [int(x) for x in matrix]
old way: int_matrix = map(int, matrix)
explicit:
int_matrix = []
for x in matrix:
int_matrix.append(int(x))

Any of these methods should be neither really slow nor really fast,
but the list comprehension should be the fastest (I think). Anyway, if
you're going to be doing lots of large matrices, and want some of your
old matlab stuff, check out numpy and numarray at
http://numeric.scipy.org/ .

Also, somebody was recently posting on here about a python <-> matlab
bridge that they developed; you should search the archives for that
(it was in february, I think).

And, finally, when doing scientific stuff, I found IPython
(http://ipython.scipy.org/) to be an invaluable tool. It's a much
improved Python interpreter.

Peace
Bill Mill
bill.mill at gmail.com

>
> (In Matlab, "integer_matrix" is always a double anyway, here I would like
> only to show the vector-matrix operation).
>
> Obviously, Python complains about:
>
> Traceback (most recent call last):
> File "<interactive input>", line 1, in ?
> TypeError: int() argument must be a string or a number
>
> I would like to avoid loops because, having been blinded by Matlab vector-matrix
> abilities (and corresponding SLOW for-while loops operations), I tend to
> think that also Python will be slow if I use loops.
>
> Does anyone have a suggestion (or maybe could anyone show me that I'm wrong
> about loops?)
>
> Thanks you a lot.
>
> Andrea.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

 
Reply With Quote
 
 
 
 
George Sakkis
Guest
Posts: n/a
 
      03-05-2005

"Bill Mill" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Fri, 4 Mar 2005 22:35:48 +0100, (E-Mail Removed)
> <(E-Mail Removed)> wrote:
> > Hello NG,
> >
> > I was wondering if there is a way to obtain, from a list of floats,
> > a list of integers without loops. Probably is a basic question, but I can't
> > find an answer... I have had my eyes blinded by Matlab for years, but now
> > that I discovered Python+wxPython there seems to be no limit on what one
> > can do with these 2 tools. Anyway, following the Matlab style, I would like
> > to do something like this:
> >
> > matrix = [1.5, 4.3, 5.5]
> > integer_matrix = int(matrix) (float for Matlab)

>
> You're going to have to use loops. I don't know how Matlab can do it
> without them, unless it maintains the matrix as a list of floats and
> simply *views* it as a list of ints. More likely, it simply hides the
> loop away from you. Anyway, here's some ways to do it:
>
> preferable: int_matrix = [int(x) for x in matrix]
> old way: int_matrix = map(int, matrix)
> explicit:
> int_matrix = []
> for x in matrix:
> int_matrix.append(int(x))
>
> Any of these methods should be neither really slow nor really fast,
> but the list comprehension should be the fastest (I think). Anyway, if
> you're going to be doing lots of large matrices, and want some of your
> old matlab stuff, check out numpy and numarray at
> http://numeric.scipy.org/ .
>
> Also, somebody was recently posting on here about a python <-> matlab
> bridge that they developed; you should search the archives for that
> (it was in february, I think).
>
> And, finally, when doing scientific stuff, I found IPython
> (http://ipython.scipy.org/) to be an invaluable tool. It's a much
> improved Python interpreter.
>
> Peace
> Bill Mill
> bill.mill at gmail.com



Using numpy, your example would be:
>>> from Numeric import array
>>> matrix = array([1.5, 4.3, 5.5])
>>> integer_matrix = matrix.astype(int)


George


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
operator== (float, float) Jukka Lehtonen C++ 5 08-05-2004 08:28 AM
need code to convert float format to internal java float format which is kept in 4 bytes integer Andy Java 7 05-10-2004 09:26 PM
static_cast<float>(a) versus float(a) Jim West C++ 4 01-16-2004 12:36 PM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments