Python: filtering lists by indices

后端 未结 7 1091
情深已故
情深已故 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:06

    If you do not require a list with simultaneous access to all elements, but just wish to use all the items in the sub-list iteratively (or pass them to something that will), its more efficient to use a generator expression rather than list comprehension:

    (aList[i] for i in myIndices) 
    

提交回复
热议问题