python - Single quotes around list elements that should be floats -
i asked "return list of tuples containing subset name (as string) , list of floating point data values".
my code is:
def load_data(filename): fileopen = open(filename) result_open=[] line in fileopen: answer = (line.strip().split(",")) result_open.append((answer[0],(answer[1:]))) return result_open
however, when run code, following appears:
[('slow loris', [' 21.72', ' 29.3', ' 20.08', ' 29.98', ' 29.85', ' 26.22', ' 19......)]
is there anyway change tuple appear without apostrophes? want like:
[('slow loris', [21.72, 29.3, 20.08, 29.98, 29.85, 6.22, 19......)]
line
string, , line.strip().split(",")
list of strings. need convert string values float
or decimal
values. 1 way be:
result_open.append((answer[0], [float(val) val in answer[1:]]))
that raise exception on values can't converted float, should think how want handle such input.
Comments
Post a Comment