3 回答
TA贡献1789条经验 获得超10个赞
如果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]
TA贡献1963条经验 获得超6个赞
你可以试试这个方法:
>>> 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]]
TA贡献1790条经验 获得超9个赞
如果您需要注意更改输入字符串,这是另一种方法
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]]
添加回答
举报