在Python中,我有一个元素列表
例如:
aList
和一个索引列表myIndices
。是否有任何方法可以一次性检索出在aList
中具有与myIndices
中值相同的索引的所有项目?例如:
>>> aList = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> myIndices = [0, 3, 4]
>>> aList.A_FUNCTION(myIndices)
['a', 'd', 'e']
[aList[i] for i in myIndices]
可以翻译为“对于myIndices中的每个i,在aList中获取索引为i的元素并将其放入一个新列表中”。 - Morwenn(aList[i] for i in myIndices)
。 - hochl