Re: [問題]沒頭緒的一個問題...

看板Python作者 (lun)時間15年前 (2010/04/11 00:33), 編輯推噓3(303)
留言6則, 4人參與, 最新討論串2/5 (看更多)
: 輸入一字串 例如 abc : 再輸入一串密碼 例如312 : a b c : 3 1 2(a放到第3個位置 b放到第1個位置 c放到第2個位置) : print的結果 : = bca inp = raw_input("string: ") position = raw_input("position: ") out = [] for index in range(len(inp)): out.insert(int(position[index])-1, inp[index]) print "=", "".join(out) -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.163.22.15

04/11 00:39, , 1F
謝謝 不過我不是很懂 因為有些東西還沒學過
04/11 00:39, 1F

04/11 01:55, , 2F
z=zip(pos,str); z.sort(lamdb key:key[0]);
04/11 01:55, 2F

04/11 01:55, , 3F
print "".join([s[1] for s in z])
04/11 01:55, 3F

04/11 09:12, , 4F
這樣abc, 321會錯,要改成out = [0] * len(inp)
04/11 09:12, 4F

04/11 09:12, , 5F
out[int(position[index]) -1] = inp[index]
04/11 09:12, 5F

04/11 12:01, , 6F
恩,樓上才對,我沒有把順序考慮進去@@
04/11 12:01, 6F
文章代碼(AID): #1BmAXV-O (Python)
討論串 (同標題文章)
文章代碼(AID): #1BmAXV-O (Python)