简体   繁体   English

如何以不规则的间隔切片列表?

[英]How to slice a list with irregular intervals?

I the following list I want to do slicing such that my final result is [1,1.5,2,2.5,4.5,5] . 我想对以下列表进行切片,以使最终结果为[1,1.5,2,2.5,4.5,5]。 I tried to access by indices but it gives me error. 我试图按索引访问,但它给了我错误。

`list = [1,1.5,2,2.5,3,3.5,4,4.5,5]
print(list)
print(list[0,1,2,3,7,8])`

You can't use a list of indexes to slice or index a list. 您不能使用索引列表来对列表进行切片或索引。 You have to use a loop—eg, via a comprehension , or a function with an implicit loop in it like the one returned by itemgetter : 您必须使用一个循环,例如通过comprehension或其中包含隐式循环的函数,例如itemgetter返回的循环:

>>> lst = [1,1.5,2,2.5,3,3.5,4,4.5,5]
>>> print([lst[i] for i in [0, 1, 2, 3, 7, 8]])
[1, 1.5, 2, 2.5, 4.5, 5]
>>> print(operator.itemgetter(0, 1, 2, 3, 7, 8)(lst))
[1, 1.5, 2, 2.5, 4.5, 5]

But you can do something like this with numpy arrays. 但是您可以使用numpy数组执行类似的操作。 You can't directly use the six indices (because that would mean multidimensional indexing), but you can use any array-like, such as a list, containing those six indices: 您不能直接使用六个索引(因为这意味着多维索引),但是您可以使用任何包含这六个索引的类似数组的形式(例如列表):

>>> arr = np.array(lst)
>>> print(arr[[0, 1, 2, 3, 7, 8]])
[1.  1.5 2.  2.5 4.5 5. ]

This is explained in Index arrays in the basic indexing tutorial. 在基本索引教程的“ 索引数组”中对此进行了说明。

Python does not support irregular intervals. Python不支持不规则间隔。 You must use a list comprehension. 您必须使用列表理解。

lst = [1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5]
indices = (0, 1, 2, 3, 7, 8)
slice = [lst[i] for i in indices]

print(slice) # [1, 1.5, 2, 2.5, 4.5, 5]

Also, do not use names such as list for your variables as this overshadows builtin functions. 另外,请勿使用诸如list名称作为变量,因为这会掩盖内置函数。

Work the other way around; 反过来工作; instead of selecting a non-contiguous range, remove the contiguous range you don't want. 而不是选择一个非连续范围,请删除不需要的连续范围。 You just want to get the original list minus indices 4-6 (inclusive), so copy your list , then del the indices you don't need: 您只想获取原始list减去索引4-6(含索引),因此复制list ,然后del不需要的索引:

mylist = [1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5]
trimmed = mylist[:]
del trimmed[4:7]
print(trimmed)

Alternatively, you could use operator.itemgetter to retrieve the specific indices you listed, eg: 另外,您可以使用operator.itemgetter检索列出的特定索引,例如:

from operator import itemgetter

mylist = [1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5]
print(itemgetter(0, 1, 2, 3, 7, 8)(mylist))

itemgetter of multiple values will output a tuple , so if that bothers you, you'd have to convert back to list , eg print(list(itemgetter(0, 1, 2, 3, 7, 8)(mylist))) (and note, the choice to change your variable name to mylist made it possible to access the list constructor, which would have been impossible if you named your variable list ). 包含多个值的itemgetter将输出一个tuple ,因此,如果这困扰您,则必须转换回list ,例如print(list(itemgetter(0, 1, 2, 3, 7, 8)(mylist))) (请注意,将变量名更改为mylist的选择使访问list构造函数成为可能,如果将变量list命名为,这是不可能的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM