如何打印列表每次在python中跳过一个元素而没有numpy?

时间:2016-02-04 07:28:31

标签: python list numpy

给出

samplelist = [100,101,102,103,104,105,106,107,108,109]

然后我想要输出如下:

[100,[101,102,103,104,105,106,107,108,109]]
[101,[100,102,103,104,105,106,107,108,109]]
[102,[100,101,103,104,105,106,107,108,109]]

注意:在第一行输出100被跳过,即list[0],然后跳过第二行list[1]

我尝试弹出并显示,但下次没有显示弹出的所有元素。所以我试着再次将它们插入循环结束但没有运气。

3 个答案:

答案 0 :(得分:3)

一个简单的列表理解应该这样做

>>> samplelist = [100,101,102,103,104,105,106,107,108,109]
>>> [[el for el in samplelist if el is not i] for i in samplelist]
[[101, 102, 103, 104, 105, 106, 107, 108, 109],
 [100, 102, 103, 104, 105, 106, 107, 108, 109],
 [100, 101, 103, 104, 105, 106, 107, 108, 109],
 [100, 101, 102, 104, 105, 106, 107, 108, 109],
 [100, 101, 102, 103, 105, 106, 107, 108, 109],
 [100, 101, 102, 103, 104, 106, 107, 108, 109],
 [100, 101, 102, 103, 104, 105, 107, 108, 109],
 [100, 101, 102, 103, 104, 105, 106, 108, 109],
 [100, 101, 102, 103, 104, 105, 106, 107, 109],
 [100, 101, 102, 103, 104, 105, 106, 107, 108]]

基本上它会扫描列表,并为每个元素生成除当前元素之外的整个列表。

或者,您可以使用生成器表达式

>>> g = ([el for el in samplelist if el is not i] for i in samplelist)
>>> for x in g:
...     print(x)
... 
[101, 102, 103, 104, 105, 106, 107, 108, 109]
[100, 102, 103, 104, 105, 106, 107, 108, 109]
[100, 101, 103, 104, 105, 106, 107, 108, 109]
[100, 101, 102, 104, 105, 106, 107, 108, 109]
[100, 101, 102, 103, 105, 106, 107, 108, 109]
[100, 101, 102, 103, 104, 106, 107, 108, 109]
[100, 101, 102, 103, 104, 105, 107, 108, 109]
[100, 101, 102, 103, 104, 105, 106, 108, 109]
[100, 101, 102, 103, 104, 105, 106, 107, 109]
[100, 101, 102, 103, 104, 105, 106, 107, 108]

根据您的新要求进行编辑(即必须包含跳过的元素):

>>> g = ([i, [el for el in samplelist if el is not i]] for i in samplelist)
>>> for x in g:
...     print(x)
... 
[100, [101, 102, 103, 104, 105, 106, 107, 108, 109]]
[101, [100, 102, 103, 104, 105, 106, 107, 108, 109]]
[102, [100, 101, 103, 104, 105, 106, 107, 108, 109]]
[103, [100, 101, 102, 104, 105, 106, 107, 108, 109]]
[104, [100, 101, 102, 103, 105, 106, 107, 108, 109]]
[105, [100, 101, 102, 103, 104, 106, 107, 108, 109]]
[106, [100, 101, 102, 103, 104, 105, 107, 108, 109]]
[107, [100, 101, 102, 103, 104, 105, 106, 108, 109]]
[108, [100, 101, 102, 103, 104, 105, 106, 107, 109]]
[109, [100, 101, 102, 103, 104, 105, 106, 107, 108]]

答案 1 :(得分:1)

您可以在列表理解中使用简单的切片:

>>> [samplelist[:i]+samplelist[i+1:] for i,_ in enumerate(samplelist)]
[[101, 102, 103, 104, 105, 106, 107, 108, 109],
 [100, 102, 103, 104, 105, 106, 107, 108, 109],
 [100, 101, 103, 104, 105, 106, 107, 108, 109],
 [100, 101, 102, 104, 105, 106, 107, 108, 109],
 [100, 101, 102, 103, 105, 106, 107, 108, 109],
 [100, 101, 102, 103, 104, 106, 107, 108, 109],
 [100, 101, 102, 103, 104, 105, 107, 108, 109],
 [100, 101, 102, 103, 104, 105, 106, 108, 109],
 [100, 101, 102, 103, 104, 105, 106, 107, 109],
 [100, 101, 102, 103, 104, 105, 106, 107, 108]]

如果要捕获省略的项目:

>>> [[samplelist[i],samplelist[:i]+samplelist[i+1:]] for i,_ in enumerate(samplelist)]
[[100, [101, 102, 103, 104, 105, 106, 107, 108, 109]], [101, [100, 102, 103, 104, 105, 106, 107, 108, 109]], [102, [100, 101, 103, 104, 105, 106, 107, 108, 109]], [103, [100, 101, 102, 104, 105, 106, 107, 108, 109]], [104, [100, 101, 102, 103, 105, 106, 107, 108, 109]], [105, [100, 101, 102, 103, 104, 106, 107, 108, 109]], [106, [100, 101, 102, 103, 104, 105, 107, 108, 109]], [107, [100, 101, 102, 103, 104, 105, 106, 108, 109]], [108, [100, 101, 102, 103, 104, 105, 106, 107, 109]], [109, [100, 101, 102, 103, 104, 105, 106, 107, 108]]]

答案 2 :(得分:1)

使用itertools.combinations()

import itertools
a = [100,101,102,103,104,105,106,107,108,109]
list(itertools.combinations(a, len(a)-1))[::-1]