Python, making a float of a list

  • Context: Python 
  • Thread starter Thread starter MaxManus
  • Start date Start date
  • Tags Tags
    Float List Python
Click For Summary

Discussion Overview

The discussion revolves around converting a list of string representations of numbers into a list of floats in Python. Participants explore methods for achieving this, particularly in the context of numerical calculations involving acceleration and velocity.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant describes their issue with converting a list containing a string representation of a number into a float, noting an error encountered when using the float function.
  • Another participant suggests using the map function to convert the list of strings to floats.
  • A participant clarifies their context, mentioning they need to compute velocity from acceleration data but are struggling with the conversion process.
  • Further advice is given to ensure the list being passed to the map function is correctly formatted, indicating that the original list may contain nested lists instead of a flat list of strings.
  • One participant recommends debugging by printing the list before applying the map function to understand its structure better.

Areas of Agreement / Disagreement

Participants generally agree on the use of the map function for conversion, but there is a lack of consensus on the structure of the list being processed, leading to different suggestions for resolving the issue.

Contextual Notes

Participants note that the list may contain nested lists, which complicates the conversion process. There are also indications of language barriers affecting communication.

Who May Find This Useful

Individuals working with Python for data processing, particularly those dealing with numerical data conversion and integration in programming contexts.

MaxManus
Messages
268
Reaction score
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
map(float,a) will give you a list of floats.
 
I'm sorry. English is not my native language. I have a list of the acceleration of an object at some discrete 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.
 
If you do a=map(float,a) then a is now a list of floats. The first float is a[0], etc.
 
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)
 
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.
 
Thank you.
 

Similar threads

  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
Replies
3
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K