在 Python 中,我有一个元素列表 aList
和一个索引列表 myIndices
。有什么方法可以一次检索 aList
中的所有项目,这些项目将 myIndices
中的值作为索引?
例子:
>>> aList = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> myIndices = [0, 3, 4]
>>> aList.A_FUNCTION(myIndices)
['a', 'd', 'e']
最佳答案
我不知道有什么方法可以做到这一点。但是您可以使用 list comprehension :
>>> [aList[i] for i in myIndices]
关于Python:按索引过滤列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11847491/