本文介绍了Python:通过索引筛选列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,我有一个元素列表 aList 和一个索引列表 myIndi​​ces 。有什么办法可以一次检索所有在 aList 中有 myIndi​​ces ?

 >>> aList = ['a','b','c','d','e','f','g'] 
>>> myIndi​​ces = [0,3,4]
>>> aList.A_FUNCTION(myIndi​​ces)
['a','d','e']


解决方案

我不知道有什么办法可以做到。但是您可以使用:

 >>> [myList [i] for myIndi​​ces] 


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 having as indices the values in myIndices?

Example:

>>> aList = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> myIndices = [0, 3, 4]
>>> aList.A_FUNCTION(myIndices)
['a', 'd', 'e']
解决方案

I don't know any method to do it. But you could use a list comprehension:

>>> [aList[i] for i in myIndices]

这篇关于Python:通过索引筛选列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 16:28