翻转的列表结构的列表中的同一项目的具体位置
我有以下数据结构:翻转的列表结构的列表中的同一项目的具体位置
pool = [[[0,0,0,0,0,0,0,0],"ze","Zero"], [[0,0,3,0,3,0,0,0],"bd","BasicDilemma"],
[[0,0,3,2,3,0,0,2],"lk","LowLock"],
[[0,1,3,2,0,3,1,2],"DlCo",""],
[[0,1,3,2,0,3,2,1],"DlPc",""],
[[0,1,3,2,1,3,0,2],"DlAs",""],
[[0,1,3,2,1,3,2,0],"DlHa",""],
[[0,1,3,2,2,3,0,1],"DlSh",""],
[[0,1,3,2,2,3,1,0],"DlNc",""]]
def ListFlip (pool):
for game in range (0, len(pool)):
game[0][2], game[0][3] = game[0][3], game[0][2]
game[0][6], game[0][7] = game[0][7], game[0][6]
return (pool)
我需要翻转在每个项目中列出,只是数值的这个列表特定索引位置。
的结构将是:
[0,1,2,3,4,5,6,7] -> [0,1,3,2,4,5,7,6]
所以对于所有的项目,我需要翻转位置[2] and [3]
,并[6] and [7]
例如:
[[0,1,3,2,0,3,1,2],"DlCo",""] -> [[0,1,2,3,0,3,2,1],"DlCo",""]
我认为这将做它的方式,但它不工作。有谁知道我做错了什么?
谢谢!
回答:
这条线:
for game in range (0, len(pool)):
应该是:
for game in pool:
随着第一只获得在游泳池每场比赛的索引,所以索引game[0][2]
无效这里。
你的代码工作现在罚款:
pool = [[[0,0,0,0,0,0,0,0],"ze","Zero"], [[0,0,3,0,3,0,0,0],"bd","BasicDilemma"],
[[0,0,3,2,3,0,0,2],"lk","LowLock"],
[[0,1,3,2,0,3,1,2],"DlCo",""],
[[0,1,3,2,0,3,2,1],"DlPc",""],
[[0,1,3,2,1,3,0,2],"DlAs",""],
[[0,1,3,2,1,3,2,0],"DlHa",""],
[[0,1,3,2,2,3,0,1],"DlSh",""],
[[0,1,3,2,2,3,1,0],"DlNc",""]]
def ListFlip(pool):
for game in pool:
game[0][2], game[0][3] = game[0][3], game[0][2]
game[0][6], game[0][7] = game[0][7], game[0][6]
return pool
print(ListFlip(pool))
,输出:
[[[0, 0, 0, 0, 0, 0, 0, 0], 'ze', 'Zero'], [[0, 0, 0, 3, 3, 0, 0, 0], 'bd', 'BasicDilemma'],
[[0, 0, 2, 3, 3, 0, 2, 0], 'lk', 'LowLock'],
[[0, 1, 2, 3, 0, 3, 2, 1], 'DlCo', ''],
[[0, 1, 2, 3, 0, 3, 1, 2], 'DlPc', ''],
[[0, 1, 2, 3, 1, 3, 2, 0], 'DlAs', ''],
[[0, 1, 2, 3, 1, 3, 0, 2], 'DlHa', ''],
[[0, 1, 2, 3, 2, 3, 1, 0], 'DlSh', ''],
[[0, 1, 2, 3, 2, 3, 0, 1], 'DlNc', '']]
以上是 翻转的列表结构的列表中的同一项目的具体位置 的全部内容, 来源链接: utcz.com/qa/265101.html