Explicitly select items from a list or tuple

前端 未结 8 1944

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

    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)

提交回复
热议问题