我需要做一個排列,看起來像1-N序列,0填充到位#k。在Python中進行移位排列
這種方法的工作,但有什麼更簡單的使用內置函數?
def permshift(n,k):
return [0 if x == k else x+(x<k) for x in range(n)]
>>> permshift(7,0)
[0, 1, 2, 3, 4, 5, 6]
>>> permshift(7,1)
[1, 0, 2, 3, 4, 5, 6]
>>> permshift(7,2)
[1, 2, 0, 3, 4, 5, 6]
>>> permshift(7,3)
[1, 2, 3, 0, 4, 5, 6]
>>> permshift(7,4)
[1, 2, 3, 4, 0, 5, 6]
>>> permshift(7,5)
[1, 2, 3, 4, 5, 0, 6]
>>> permshift(7,6)
[1, 2, 3, 4, 5, 6, 0]
3相同的答案,在10秒內。 :)刪除我的 – JBernardo
@JBernardo四,如果你算上我準備粘貼的那個,當它告訴我有三個新答案時。 :^) – DSM
@JBernardo:是的,我知道那是什麼感覺。 :] – voithos