1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Python, making a float of a list

  1. Oct 13, 2008 #1
    I have made a list of data from a file, the file contains numbers and I want to calculate with the numbers. How do I make a float of a list where a[1] is: ['0.00500006128645'], I tried to just use float, but then I got:
    float() argument must be a string or a number
     
  2. jcsd
  3. Oct 13, 2008 #2

    Dick

    User Avatar
    Science Advisor
    Homework Helper

    map(float,a) will give you a list of floats.
     
  4. Oct 13, 2008 #3
    I'm sorry. English is not my native language. I have a list of the acceleration of an object at some descrete and equally spaced points. And I am supposed to calculate the velocity by computing the integral numerically, but I can't manage to get the floats oit of the list.
     
  5. Oct 13, 2008 #4

    Dick

    User Avatar
    Science Advisor
    Homework Helper

    If you do a=map(float,a) then a is now a list of floats. The first float is a[0], etc.
     
  6. Oct 14, 2008 #5
    I tried, but I just got: TypeError: float() argument must be a string or a number

    f = open("acc.dat", "r")


    a = []
    for lines in f:
    numbers = lines.split()
    a.append(numbers)

    f.close()

    a = map(float,a)
     
  7. Oct 14, 2008 #6

    Dick

    User Avatar
    Science Advisor
    Homework Helper

    Add a 'print a' statement before the 'map' statement to see what is actually going into the 'map'. You'll see that it's not a list of strings. It's a LIST of 'lists of strings'. You don't really want to append the list of strings 'lines.split()' to a, you want to concatenate it. Change 'a.append(numbers)' to 'a=a+numbers'. Python is VERY easy to debug. Use print statements to follow the program flow. If you don't understand exactly what a function does, play with it using the interactive interpreter.
     
  8. Oct 15, 2008 #7
    Thank you.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Python, making a float of a list
  1. Python Beginner (Replies: 3)

  2. Python question (Replies: 1)

Loading...