how to print list skipping one element each time in python without numpy?

前端 未结 3 1745
既然无缘
既然无缘 2021-01-20 08:03

given

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

then I want output as below:

[100,[101,102,103,104,105,106,10         


        
相关标签:
3条回答
  • 2021-01-20 08:18

    You can use a simple slicing within a list comprehension :

    >>> [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]]
    

    If you want to capture the omitted item :

    >>> [[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]]]
    
    0 讨论(0)
  • 2021-01-20 08:22

    a simple list comprehension should do it

    >>> 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]]
    

    Basically it scans the list and for each element it produces the whole list excluding the current element.

    Alternatively, you can use a generator expression

    >>> 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]
    

    EDIT as per your new requirements (i.e. the skipped element must be included):

    >>> 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]]
    
    0 讨论(0)
  • 2021-01-20 08:38

    use itertools.combinations():

    import itertools
    a = [100,101,102,103,104,105,106,107,108,109]
    list(itertools.combinations(a, len(a)-1))[::-1]
    
    0 讨论(0)
提交回复
热议问题