我有两个列表,我想以相反的顺序对其中一个列表(scores
)进行排序,并获取相应的索引以对第二个列表进行排序(section_id
)。
例如:
section_id = [5, 6, 8, 14]
scores = [4, 11, 13, 7]
新名单将是:
sorted_reverse_scores = [13, 11, 7, 4]
sorted_section_id = [8, 6, 14, 5]
你知道如何实现这个目标吗?
目前我唯一做的是:
sorted_reverse_scores = section_id.sort(reverse=True)
答案 0 :(得分:1)
section_id = [5, 6, 8, 14]
scores = [4, 11, 13, 7]
sorted_reverse_scores =[]
sorted_section_id =[]
for i in sorted(zip(scores,section_id),reverse=True):
sorted_reverse_scores.append(i[0])
sorted_section_id.append(i[1])
print(sorted_reverse_scores)
print(sorted_section_id)
输出
[13,11,7,4] [8,6,14,5]