【发布时间】:2012-08-04 13:23:32
【问题描述】:
在 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] -
如果你只想遍历元素,我建议改用生成器表达式:
(aList[i] for i in myIndices)
标签: python list filter indexing