Is there a \"straightforward\" way to convert a str containing numbers into a list of [x,y] ints?
# from: \'5,4,2,4,1,0,3,0,5,1,3,3,14,32,3,5\'
# to: [[5, 4
First, use split to make a list of numbers (as in all of the other answers).
num_list = num_str.split(",")
Then, convert to integers:
num_list = [int(i) for i in num_list]
Then, use the itertools groupby recipe:
from itertools import izip_longest
def grouper(n, iterable, fillvalue=None):
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return izip_longest(fillvalue=fillvalue, *args)
pair_list = grouper(2, num_list)
Of course, you can compress this into a single line if you're frugal:
pair_list = grouper(2, [int(i) for i in num_str.split(",")]