Python, making a float of a list

In summary, the conversation discusses the process of converting a list of strings into a list of floats in order to calculate velocity. The issue of using the 'map' function is addressed and advice is given on how to debug and better understand the functions used in the program.
  • #1
MaxManus
277
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
 
Technology news on Phys.org
  • #2
map(float,a) will give you a list of floats.
 
  • #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.
 
  • #4
If you do a=map(float,a) then a is now a list of floats. The first float is a[0], etc.
 
  • #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)
 
  • #6
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.
 
  • #7
Thank you.
 

Related to Python, making a float of a list

1. How do I make a float out of a list in Python?

To make a float out of a list in Python, you can use the built-in float() function. This function takes in a value as its argument and returns a float version of that value. For example, if you have a list [1, 2, 3], you can use float([1, 2, 3]) to get [1.0, 2.0, 3.0].

2. What if my list contains non-numeric values?

If your list contains non-numeric values, the float() function will raise an error. To avoid this, you can use a try-except block to catch the error and handle it accordingly.

3. Can I convert a list of strings into floats?

Yes, you can convert a list of strings into floats using the float() function. However, if any of the strings in the list cannot be converted to a float, an error will be raised. To avoid this, you can use a try-except block to catch the error and handle it accordingly.

4. Is there a way to convert a list of floats back into a list of integers?

Yes, you can use the built-in int() function to convert a float back into an integer. This function takes in a float value as its argument and returns the integer version of that value. For example, if you have a list [1.0, 2.0, 3.0], you can use int(1.0) to get 1.

5. Can I round the float values in my list to a specific number of decimal places?

Yes, you can use the built-in round() function to round the float values in your list to a specific number of decimal places. This function takes in two arguments - the value to be rounded and the number of decimal places to round to. For example, if you have a list [1.234, 2.345, 3.456], you can use [round(1.234, 2), round(2.345, 2), round(3.456, 2)] to get [1.23, 2.35, 3.46].

Similar threads

  • Programming and Computer Science
Replies
5
Views
1K
Replies
6
Views
800
  • Programming and Computer Science
Replies
8
Views
927
  • Programming and Computer Science
Replies
1
Views
432
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
6
Views
938
  • Programming and Computer Science
2
Replies
35
Views
1K
Back
Top