Complex list slice/index in python

馋奶兔 提交于 2019-12-04 03:43:19

Use operator.itemgetter():

from operator import itemgetter

itemgetter(1, 5, 6, 8, 9, 12)(lst)

Demo:

>>> from operator import itemgetter
>>> lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
>>> itemgetter(1, 5, 6, 8, 9, 12)(lst)
(2, 6, 7, 9, 10, 13)

This returns a tuple; cast to a list with list(itemgetter(...)(lst)) if a that is a requirement.

Note that this is the equivalent of a slice expression (lst[start:stop]) with a set of indices instead of a range; it can not be used as a left-hand-side slice assignment (lst[start:stop] = some_iterable).

Numpy arrays have this kind of slicing syntax:

In [45]: import numpy as np

In [46]: lst = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13])

In [47]: lst[[1, 5, 6, 8, 9, 12]]
Out[47]: array([ 2,  6,  7,  9, 10, 13])

It's easily and straightforwardly done using a list comprehension.

lst = range(1, 14)
indices = [1, 5, 6, 8, 9, 12]
filtered_lst = [lst[i] for i in indices]
inspectorG4dget

I'd go with the operator.itemgetter() method that Martijn Pieters has suggested, but here's another way (for completeness)

In [23]: lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

In [24]: indices = set([1, 5, 6, 8, 9, 12])

In [25]: [n for i,n in enumerate(lst) if i in indices]
Out[25]: [2, 6, 7, 9, 10, 13]

A Python slice allows you to make the slice the target of an assignment. And the Python slicing syntax does not allow for slices with irregular patterns of indices. So, if you want to make your "custom" slice the target of an assignment, that's not possible with Python slice syntax.

If your requirements are met by taking a copy of the specified elements, then operator.itemgetter() meets your needs. If you need slice assignment, then numpy slices are a good option.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!