我正在尝试解决一个问题。在试图改变列表值时,我观察到了奇怪的列表行为。我无法更改列表元素的值。
top = list(map(int, raw_input().split()))
staff = list(map(int, raw_input().split()))
ceo = top[0]
coo = top[1]
cto = top[2]
top.extend(staff)
alls = sorted(top)
tot = len(alls)
print(alls)
alls[tot/2], alls[alls.index(ceo)] = alls[alls.index(ceo)], alls[tot/2]
print(alls)
alls[0], alls[alls.index(coo)] = alls[alls.index(coo)], alls[0]
alls[-1], alls[alls.index(cto)] = alls[alls.index(cto)], alls[-1]
print(alls)
这是程序的输出:
输入:
13 11 17
12 10
输出
[10, 11, 12, 13, 17]
[10, 11, 12, 13, 17]
[10, 11, 12, 13, 17]
所有列表值为什么都没有改变?我做错了什么吗?
编辑: 问题陈述:https://www.hackerearth.com/codejunk/algorithm/steal-the-show/ 我知道我的方法不是解决这个问题的最佳方式,但我想知道为什么列表值没有改变?