简体   繁体   中英

How to change an element from string to float in a nested list

I want to use list comprehension to change an element within a nested list to float.

lines = [[1, '74.37000326528938', 'Psychologist'], [2, '67.49686206937491', 'Psychologist'], [3, '74.92356434760966', 'Psychologist']]
>>> lines = [[line[0]] + [float(x) for x in line[1]] + [line[2]] for line in lines]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: .

I also don't understand why this would work for a different example:

mylist = [['1','2','3'],['5','6','7']]
datl3 = [[int(x) for x in line[0]] + line[1:] for line in mylist]
datl4 = [[line[0]] + [float(x) for x in line[1]] + [line[2]] for line in datl3]
>>> datl4
[[1, 2.0, '3'], [5, 6.0, '7']]

I think it has to do with decimal points, because the code won't work for this example:

mylist = [['1','2.555','3'],['5','6.777','7']]
datl3 = [[int(x) for x in line[0]] + line[1:] for line in mylist]
datl4 = [[line[0]] + [float(x) for x in line[1]] + [line[2]] for line in datl3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: .

You were close but the problem was that you were using a for loop on line[1] which is a single string. Hence, you were looping through each element/character of your float number. When you reach the decimal . you get the ValueError: could not convert string to float: . . Your second example works because there you just have a single digit strings 2 and 6 without any decimal.

If you stick to your code, you just need to replace the index line[1] by line[1:2] as follows where you slice

lines = [[line[0]] + [float(x) for x in line[1:2]] + [line[2]] for line in lines]

However, a more concise way is following where you avoid an unnecessary intermediary for loop by directly accessing the second element of the sublists. You don't need to create three sublists and then add them using +

lines = [[line[0], float(line[1]), line[2]] for line in lines]

Output

[[1, 74.37000326528938, 'Psychologist'],
 [2, 67.49686206937491, 'Psychologist'],
 [3, 74.92356434760966, 'Psychologist']]

The problem is that this line is trying to do too many things at once:

datl4 = [[line[0]] + [float(x) for x in line[1]] + [line[2]] for line in datl3]

The problem occurs in the inner list comprehension:

[float(x) for x in line[1]]

But what is line[1] ? It comes from the iteration for line in datl3 . So let's see what values it has:

>>> [line[1] for line in datl3]
['2.555', '6.777']

So when you do [float(x) for x in line[1]] you are iterating over the characters of each numerical string... "2" then "." then "5" then "5" then "5" . But "." is not a valid float.

I think what you want to do instead is just convert line[1] to a float :

lines = [[line[0], float(line[1]), line[2]] for line in lines]

可以通过列表理解来完成,只需要正确地设置它,检查str的十进制数即可,如果这样,则将该项目设置为int

l = [[float(j) if '.' in str(j) else j for j in i] for i in l]
 [[1, 74.37000326528938, 'Psychologist'], [2, 67.49686206937491, 'Psychologist'], [3, 74.92356434760966, 'Psychologist']] 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM