you can use a list comprehension:
new_list = [float(integral) for integral in old_list]
scru
Posting Virtuoso
1,629 posts since Feb 2007
Reputation Points: 975
Solved Threads: 140
Then you would do:
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.
scru
Posting Virtuoso
1,629 posts since Feb 2007
Reputation Points: 975
Solved Threads: 140
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():
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]
Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
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
jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 156
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.
scru
Posting Virtuoso
1,629 posts since Feb 2007
Reputation Points: 975
Solved Threads: 140