简体   繁体   中英

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

Is this what you want? I changed the ctrlList to strings since I don't know the variables.

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)   

This code will output the contents of ctrlList in the order of the shapes.

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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM