-
至尊宝的传说
如果Something是一个字符串,你可以这样做。something = "(1,2,3)&(6,8,10)&(2,5)&(29,8,6)"words = something.split('&')for i,x in enumerate(words): words[i] = x.replace('(','').replace(')','')或使用列表理解而不是 for 循环之类的,words[:] = [x.replace('(','').replace(')','') for x in words]
-
神不在的星期二
你可以试试这个方法:>>> def to_list(s):... return [int(i) for i in s.strip('()').split(',')]... >>> data = '(1,2,3)&(6,8,10)&(2,5)&(29,8,6)'>>> [to_list(item) for item in data.split('&')][[1, 2, 3], [6, 8, 10], [2, 5], [29, 8, 6]]
-
富国沪深
如果您需要注意更改输入字符串,这是另一种方法data = '(1,2,3)&(6,8,10)&(2,5)&(29,8,6)'a = []for i in data.split('&'): a.append([int(j) for j in i[i.find('(')+1:i.find(')')].split(',')])print(a) #[[1, 2, 3], [6, 8, 10], [2, 5], [29, 8, 6]]