所以我的输入数据是这样的字符串列表:
coordIndex =
[' 8, 9, 6, 4, 2, 0, -1,',
' 11, 1, 3, 5, 7, 10, -1,',
' 1, 11, 8, 0, -1,',
' 3, 1, 0, 2, -1,',
' 5, 3, 2, 4, -1,',
' 7, 5, 4, 6, -1,',
' 10, 7, 6, 9, -1,',
' 11, 10, 9, 8, -1']
和
它应该是一个这样的整数元组:
coordIndex=
[[8, 9, 6, 4, 2, 0],
[11, 1, 3, 5, 7, 10],
[1, 11, 8, 0],
[3, 1, 0, 2],
[5, 3, 2, 4],
[7, 5, 4, 6],
[10, 7, 6, 9],
[11, 10, 9, 8]]
我可以清除空白,去掉逗号,通过这样做解析成int:
coordIndex = [x.replace(' ','') for x in coordIndex]
coordIndex = [x.replace(',-1,','') for x in coordIndex]
coordIndex = [x.replace(',-1','') for x in coordIndex]
coordIndex = [x.replace(',',' ') for x in coordIndex]
coordIndex = [x.rstrip() for x in coordIndex]
j = 0
for items in coordIndex:
coordIndex[j] = tuple(map(int, items.split(' ')))
j+=1
但是,输入数据的“-1”在新行中存在问题。“-1”始终是我必须创建的每个元组行的分隔符,但我不知道如何在Python中实现这一点。
如果有人有主意,会非常感谢帮助。谢谢!