我想解決一個問題。在嘗試更改列表值時,我觀察到一個奇怪的列表行爲。我無法更改列表元素的值。Python列表值不變
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/
我知道我的做法是不是解決這個問題的最好辦法,但我只是想知道爲什麼名單的值沒有改變?
什麼是代碼應該是做什麼 –
其正在進行的比賽的問題,因此不能鏈接的問題。反正它有關係嗎? –
是的,它很重要,因爲你剛剛傾銷了一個代碼負載零解釋什麼應該發生什麼,爲什麼 –