Python:按索引过滤列表

标签 python list filter indexing

在 Python 中,我有一个元素列表 aList 和一个索引列表 myIndi​​ces。有什么方法可以一次检索 aList 中的所有项目,这些项目将 myIndi​​ces 中的值作为索引?

例子:

>>> 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/

相关文章:

python - 使用 sympy 中给定的术语进行智能替换

python - 随机模块没有属性 'choices'

python - 将不同的随机列表分配给两个变量的最短方法是什么?

python - 从列表列表创建新列表并在 Python 中精确打印

python - 终止由 Python pexpect spawn 创建的 ssh 连接

python - 获取应用程序的响应

c# - 如何将一个列表分成两个可以容纳奇数个计数的列表?

filter - hasOne 按先前 hasOne 的值过滤?

php - 网上商店过滤器返回的行不正确

android - 如何在 android 中关闭搜索 View 后重置 ListView ?