| | |
convert list of int to floats
Thread Solved |
you can use a list comprehension:
python Syntax (Toggle Plain Text)
new_list = [float(integral) for integral in old_list]
Last edited by scru; Feb 13th, 2009 at 8:00 am.
Then you would do:
[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.
python Syntax (Toggle Plain Text)
l1 = [1,2,3,4,5] l2 = [float(i) for i in l1] 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.
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():
Here is an example how to use Python function map():
python Syntax (Toggle Plain Text)
q = [1, 2, 3, 4, 5] # map applies float() to every element in the list q qf = map(float, q) print(qf) # [1.0, 2.0, 3.0, 4.0, 5.0]
drink her pretty in burbank
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
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
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.
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.
![]() |
Similar Threads
- template list (C++)
Other Threads in the Python Forum
- Previous Thread: Code correction - counting characters.
- Next Thread: How to change reorder the elements of a file
Views: 2753 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for Python
aliased application array beginner c++ c/c++ change character class client code command convert count create csv ctypes database dictionary django dll error examples excel exe extensions fdlib file float format framework ftp function graphics gui homework image images import input keyboard library line linux list lists logging loop loops microcontroller mouse mysql mysqldb number numbers output parse parsing path port prime processing program programming py2exe pygame pygtk pyqt python random raw_input recursion recursive redirect remote scrolledtext server socket ssh stdout string strings syntax table terminal text thread threading tkinter transparency tuple tutorial ubuntu unicode variable variables web windows wordgame wxpython







