I have the following list:
Attributes are listed in first row and dummy variables check whether a certain candy type fits this critea. The last column shows the "success value" of a certain candy type in each row
I read that using the map function would be "the most elegant, pythonic and recommended method to perform this particular task."
So attempted to apply the map function to my list (here stored as 'data') as follows:
data_int=list(map(int, data)) print(data_int)
However, I get the error code
"ValueError: invalid literal for int() with base 10: 'Whoppers'"
for the first line. ("Whoppers" being the first element of the last line of the table)
Can anyone please explain me my error and what to do in order to resolve this issue?
It is a little unclear what you are trying to do. Could you add an example of what data
contains?
The error is generated because you are attempting to convert every element in data
to an int. This includes the names of the candies, which is not possible to convert to a numerical value, causing the error message.
If you run int("Whopper")
in the interpreter you will get this error message.
If data
is in the form: ["Whopper", "0", "0", "1", "0", "1"]
then running list(map(int,data[1:]))
will give you the result that I think you are looking for, since the operator [1:]
excludes the first element of the list (in your case, the name of the candy).