convert list of int to floats

Thread Solved

Join Date: Feb 2009
Posts: 48
Reputation: karthik.c is an unknown quantity at this point 
Solved Threads: 0
karthik.c's Avatar
karthik.c karthik.c is offline Offline
Light Poster

convert list of int to floats

 
0
  #1
Feb 13th, 2009
Given a list of integers, generate the list of the corresponding floats.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,617
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 136
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Re: convert list of int to floats

 
0
  #2
Feb 13th, 2009
you can use a list comprehension:

  1. new_list = [float(integral) for integral in old_list]
Last edited by scru; Feb 13th, 2009 at 8:00 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 48
Reputation: karthik.c is an unknown quantity at this point 
Solved Threads: 0
karthik.c's Avatar
karthik.c karthik.c is offline Offline
Light Poster

Re: convert list of int to floats

 
0
  #3
Feb 13th, 2009
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)
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,617
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 136
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Re: convert list of int to floats

 
0
  #4
Feb 13th, 2009
Then you would do:
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,593
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 187
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: convert list of int to floats

 
0
  #5
Feb 13th, 2009
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():
  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]
drink her pretty in burbank
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 48
Reputation: karthik.c is an unknown quantity at this point 
Solved Threads: 0
karthik.c's Avatar
karthik.c karthik.c is offline Offline
Light Poster

Re: convert list of int to floats

 
0
  #6
Feb 13th, 2009
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??
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,593
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 187
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: convert list of int to floats

 
0
  #7
Feb 14th, 2009
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
drink her pretty in burbank
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 152
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: convert list of int to floats

 
0
  #8
Feb 16th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,617
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 136
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Re: convert list of int to floats

 
0
  #9
Feb 16th, 2009
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 5:17 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum


Views: 2753 | Replies: 8
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC