将一个数组的元素与另一个数组匹配并更改主索引
matching elements of one array with another and changing the main index
ctrlList = [box_1_ctrl, box_2_ctrl, box_3_ctrl, box_4_ctrl, box_5_ctrl, box_6_ctrl, box_7_ctrl, box_8_ctrl];
ctrl1 = ctrlList.index(ctrlList[0]);
ctrl2 = ctrlList.index(ctrlList[1]);
ctrl3 = ctrlList.index(ctrlList[2]);
ctrl4 = ctrlList.index(ctrlList[3]);
ctrl5 = ctrlList.index(ctrlList[4]);
ctrl6 = ctrlList.index(ctrlList[5]);
ctrl7 = ctrlList.index(ctrlList[6]);
ctrl8 = ctrlList.index(ctrlList[7]);
ctrlIndex = (ctrl1, ctrl2, ctrl3, ctrl4, ctrl5, ctrl6, ctrl7, ctrl8); *first index list
shapes = (shape1, shape2, shape3, shape4, shape5, shape6, shape7, shape8);
ctrlIndex 和 shapes 是 2 个索引列表。我希望输出看起来像:
之前:
print ctrlIndex
(0,1,2,3,4,5,6,7)
print shapes
(0,4,5,3,2,1,6,7)
之后:
print ctrlIndex
(0,4,5,3,2,1,6,7)
并且此列表还根据 ctrlIndex 更改了 ctrlList 的顺序。 有人可以帮我解决这个问题吗?我是初学者,卡在了这一步。已尝试使用 for 循环,
for m in ctrlList:
for n in shapes:
ctrlList = ctrlList[m]
shapes = shapes[n]
if ctrlList != shapes:
ctrlList.remove(m)
ctrlList.insert(n)
result.append()
这是你想要的吗?我将 ctrlList 更改为字符串,因为我不知道变量。
ctrlList = ['box_1_ctrl', 'box_2_ctrl', 'box_3_ctrl', 'box_4_ctrl', 'box_5_ctrl', 'box_6_ctrl', 'box_7_ctrl', 'box_8_ctrl']
shapes =[0,4,5,3,2,1,6,7]
newlist = []
for shape in shapes:
newlist.append(ctrlList[shape])
print(newlist)
newindex = []
for item in newlist:
newindex.append(ctrlList.index(item))
print(newindex)
这段代码会按照形状的顺序输出ctrlList的内容。
您可以使用列表理解。您无需生成 ctrlIndex
.
ctrlList = ['box_1_ctrl', 'box_2_ctrl', 'box_3_ctrl', 'box_4_ctrl', 'box_5_ctrl', 'box_6_ctrl', 'box_7_ctrl', 'box_8_ctrl'];
shape = (0,4,5,3,2,1,6,7)
print [y for x in shape for y in ctrlList if ctrlList.index(y) == x] # for python 2
>>>['box_1_ctrl', 'box_5_ctrl', 'box_6_ctrl', 'box_4_ctrl', 'box_3_ctrl', 'box_2_ctrl', 'box_7_ctrl', 'box_8_ctrl']