When providing a list of data, I will be asked to convert the lines from the list to the appropriate type, for example, int if the string contains integers, or float if it does not contain an integer. But I ran into this problem when it converts the value but does not change the original value in the list.
For example, if given:
d = [['abc', '123', '45.6', 'True', 'False']]
It should look like this after the conversion:
[['abc', 123, 45.6, True, False]]
So far, I have tried to write a for loop and convert the values this way, but as I mentioned, it does not change the original list:
for lst in data:
for index in lst:
if index.isdigit():
index = int(index)
elif not index.isdigit():
index = float(index)
I am not sure how to fix this problem or if it has another way. Any help would be appreciated! Thank:)
source
share