赞
踩
# 身高从低到高,身高相同体重从轻到重,体重相同维持原来顺序。 import numpy as np def sort_hw(n, height, weight): args = np.array(np.argsort(height)) i = 0 tmp = height[args[0]] # 记录新值 tmp_w = [weight[args[0]]] args_w = np.array([0]) while i < n-1: i += 1 if height[args[i]] == tmp: tmp_w.append(weight[args[i]]) args_w = np.append(args_w, i) if i == n-1: # 如果走到最后一步也需要做判断 if len(tmp_w) > 1: tmp_arg = np.argsort(tmp_w) # 体重排序 args_w2 = args_w[tmp_arg] args[args_w] = args[args_w2] # 按照体重重排序 else: if len(tmp_w) > 1: tmp_arg = np.argsort(tmp_w) # 体重排序 args_w2 = args_w[tmp_arg] args[args_w] = args[args_w2] # 按照体重重排序 tmp = height[args[i]] tmp_w = [weight[args[i]]] args_w = np.array([i]) return [i+1 for i in args] if __name__ == '__main__': n = 6 height = [100, 100, 100, 120, 130, 120] weight = [40, 30, 20, 60, 50, 50] print(sort_hw(n, height, weight)) n2 = 3 height2 = [90, 110, 90] weight2 = [45, 60, 45] print(sort_hw(n2, height2, weight2))
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。