[float(i) for i in lst]
to be precise, it creates a new list with float values. Unlike the map approach it will work in py3k.
[float(i) for i in lst]
to be precise, it creates a new list with float values. Unlike the map approach it will work in py3k.
map(float, mylist) should do it.
(In Python 3, map ceases to return a list object, so if you want a new list and not just something to iterate over, you either need list(map(float, mylist) - or use SilentGhost's answer which arguably is more pythonic.)
Convert a list of float to string in Python - Stack Overflow
How do I convert a list of dollar amounts to a list of floats and remove the dollar signs?
Trying to format a list of floats with f-strings
Convert Dictionary Values to Floats
Videos
Not able to convert the "close_list" list into a float? list? I've tried a lot of ways that I see on the internet a lot that it's too much to put in here, and i am still getting an error of:" Exception has occurred: TypeError:list indices must be integers or slices, not float File "{filename}\import csv.py", line 25, in <module> diff = close_list[i] - price_compare TypeError: list indices must be integers or slices, not float ".
Am i missing anything that needs to be declared or imported, updated extensions? I am doing exactly what the internet says and even putting in "import numpy as np" and that's not working and am getting real disappointed. i have tried updating it and numpy is already up to date. How can i modify this so i can actually subtract price_compare from close_list[i].
import csv
import os
import sys
import numpy as np
close_list = ['0.0', '1.0', '2.5', '2.0']
len = len(close_list)
price_compare = [close_list[0]]
diff = 0.0
price=0
# [float(i) for i in value]
diff_close_list = []
for i in range(1,len):
i = float(i)
# price_compare = float(price_compare)
diff = close_list[i] - price_compare
price_compare = i
print(type(i))
print(i)
print(type(price_compare))
print(price_compare)
print(type(diff))
print(diff)
diff_close_list.append(diff)
# print(diff_close_list)
Use string formatting to get the desired number of decimal places.
>>> nums = [1883.95, 1878.3299999999999, 1869.4300000000001, 1863.4000000000001]
>>> ['{:.2f}'.format(x) for x in nums]
['1883.95', '1878.33', '1869.43', '1863.40']
The format string {:.2f} means "print a fixed-point number (f) with two places after the decimal point (.2)". str.format will automatically round the number correctly (assuming you entered the numbers with two decimal places in the first place, in which case the floating-point error won't be enough to mess with the rounding).
If you want to keep full precision, the syntactically simplest/clearest way seems to be
mylist = list(map(str, mylist))