Search code examples
pythonpython-3.xpython-2.7maya

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 and shapes are 2 indexed lists. i want the output to look like:

Before:

print ctrlIndex
(0,1,2,3,4,5,6,7)

print shapes
(0,4,5,3,2,1,6,7)

After:

print ctrlIndex
(0,4,5,3,2,1,6,7)

and also this list changes the order of ctrlList according to ctrlIndex. can someone please help me solve this? i am beginner and am stuck at this step. have tried using for loop,

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

Solution

  • you can use list comprehension. No need for you to generate a 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']