[英]How to convert all values of list into integers?
Hitting a huge roadblock because I can't seem to convert this list into integers.遇到了一个巨大的障碍,因为我似乎无法将此列表转换为整数。
grades = [
# First line is the descriptive header. Subsequent lines hold data
['Student', 'Exam 1', 'Exam 2', 'Exam 3'],
['Thorny', '100', '90', '80'],
['Mac', '88', '99', '111'],
['Farva', '45', '56', '67'],
['Rabbit', '59', '61', '67'],
['Ursula', '73', '79', '83'],
['Foster', '89', '97', '101']
]
values = [data[1:] for data in grades[1:]]
When I try to convert values to be all integers, I keep getting errors I have tried...当我尝试将值转换为所有整数时,我不断收到我尝试过的错误......
val=int(x) for x in values
and...和...
int_list = map(int, values)
print(list(int_list))
Any help would be appreciated!任何帮助,将不胜感激!
values
is a list of lists: values
是一个列表列表:
>>> values
[['100', '90', '80'], ['88', '99', '111'], ['45', '56', '67'], ['59', '61', '67'], ['73', '79', '83'], ['89', '97', '101']]
so you'll need to iterate through that:所以你需要遍历它:
>>> [list(map(int, value)) for value in values]
[[100, 90, 80], [88, 99, 111], [45, 56, 67], [59, 61, 67], [73, 79, 83], [89, 97, 101]]
When getting the values
, you can directly cast them to int
, eg,获取
values
时,您可以直接将它们转换为int
,例如,
values = [data[:1] + [int(v) for v in data[1:]] for data in grades[1:]]
or using destructuring and the *
-operator:或使用解构和
*
运算符:
values = [[name, *map(int, data)] for name, *data in grades[1:]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.