Python 将一个数组的元素与另一个数组的元素匹配并更改主索引
ctrlIndex和形状是两个索引列表。我希望输出看起来像: 之前:Python 将一个数组的元素与另一个数组的元素匹配并更改主索引,python,python-3.x,python-2.7,maya,Python,Python 3.x,Python 2.7,Maya,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]; ctrl1 = ctrlList.index(ctrlList[0]); ctrl2 = ctrlList.index(ctrlList[1]); ctrl3 = ctrlList.index(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];
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);
之后:
print ctrlIndex
(0,1,2,3,4,5,6,7)
print shapes
(0,4,5,3,2,1,6,7)
此外,此列表还根据ctrlIndex更改ctrlList的顺序。
有人能帮我解决这个问题吗?我是一个初学者,在这一步上进退两难。尝试使用for循环
print ctrlIndex
(0,4,5,3,2,1,6,7)
这是你想要的吗?我把ctrlList改成了字符串,因为我不知道变量
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的内容。您可以使用列表理解。无需生成
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']
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)
请格式化您的问题和代码。。。这是不可理解的抱歉,这是我第一次在这里发布,完成了。你能发布一个代码示例,让我们重现你的问题吗?你到底想在Maya中做什么?这能不能与当前选择一起工作,而不是硬编码名称?我正在尝试创建一个rubiks立方体。我无法选择所有内容,因为每个多维数据集都有唯一的值,当我对它们进行解列时,值会发生变化。我认为直接使用对象名更好。或者,如果您熟悉其他技术,请与我们分享,非常感谢@GreenCell:)哇,非常感谢!我不知道你也能这样写。!是的,我一直在找这个。可能听起来我很傻(因为我是一个初学者),第一个“y”字在里面是干什么的?非常感谢你帮助我<代码>对于ctrlList中的y,意味着对于ctrlList中的每个项目不对于ctrlList中的y,第一行“y对于x”我熟悉形状中的x谢谢:)这里有两个for循环,在列表理解中,对于
形状
中的每个x
,对ctrlList
中的每个项目y
迭代ctrlList
,如果给定条件如果ctrlList.index(y)=x
(y的索引==x),则选择该y
。开始时的y
选择满足条件的y
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']