在Python 3中,如果dict
为{old index: new index, old index: new index, old index: new index}
,将多个可能不连续的元素移动到新的可能不连续的索引中的最佳方法是什么
重要提示:dict
可能不包含元素的所有新位置,这就是为什么第一个例子下面的例子不起作用
编辑:对不起,我忘了提到,你不必担心检查new_idxs
中的所有索引是否有效,是否在seq
的范围内。new_idxs
的键也已经按排序顺序排列
from typing import Any
def move_elements(
seq: list[Any],
new_idxs: dict,
) -> list[Any]:
new = []
idx = 0
done = set()
while len(new) < len(seq):
if idx in new_idxs and idx not in done:
new.append(seq[new_idxs[idx]])
done.add(idx)
elif idx not in done:
new.append(seq[idx])
idx += 1
else:
idx += 1
return new
# works
new_idxs = {0: 1, 1: 0}
seq = [0, 1]
seq = move_elements(seq, new_idxs)
print ("\nexpected:", [1, 0])
print ("actual :", seq)
# expected output
# [1, 0]
# doesn't work
new_idxs = {3: 0, 5: 1}
seq = [0, 1, 2, 3, 4, 5, 6, 7]
seq = move_elements(seq, new_idxs)
print ("\nexpected:", [3, 5, 0, 1, 2, 4, 6, 7])
print ("actual :", seq)
# expected output
# [3, 5, 0, 1, 2, 4, 6, 7]
# doesn't work
new_idxs = {3: 6, 5: 7}
seq = [0, 1, 2, 3, 4, 5, 6, 7]
seq = move_elements(seq, new_idxs)
print ("\nexpected:", [0, 1, 2, 4, 6, 7, 3, 5])
print ("actual :", seq)
# expected output
# [0, 1, 2, 4, 6, 7, 3, 5]
new_idxs = {3: 1, 7: 4}
seq = [0, 1, 2, 3, 4, 5, 6, 7]
seq = move_elements(seq, new_idxs)
print ("\nexpected:", [0, 3, 1, 2, 7, 4, 5, 6])
print ("actual :", seq)
# expected output
# [0, 3, 1, 2, 7, 4, 5, 6]
new_idxs = {0: 3, 3: 1, 7: 4}
seq = [0, 1, 2, 3, 4, 5, 6, 7]
seq = move_elements(seq, new_idxs)
print ("\nexpected:", [1, 3, 2, 0, 7, 4, 5, 6])
print ("actual :", seq)
# expected output
# [0, 1, 2, 3, 4, 5, 6, 7]
# [1, 3, 2, 0, 7, 4, 5, 6]
2条答案
按热度按时间zujrkrfu1#
线性时间1我从一个充满虚拟对象的结果列表开始。然后根据请求将元素从输入序列移动到结果列表中。然后用输入序列中剩余的非伪元素替换结果中剩余的伪元素。
输出(Attempt This Online!):
cidc1ykv2#
解决方案:
输出(针对您的测试用例):