我想在某个索引中转换一个切片,以便一次可以访问向量的每个元素。
你可以用 indices range . 不过,您需要知道实例的长度。
indices
range
例如:
>>> sl = slice(2) # or wherever you get that from >>> length = 100 >>> list(range(*sl.indices(length))) [0, 1]