Python: filtering lists by indices

后端 未结 7 1088
情深已故
情深已故 2020-12-01 11:56

In Python I have a list of elements aList and a list of indices myIndices. Is there any way I can retrieve all at once those items in aList

7条回答
  •  情深已故
    2020-12-01 12:27

    You could use map

    map(aList.__getitem__, myIndices)
    

    or operator.itemgetter

    f = operator.itemgetter(*aList)
    f(myIndices)
    

提交回复
热议问题