简体   繁体   English

用字典切片NumPy数组

[英]Slicing NumPy array with dictionary

Is there a simple option to slice a NumPy array with the predefined dictionary of indices? 是否有一个简单的选项可以使用预定义的索引字典对NumPy数组进行切片?

For example: 例如:

>> a = array([3, 9, 1, 5, 5])

and (fictitious) dictionary: 和(虚构的)字典:

>> index_dict = {'all_except_first': (1:None), 'all_except_last': (None:-1)}

and then: 接着:

>> a[index_dict['all_except_first']]
>> array([9, 1, 5, 5])
>> a[index_dict['all_except_first']]
>> array([3, 9, 1, 5])

Sort of slicing with names and not with numbers. 切片的名称而不是数字切片。

Create slice s: 创建slice

>>> index_dict = {'all_except_first': slice(1, None), 'all_except_last': slice(None, -1)}
>>>
>>> a[index_dict['all_except_first']]
array([9, 1, 5, 5])
>>> a[index_dict['all_except_last']]
array([3, 9, 1, 5])

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

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