将一个数组的元素与另一个数组匹配,并更改主索引

时间:2019-12-10 08:27:57

标签: python python-3.x python-2.7 maya

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()

2 个答案:

答案 0 :(得分:0)

这是您想要的吗?由于不知道变量,我将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的内容。

答案 1 :(得分:0)

您可以使用列表理解。无需您生成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']