【发布时间】:2020-06-11 13:52:39
【问题描述】:
我有三个列表 price1、price2 和 deviation。我想在price1 和price2 之间找到前三名的最高价格。因此,为了实现这一点,我首先按降序对它们进行排序,然后我从列表price1 和price2 中取出前三个元素,然后,我找到了每个最大值之间的最大值。现在我想知道在这一切之后获得的最大值的原始位置,以便我可以把它放在deviation中。对price1和price2进行排序之前:
price1 = [12,1,2,4,45,67,433,111,45,12] #Total 10 elements in all the lists
price2 = [23,12,233,11,54,232,323,12,42,4]
deviation = [23,45,56,67,78,786,45,34,2,1]
排序后得到前3名的价格:
print("Price1: ",sorted(price1, reverse=True))
print("Price2: ",sorted(price2, reverse=True))
输出:
Price1: [433, 111, 67]
Price2: [323, 233, 232]
要从中获得最大值:
sortedPrice1 = sorted(price1, reverse=True)
sortedPrice2 = sorted(price2, reverse=True)
print("Price1: ", sortedPrice1[:3])
print("Price2: ", sortedPrice2[:3])
for i,j in zip(sortedPrice1[:3], sortedPrice2[:3]):
print("max: ", max(i, j))
输出:
Price1: [433, 111, 67]
Price2: [323, 233, 232]
max: 433
max: 233
max: 232
现在我想要的是找到这些最大值的位置。例如:
433 is in `price1` at 6th position
233 is in `price2` at 2nd position
232 is in `price2` at 5th position
最后我想把这些仓位放到deviation列表中,得到这些价格前面的值。所以:
deviation[6] = 45
deviations[2] = 56
deviations[5] = 786
【问题讨论】:
-
你试过
list.index(number)吗? -
我觉得你在这里添加了不必要的步骤。
标签: python python-3.x list indexing