python build a dynamic growing truth table

后端 未结 7 1943
失恋的感觉
失恋的感觉 2021-02-07 04:36

My question is simple: \"how to build a dynamic growing truth table in python in an elegant way?\"

for n=3

for p in False, True:
    for q in False, Tru         


        
7条回答
  •  名媛妹妹
    2021-02-07 04:37

    Use itertools.product():

    table = list(itertools.product([False, True], repeat=n))
    

    Result for n = 3:

    [(False, False, False),
     (False, False, True),
     (False, True, False),
     (False, True, True),
     (True, False, False),
     (True, False, True),
     (True, True, False),
     (True, True, True)]
    

提交回复
热议问题