Save binary variable in list mode

1

How can I save the binary variable in horizontal line type list mode with all the values that come out in the binary variable.

for n in receivedMessage:
    contador += 1
    print (contador)
    binario=list(dec_to_bin(n))# assim sai cada valor na vertical 
    print("RX_Binario: ", binario)
    
asked by anonymous 27.09.2017 / 11:24

1 answer

1

It was not very clear if you just want to display everything on the same line or store, in fact, in the same list, since you are overwriting the value of binario at each iteration.

Since, in my view, it would not make much sense to only display on the same line and store separately, I'll consider that you'd like to store the same list as well. To do so, whereas the return of dec_to_bin is a string , which is converted to a list with list(...) , then you can do:

binario = []

for n in receivedMessage:
    contador += 1
    print (contador)
    binario += list(dec_to_bin(n))

print(binario)

Notice that the print(binario) is outside the loop to print only the final value. The changes that modify the logic are: 1. initially define binario as an empty list and 2. within the loop only increment its value using the += operator.

In this way, the result will look something like:

binario = ['1', '1', '0', '0', '0','0', '1' ,'1', '1', ...]
    
27.09.2017 / 14:52