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!
CodePudding user response:
values
is a list of lists:
>>> 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]]
CodePudding user response:
When getting the values
, you can directly cast them to int
, e.g.,
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:]]