943,921 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 10922
  • Python RSS
Feb 13th, 2009
0

convert list of int to floats

Expand Post »
Given a list of integers, generate the list of the corresponding floats.
Similar Threads
Reputation Points: 16
Solved Threads: 0
Light Poster
karthik.c is offline Offline
48 posts
since Feb 2009
Feb 13th, 2009
0

Re: convert list of int to floats

you can use a list comprehension:

python Syntax (Toggle Plain Text)
  1. new_list = [float(integral) for integral in old_list]
Last edited by scru; Feb 13th, 2009 at 9:00 am.
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007
Feb 13th, 2009
0

Re: convert list of int to floats

hi scru,
it wud b more helpful if u can explain the above sol for this scenario
suppose if i give
>>> l1=[1,2,3,4,5]
>>> l1
[1, 2, 3, 4, 5]
i want l1 to print [1.0,2.0,3.0.....](in float)
Reputation Points: 16
Solved Threads: 0
Light Poster
karthik.c is offline Offline
48 posts
since Feb 2009
Feb 13th, 2009
0

Re: convert list of int to floats

Then you would do:
python Syntax (Toggle Plain Text)
  1. l1 = [1,2,3,4,5]
  2. l2 = [float(i) for i in l1]
  3. print l2

[float(i) for i in l1] is what is known as a list comprehension. You should be already familiar with the for loop which loops through each integer in the list and assigns it to the variable i. The first expression simply tells the comprehension what value to append to the new list; the value here is float(i). So essentially, the comprehension loops through each integer in the list, converts it to a float, and then appends it to a new one which is the assigned to l2.
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007
Feb 13th, 2009
0

Re: convert list of int to floats

Just an observation, I would avoid using variable names like l1, l2 and so on, they look a lot like numbers 11, 12.

Here is an example how to use Python function map():
python Syntax (Toggle Plain Text)
  1. q = [1, 2, 3, 4, 5]
  2.  
  3. # map applies float() to every element in the list q
  4. qf = map(float, q)
  5.  
  6. print(qf) # [1.0, 2.0, 3.0, 4.0, 5.0]
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Feb 14th, 2009
0

Re: convert list of int to floats

thanks scru and ene...

i think i've to go through basics well..and which book do u think is best and easy to learn for beginners in python??
Reputation Points: 16
Solved Threads: 0
Light Poster
karthik.c is offline Offline
48 posts
since Feb 2009
Feb 14th, 2009
0

Re: convert list of int to floats

There are a flock of badly written and rather outdated Python books on the market. I would go for some of the free online books.

Swaroop C.H. has rewritten his excellent beginning Python tutorial for Python30:
http://www.swaroopch.com/notes/Pytho...le_of_Contents

How to Think Like a Computer Scientist - Learning with Python
written by a college professor, a high school teacher, and a professional programmer:
http://www.thinkpython.com
Foreword by David Beazley, University of Chicago:
http://www.greenteapress.com/thinkpy.../foreword.html
http://www.ibiblio.org/obp/thinkCSpy/

Above updated: "Think like a Python Programmer" (PDF format):
http://greenteapress.com/thinkpython/thinkPP.pdf
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Feb 16th, 2009
0

Re: convert list of int to floats

I really like the text we used for my high-school level class: Python Programming for the Absolute Beginner. Two caveats -- it's game-oriented, and it doesn't use Python 3.0.

That said, it contains clear explanations and models reasonably good programming practices.

Jeff
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Feb 16th, 2009
0

Re: convert list of int to floats

I second jrcagle's recommendation. I used it in high school too. It's a nice lighthearted approach that explains the major concepts of object-oriented programming fairly well, as well as a pretty good introduction to Python itself.

I remember actually looking forward to reading ahead with this book in my free time.
Last edited by scru; Feb 16th, 2009 at 6:17 pm.
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Code correction - counting characters.
Next Thread in Python Forum Timeline: How to change reorder the elements of a file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC