pythonjoinpropositional-calculus

Python - join propositions


I have an list of strings which illustrate propositions. E.g.

L = ['(A∧B)', 'A', 'B']

My aim is to join each element with the string '∧' and brackets "()", which results in following string:

aim = "(((A ∧ B) ∧ A) ∧ B)"

is their a simple method to do that?


Solution

  • Use reduce from functools module:

    from functools import reduce
    
    aim = reduce(lambda l, r: f"({l} ^ {r})", L)
    print(aim)
    
    # Output
    (((A∧B) ^ A) ^ B)