我有一个数组列表,像这样:
data = [([-1.01201792, 2.5, 0.68665077]), ([-2.5, 0.5, 2.68189991]), ([-2.5, 3.5, 5.92202221]), ([-2.5, 5.5, 10.19026759]), ([-2.5, 6.5, 15.30091085])]
我试图获取每个数组的绝对最大值,而不考虑数组中的第二个值。例如,在第一个数组中:([--1.01201792,2.5,0.68665077]),如果不考虑2.5,则绝对最大值为1.01201792。
我希望结果看起来像这样:
result = [1.01201792, 2.68189991, 5.92202221, 10.19026759, 15.30091085]
我该如何实现?
麻木的oneliner。可能比其他方法要快,具体取决于数据的实际大小。将数据转换为numpy数组,切片,以便只有第一项和最后一项,转换为绝对值,最后在轴1上取最大值。
maxs = np.abs(np.array(data)[:, [0,2]]).max(axis=1)
细分:
import numpy as np
data = [([-1.01201792, 2.5, 0.68665077]), ([-2.5, 0.5, 2.68189991]), ([-2.5, 3.5, 5.92202221]), ([-2.5, 5.5, 10.19026759]), ([-2.5, 6.5, 15.30091085])]
arr = np.array(data) # convert into array
first_last = arr[:, [0,2]] # only keep the first and last item of each sublist
absol = np.abs(first_last) # convert to absolute values
maxs = absol.max(axis=1) # get the max value on the first axis
print(maxs)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句