Explicitly select items from a list or tuple

前端 未结 8 1810

I have the following Python list (can also be a tuple):

myList = [\'foo\', \'bar\', \'baz\', \'quux\']

I can say

>>&g         


        
相关标签:
8条回答
  • 2020-11-27 11:25

    Another possible solution:

    sek=[]
    L=[1,2,3,4,5,6,7,8,9,0]
    for i in [2, 4, 7, 0, 3]:
       a=[L[i]]
       sek=sek+a
    print (sek)
    
    0 讨论(0)
  • 2020-11-27 11:25

    like often when you have a boolean numpy array like mask

    [mylist[i] for i in np.arange(len(mask), dtype=int)[mask]]

    A lambda that works for any sequence or np.array:

    subseq = lambda myseq, mask : [myseq[i] for i in np.arange(len(mask), dtype=int)[mask]]

    newseq = subseq(myseq, mask)

    0 讨论(0)
提交回复
热议问题