2018年1月8日 星期一

Python - get all permutation of a list w/o repetitions

from itertools import combinations

lis = [1, 2, 3, 4]
for i in range(1, len(lis) + 1):
    #range will return the values 1,2,3,4 in this loop
    print (list(combinations(lis, i)),'\n')

print (list(combinations([1, 2, 3, 4], 3)))


===================== RESTART: F:/Python_APSC/b038-2.py =====================

[(1,), (2,), (3,), (4,)] 

[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)] 

[(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)] 

[(1, 2, 3, 4)] 


[(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)] 


>>> 

沒有留言:

張貼留言

2026 作業3 RFID+ Telegram 練習

 2026 作業3  RFID+ Telegram  練習 (Wokwi 與 Telegram 二者溝通訊息反映比較慢 ) 歡迎 Alex 使用 RFID 控制系統 /on : 開啟 LED /off : 關閉 LED /flash : 閃爍模式 /timer : 開啟 5 秒 ...