我有两个numpy数组:
Values = numpy.array([5, 6, 7, 8, 1, 2, 3, 14, 15, 16])Lengths = numpy.array([4, 3, 3])
Values = numpy.array([5, 6, 7, 8, 1, 2, 3, 14, 15, 16])
Lengths = numpy.array([4, 3, 3])
在numpy中反转子列表中的顺序来得到这个的有效方法是什么?
[8, 7, 6, 5, 3, 2, 1, 16, 15, 14]
我尝试过for循环,但我相信应该有一种更有效的方法来使用numpy函数。
for
sz81bmfz1#
您可以通过在np.cumsum(Lengths)给定的所需索引处拆分数组(使用np.split),然后在每个索引反转后将它们连接(使用np.concatenate)来实现这一点。
np.cumsum(Lengths)
np.split
import numpy as npValues = np.array([5, 6, 7, 8, 1, 2, 3, 14, 15, 16])Lengths = np.array([4, 3, 3])res = np.concatenate([split[::-1] for split in np.split(Values, np.cumsum(Lengths))])print(res)
import numpy as np
Values = np.array([5, 6, 7, 8, 1, 2, 3, 14, 15, 16])
Lengths = np.array([4, 3, 3])
res = np.concatenate([split[::-1] for split in np.split(Values, np.cumsum(Lengths))])
print(res)
输出量:
[ 8 7 6 5 3 2 1 16 15 14]
ikfrs5lh2#
对于矢量解决方案,可以使用np.repeat创建一个排序器数组,然后使用np.argsort对数组进行重新排序
np.repeat
np.argsort
a = np.repeat(np.arange(len(Lengths)), Lengths)[::-1]# array([2, 2, 2, 1, 1, 1, 0, 0, 0, 0])out = Values[len(Values)-1-np.argsort(a)]
a = np.repeat(np.arange(len(Lengths)), Lengths)[::-1]
# array([2, 2, 2, 1, 1, 1, 0, 0, 0, 0])
out = Values[len(Values)-1-np.argsort(a)]
array([ 8, 7, 6, 5, 3, 2, 1, 16, 15, 14])
中间体:
len(Values)-1-np.argsort(a)# array([3, 2, 1, 0, 6, 5, 4, 9, 8, 7])
len(Values)-1-np.argsort(a)
# array([3, 2, 1, 0, 6, 5, 4, 9, 8, 7])
ukqbszuj3#
import numpy as npValues = np.array([5, 6, 7, 8, 1, 2, 3, 14, 15, 16])Lengths = np.array([4, 3, 3])splits = np.split(Values, np.cumsum(Lengths)[:-1]) #splits into 3 partsreversed_list = [np.flip(split) for split in splits] #flip/reverse all the 3 partsresult = np.concatenate(reversed_list) # join all 3 partsprint(result)
splits = np.split(Values, np.cumsum(Lengths)[:-1]) #splits into 3 parts
reversed_list = [np.flip(split) for split in splits] #flip/reverse all the 3 parts
result = np.concatenate(reversed_list) # join all 3 parts
print(result)
z6psavjg4#
根据this的讨论,切片是最快的技术之一,你可以用来分割。下面的代码只使用切片,因此不使用任何额外的空间或复制。
import numpyValues = numpy.array([5, 6, 7, 8, 1, 2, 3, 14, 15, 16])Lengths = numpy.array([4, 3, 3]) start = 0end = 0for i in range(len(Lengths)): end = end + Lengths[i] if i == 0: ## Isolate the first scenario as splitting doesn't accept -1 as second argument. [end:-1:step] is not valid Values[0:end] = Values[end-1::-1] else: Values[start:end] = Values[end-1:start-1:-1] start = end print(Values)
import numpy
start = 0
end = 0
for i in range(len(Lengths)):
end = end + Lengths[i]
if i == 0: ## Isolate the first scenario as splitting doesn't accept -1 as second argument. [end:-1:step] is not valid
Values[0:end] = Values[end-1::-1]
else:
Values[start:end] = Values[end-1:start-1:-1]
start = end
print(Values)
odopli945#
您可以构建一个索引列表,这些索引相对于每个块的最后一个索引反转位置。然后将其用作值数组的间接寻址。
import numpy as npvalues = np.array([5, 6, 7, 8, 1, 2, 3, 14, 15, 16])lengths = np.array([4, 3, 3])s = np.cumsum(lengths) - 1s[1:] += s[:-1] + 1i = np.repeat(s, lengths) - np.arange(values.size)print(values[i]) # [ 8 7 6 5 3 2 1 16 15 14]
values = np.array([5, 6, 7, 8, 1, 2, 3, 14, 15, 16])
lengths = np.array([4, 3, 3])
s = np.cumsum(lengths) - 1
s[1:] += s[:-1] + 1
i = np.repeat(s, lengths) - np.arange(values.size)
print(values[i])
# [ 8 7 6 5 3 2 1 16 15 14]
5条答案
按热度按时间sz81bmfz1#
您可以通过在
np.cumsum(Lengths)
给定的所需索引处拆分数组(使用np.split
),然后在每个索引反转后将它们连接(使用np.concatenate)来实现这一点。输出量:
ikfrs5lh2#
对于矢量解决方案,可以使用
np.repeat
创建一个排序器数组,然后使用np.argsort
对数组进行重新排序输出量:
中间体:
ukqbszuj3#
z6psavjg4#
根据this的讨论,切片是最快的技术之一,你可以用来分割。下面的代码只使用切片,因此不使用任何额外的空间或复制。
odopli945#
您可以构建一个索引列表,这些索引相对于每个块的最后一个索引反转位置。然后将其用作值数组的间接寻址。