简体   繁体   English

在 Python 中,如何用另一个列表索引一个列表?

[英]In Python, how do I index a list with another list?

I would like to index a list with another list like this我想用这样的另一个列表索引一个列表

L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
Idx = [0, 3, 7]
T = L[ Idx ]

and T should end up being a list containing ['a', 'd', 'h'].并且 T 最终应该是一个包含 ['a', 'd', 'h'] 的列表。

Is there a better way than有没有比这更好的方法

T = []
for i in Idx:
    T.append(L[i])

print T
# Gives result ['a', 'd', 'h']
T = [L[i] for i in Idx]

If you are using numpy, you can perform extended slicing like that:如果您使用的是 numpy,则可以像这样执行扩展切片:

>>> import numpy
>>> a=numpy.array(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])
>>> Idx = [0, 3, 7]
>>> a[Idx]
array(['a', 'd', 'h'], 
      dtype='|S1')

...and is probably much faster (if performance is enough of a concern to to bother with the numpy import) ...而且可能要快得多(如果性能足以让 numpy 导入烦恼)

A functional approach:函数式方法:

a = [1,"A", 34, -123, "Hello", 12]
b = [0, 2, 5]

from operator import itemgetter

print(list(itemgetter(*b)(a)))
[1, 34, 12]
T = map(lambda i: L[i], Idx)

I wasn't happy with any of these approaches, so I came up with a Flexlist class that allows for flexible indexing, either by integer, slice or index-list:我对这些方法中的任何一种都Flexlist ,所以我想出了一个Flexlist类,它允许通过整数、切片或索引列表进行灵活的索引:

class Flexlist(list):
    def __getitem__(self, keys):
        if isinstance(keys, (int, slice)): return list.__getitem__(self, keys)
        return [self[k] for k in keys]

Which, for your example, you would use as:对于您的示例,您可以将其用作:

L = Flexlist(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])
Idx = [0, 3, 7]
T = L[ Idx ]

print(T)  # ['a', 'd', 'h']

You could also use the __getitem__ method combined with map like the following:您还可以将__getitem__方法与map结合使用,如下所示:

L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
Idx = [0, 3, 7]
res = list(map(L.__getitem__, Idx))
print(res)
# ['a', 'd', 'h']
L= {'a':'a','d':'d', 'h':'h'}
index= ['a','d','h'] 
for keys in index:
    print(L[keys])

I would use a Dict add desired keys to index我会使用Dict add所需的keys Dict addindex

My problem: Find indexes of list.我的问题:查找列表的索引。

L = makelist() # Returns a list of different objects
La = np.array(L, dtype = object) # add dtype!
for c in chunks:
    L_ = La[c] # Since La is array, this works.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:如何用另一个列表索引一个列表,然后用另一个列表再次索引它? - Python: how do index a list with another list, then index it again with another list? 如何按 Python 中另一个列表的索引号对列表进行排序? - How do I sort a list by another list's index number in Python? 如何确定一个列表是否包含另一个列表中的项目,然后在 Python 中打印它们的索引 - how do I determine if one list has items contained in another list, then print their index in Python Python:如何在重复的列表中始终找到下一个索引? - Python: How do I consistently find the next index after another index in a list with duplicates? 如何在python的嵌套列表中找到列表的索引 - How do I find index of list in nested list in python 如何单独打印python中另一个列表中的列表项 - How do I individually print items of a list that are in another list in python 如何使用python在另一个列表中找到一个列表? - How do i find one list in another list with python? 如何在 Python 的另一个列表中迭代搜索列表元素? - How do I iterate searching for a list element in another list in Python? 我如何将列表与 python 中的另一个列表匹配 - How do i match list with another list in python 如何将元素列表与 Python 中的另一个列表列表相乘 - How do I multiply a list of elements with another list of lists in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM