我有一个列表,例如,如果我在列表上运行 for
循环,我想选择索引元素以外的所有元素。
例如,
var = [a,b,c,d,e]
1st iteration: Choose b,c,d,e and ignore a
2nd iteration: Choose a,c,d,e and ignore b
3rd iteration: Choose a,b,d,e and ignore c
and so on...
我尝试使用切片,但我无法对前面的元素以及如何获取它们进行条件处理。
有人可以推荐任何其他方法吗?
最佳答案
这就像一个魅力:
var = [1, 2, 3, 4, 5]
for i in range(len(var)):
print(var[:i] + var[i+1:])
结果:
[2, 3, 4, 5]
[1, 3, 4, 5]
[1, 2, 4, 5]
[1, 2, 3, 5]
[1, 2, 3, 4]
关于python - 如何选择索引元素以外的列表的不同元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48304411/