看板 Python 關於我們 聯絡資訊
import itertools # repeat means how many elements you want in this combination. for args in itertools.product(range(10), repeat=3): print list(args) EDIT: # 如果你跟我一樣是一行控的話... print map(lambda x: list(x), itertools.product(range(10), repeat=3)) ※ 引述《aegisqoo (flymetomoon)》之銘言: : 大家好,我新手也有爬文,有個問題 : 假設 a=[x,y,z] 且 0 <= x,y,z <= 9 的 x.y.z組合 : 如下: : for x in range(10): : for y in range(10): : for z in range(10): : a=[x,y,z] : print(a) : 那問題是,有辦法更簡短嗎?,假設今天a=[b,c,d,e,...z] : 整個for迴圈就打很長,在網路上有看到指令 : itertools中permutations可以直接排列,但想知道假設不用 : 這個指令,且只能用一般循環指令來寫,有辦法更簡單嗎? : 或者是有人可以解釋itertools.permutations的原始代碼邏輯嗎? : 謝謝大大 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.221.35.72 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1508296311.A.0B7.html ※ 編輯: rexyeah (61.221.35.72), 10/18/2017 11:20:36
bibo9901: 你要是一行控的話應該是 map(list, ...) 10/18 11:29
rexyeah: ah, that's better. 10/18 11:33
uranusjr: lambda x: list(x) -> 脫褲子放屁, 直接寫 list 就好 10/18 15:42
rexyeah: 謝謝指教lambda順手了,下次換你用嘴巴放表演一下 10/18 15:58