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)] 


>>> 

沒有留言:

張貼留言

WOKWI ESP32 LED Control , Node-Red MQTT SQLITE  

WOKWI ESP32 LED Control ,  Node-Red  MQTT SQLITE   const char broker[] = "test.mosquitto.org" ; //const char broker[] = "br...