长风秋雁
a = [6, 8, 1, 0, 5, 0]sorted_positions = {x: i for i, x in enumerate(sorted(a))}# {0: 1, 1: 2, 5: 3, 6: 4, 8: 5}indices = [sorted_positions[x] for x in a]# [4, 5, 2, 1, 3, 1]zeroes = a.count(0)# 2answer = [ 0 if x == 0 else i - zeroes + 1 for i, x in zip(indices, a)]# [3, 4, 1, 0, 2, 0]不认识语法时要搜索的术语:列表推导式、字典推导式和 python 三元运算符。对于这种情况a=[3,3,1,1,2,2],这给出了[6, 6, 2, 2, 4, 4].