Dan*_*rén 108 python indexing list
我想用这样的另一个列表索引列表
L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
Idx = [0, 3, 7]
T = L[ Idx ]
Run Code Online (Sandbox Code Playgroud)
和T应该最终成为包含['a','d','h']的列表.
有没有比这更好的方法
T = []
for i in Idx:
T.append(L[i])
print T
# Gives result ['a', 'd', 'h']
Run Code Online (Sandbox Code Playgroud)
van*_*van 210
T = [L[i] for i in Idx]
Run Code Online (Sandbox Code Playgroud)
Pau*_*aul 36
如果你正在使用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')
Run Code Online (Sandbox Code Playgroud)
...并且可能要快得多(如果性能足以引起numpy导入的麻烦)
功能方法:
a = [1,"A", 34, -123, "Hello", 12]
b = [0, 2, 5]
from operator import itemgetter
print(list(itemgetter(*b)(a)))
[1, 34, 12]
Run Code Online (Sandbox Code Playgroud)
我对这些方法中的任何一种都不满意,所以我想出了一个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]
Run Code Online (Sandbox Code Playgroud)
对于您的示例,您可以将其用作:
L = Flexlist(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])
Idx = [0, 3, 7]
T = L[ Idx ]
print(T) # ['a', 'd', 'h']
Run Code Online (Sandbox Code Playgroud)
您还可以__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']
Run Code Online (Sandbox Code Playgroud)