The answer to a question on multiple-value elements in a config file (which exactly fits my needs) suggests to "unpack the string from the config". I read the doc for unpacking arguments lists suggested in several places but I fail to understand how this relates to my problem.
I am sure this must be obvious: having a string str = "123,456"
, how can I transform it into the list [123,456]
(the number of elements separated by a comma in the string may vary)
Thank you.
You're basically applying the function
int
to each element produced by split of your string.The result of simply
str.split(',')
would be["123","456"]
.As Daniel Roseman pointed out, you should be careful not to use variable or method names that inadvertently overshadow built in methods, like, for instance,
str
.Although I agree with the other answers, you should also handle exceptions in the case of invalid string representation of a supposed number. Take, for example, the following snippet:
Just a thought. Good luck!
The easiest way would be to use
split()
.Do you want a list of strings or a list of ints?
If you just want a list of strings, it's very simple:
If you want to convert these to ints, you need:
(Also, don't use
str
as a variable name as it shadows the built-instr()
function.)