So, your error is that instead of simply adding your latitute to the list, you are iterating over each character of the latitude, as a string, and adding that character to a list.
result=[]
for value in response_i['objcontent'][0]['rowvalues']:
lat = value[0]
print(lat)
result.append(float(lat))
print (result)
Besides that, using range(len(...))) is the way things have to be done in almost all modern languages, because they either don't implement a "for ...each" or do it in an incomplete or faulty way.
In Python, since the beginning it is a given that whenever one wants a for iteration he wants to get the items of a sequence, not its indices (for posterior retrieval of the indices). Some auxiliar built-ins come in to play to ensure you just interate the sequence: zip to mix one or more sequences, and enumerate to yield the indices as well if you need them.
Answer from jsbueno on Stack OverflowPython 3: how to create list out of float numbers? - Stack Overflow
python - How do I convert all of the items in a list to floats? - Stack Overflow
Python creating a lists of lists of floats from a single list of strings - Stack Overflow
No way i am trying lets me convert string list into float list
[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.)
Here ya go:
[[int(y) for y in x.split(",")] for x in objectListData]
output:
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 0, 0, 7], [5, 4, 3, 2], [2, 3, 3, 3], [2, 2, 3, 3]]
or, if you want floats:
[[float(y) for y in x.split(",")] for x in objectListData]
output:
[[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0], [9.0, 0.0, 0.0, 7.0], [5.0, 4.0, 3.0, 2.0], [2.0, 3.0, 3.0, 3.0], [2.0, 2.0, 3.0, 3.0]]
The problem is that your inner lists are references to one single list and not individual lists.
>>> objectListDataFloats = [[0] * len(objectListData[0].split(', '))] * len(objectListData)
>>> objectListDataFloats
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
>>> id(objectListDataFloats[0]) == id(objectListDataFloats[1])
True
After you fix that, you need to iterate from the starting index of 0, since the lists in Python start their index from 0.
for count in range(len(objectListData)):
for ii in range(len(objectListData[count].split(', '))):
objectListDataFloats[count][ii] = float(objectListData[count].split(', ')[ii])
>>> objectListDataFloats
[[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0], [9.0, 0.0, 0.0, 7.0], [5.0, 4.0, 3.0, 2.0], [2.0, 3.0, 3.0, 3.0], [2.0, 2.0, 3.0, 3.0]]
To completely do away with the initial initialization of the list with zeroes, you could also just build the list as you go along, something like
>>> objectListDataFloats = []
>>> for elem in objectListData:
test_list = []
for val in elem.split(','):
test_list.append(float(val))
objectListDataFloats.append(test_list)
>>> objectListDataFloats
[[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0], [9.0, 0.0, 0.0, 7.0], [5.0, 4.0, 3.0, 2.0], [2.0, 3.0, 3.0, 3.0], [2.0, 2.0, 3.0, 3.0]]
You don't need to iterate over the list or a string by using indices, you can just iterate over the list like in the above example.
Reduced Solution -
You could just reduce the whole solution to the following though (Change int to float if you require floating point numbers)
>>> objectListData = ["1, 2, 3, 4", "5, 6, 7, 8", "9, 0, 0, 7", "5, 4, 3, 2", "2, 3, 3, 3", "2, 2, 3, 3"]
>>> [map(int, elem.split(',')) for elem in objectListData]
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 0, 0, 7], [5, 4, 3, 2], [2, 3, 3, 3], [2, 2, 3, 3]]
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)
Trying to create a function that takes a specified length for a list, a range from a negative to positive number, and a sum. For example random_list_sum_generator(length=5, float_range=(-3.0, 3.0), sum=0) should generate a list of 5 random floats within the range -3. to 3. that cumulatively sum to 0. I have been unable to do it thus far.
Chatgpt wants to randomly generate the first 4 numbers, and then calculate the difference and then set the last number. The problem with that is that the last number might then be outside of the specified float_range.
How can I go about doing this, it doesn't seem like it would be to hard conceptually but the attempts have been unfruitful so far.