如果我有一个列表:
to_modify = [5,4,3,2,1,0]
然后声明另外两个列表:
indexes = [0,1,3,5]
replacements = [0,0,0,0]
我该如何将to_modify
的元素作为indexes
的索引,然后将to_modify
中相应的元素设置为replacements
,即运行后,indexes
应该是[0,0,3,0,1,0]
。
显然,我可以通过使用for循环来实现:
for ind in to_modify:
indexes[to_modify[ind]] = replacements[ind]
但是还有其他方法吗?我能以某种方式使用operator.itemgetter
吗?
l
包含超出m
边界的索引)?你能为这个问题提供更多上下文吗? - dbr