Python存储切片索引作为对象 [英] Python Store Slice Index as Object
本文介绍了Python存储切片索引作为对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有一个列表列表等等......深度:
Say I have a list of lists of lists etc... of some depth:
ExampleNestedObject = numpy.ones(shape = (3,3,3,3,3))
一般来说我可以得到一个元素写作:
In general I can get an element by writing:
#Let:
#a, b, c, d, e -> are integers
print ExampleNestedObject[a][b][c][d][e]
#numpy also happens to allow:
print ExampleNestedObject[(a,b,c,d,e)]
#python in general allows:
print ExampleNestedObject[a,b,:,d,e]
我的问题是 - >如何存储索引a,b,:,d,e 作为对象?
My question is -> how can I store the index "a,b,:,d,e" as an object?
SomeSliceChoice = a,b,:,d,e
print ExampleNestedObject[SomeSliceChoice]
推荐答案
诀窍是想到一个索引对象作为切片对象的元组。
The trick is to think of an index object as a tuple of slice objects.
示例1:
Object[1,2,:] == Object[(1,2,slice(None,None,None))]
示例2:
WantedSliceObject = (1,2,slice(None,None,None), 4,5)
Object[1,2,:,4,5] == Object[WantedSliceObject]
注意'''切片的语法:
Note the syntax of '''slice:
#slice(start, stop[, step])
#1 == slice(1, 2, 1)
WantedSliceObject2 = (
slice(1, 2, 1),
slice(2, 2, 1),
slice(None,None,None),
slice(4, 2, 1),
slice(5, 2, 1)
)
#WantedSliceObject2 == WantedSliceObject
这篇关于Python存储切片索引作为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文