I am trying to make some binary singal system by boolean variables I named with LIGHTx.
LIGHT1 = True
LIGHT2 = True
LIGHT3 = False
LIGHT4 = False
Next, I nest these variables into a list for future calculation,
signal = [LIGHT1, LIGHT2, LIGHT3, LIGHT4]
Currently I am using the idea from Python: Boolean List to Binary String and Convert base-2 binary number string to int to convert the list to int number which is my signal. Here, [1,1,0,0] means 12.
In [97]: boolList2BinString(signal)
Out[97]: 12
My questions are:
- How can I automatically updating the elements of "signal" by updating the value of the LIGHTs, rather than running
signal = [LIGHT1, LIGHT2, LIGHT3, LIGHT4]
again and again? Whitch means, in the rest of my codes, I only need to runLIGHTx = xxxx
andboolList2BinString(signal)
. (Maybe some way like pointer of C++ ?) - If it is impossible with question 1, is there any way that I can fix the order of the LIGHTs in the list?
[Update]
Please exclude the way that building the 'signal' list inside the 'boolList2BinString' function.
Original:
def boolList2BinString(lst):
return int('0b' + ''.join(['1' if x else '0' for x in lst]), 2)
Building inside:
def boolList2BinString():
osignal = [LIGHT1 , LIGHT2 , LIGHT3 , LIGHT4 ]
return int('0b' + ''.join(['1' if x else '0' for x in signal ]), 2)
Thanks in advance!