[英]Slice list where index values are strings?
I have loaded a black and white image as a numpy array. 我已将黑白图像加载为numpy数组。 The array shape is equal to the pixels in the image.
阵列形状等于图像中的像素。 I want to extract certain ranges of pixel values, for instance,
我想提取一定范围的像素值,例如,
numpy_array_to_slice[160:300,28:43]
etc etc, but I don't want to hardcode the index numbers. 等等等等,但是我不想对索引号进行硬编码。 Rather, I want to load the index values from a list of values.
相反,我想从值列表中加载索引值。 For instance, I have a list of index values like:
例如,我有一个索引值列表,例如:
listofindexvalues = [['160:300,28:43'],['160:300,40:55'],['160:300,28:43']]
So effectively I want something like: 因此,我实际上想要这样的东西:
numpy_array_to_slice[listofindexvalues[0]]
which would take the place of: 它将代替:
numpy_array_to_slice['160:300,28:43']
I've tried a variety of things which haven't worked like: 我已经尝试了许多不起作用的方法:
first,second = str(index_list[19]).replace('[','').replace(']','').replace('\'','').split(':') ##for just one side of an index value, such as 28:59
and trying to pass that like so: 并试图像这样通过:
numpy_array_to_slice[int(first)+':'+int(second]
but that doesn't work because I can't concatenate those values. 但这不起作用,因为我无法连接这些值。 Is there any way to accomplish this?
有什么办法可以做到这一点?
Slicing is syntax , not a string. 切片是语法 ,而不是字符串。 See the Slicings expressions reference documentation :
请参阅Slicings表达式参考文档 :
The semantics for a slicing are as follows.
切片的语义如下。 The primary is indexed (using the same
__getitem__()
method as normal subscription) with a key that is constructed from the slice list, as follows.主键使用从切片列表构造的键进行索引(使用与普通订阅相同的
__getitem__()
方法),如下所示。 If the slice list contains at least one comma, the key is a tuple containing the conversion of the slice items ;如果切片列表至少包含一个逗号,则键为一个包含切片项转换的元组 ; otherwise, the conversion of the lone slice item is the key.
否则,单独切片项的转换是关键。 The conversion of a slice item that is an expression is that expression.
作为表达式的切片项的转换就是该表达式。 The conversion of a proper slice is a
slice
object (see section The standard type hierarchy ) whosestart
,stop
andstep
attributes are the values of the expressions given as lower bound, upper bound and stride, respectively, substitutingNone
for missing expressions.适当切片的转换是一个
slice
对象(请参见“标准类型层次结构”一节),其start
,stop
和step
属性分别是表达式的值,分别指定为下限,上限和步幅,用None
代替缺少的表达式。
Bold emphasis mine. 大胆强调我的。
You can bypass this conversion (from slice syntax to slice()
objects) by creating slice()
objects directly; 您可以通过直接创建
slice()
对象来绕过这种转换(从slice语法到slice()
对象)。 you can put them in tuples if need be. 您可以根据需要将它们放入元组中。
So 所以
numpy_array_to_slice[160:300,28:43]
is equivalent to 相当于
box = slice(160, 300), slice(28, 43)
numpy_array_to_slice[box]
I've omitted the step
argument; 我省略了
step
参数; it defaults to None
when omitted. 省略时默认为
None
。
Extending this to your list would be: 将其扩展到您的列表将是:
listofindexvalues = [
(slice(160, 300), slice(28, 43)),
(slice(160, 300), slice(40, 55)),
(slice(160, 300), slice(28, 43))
]
for box in listofindexvalues:
sliced_array = numpy_array_to_slice[box]
You should use a list of slice
instances (or tuples thereof), not strings. 您应该使用
slice
实例(或其元组)列表,而不是字符串。
Here's an example. 这是一个例子。
>>> import numpy as np
>>>
>>> listofindexvalues = [(slice(1, 6), slice(3, 4))]
>>> a = np.arange(100).reshape(10,10)
>>> a[listofindexvalues[0]]
array([[13],
[23],
[33],
[43],
[53]])
a[listofindexvalues[0]]
is equivalent to a[1:6, 3:4]
in this case. 在这种情况下
a[1:6, 3:4]
a[listofindexvalues[0]]
等效于a[1:6, 3:4]
a[listofindexvalues[0]]
a[1:6, 3:4]
。
>>> a[1:6, 3:4]
array([[13],
[23],
[33],
[43],
[53]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.