Store binary variable in list mode

Asked

Viewed 461 times

1

How can I store the binary variable in horizontal line type list mode with all the values coming 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)
  • 2

    What is the function dec_to_bin? What are the values of n? What is the result that appears when you execute the code? Finally, what would be the desired result?

  • The dec_to_bin function converts the decimal value of each character of a string, for example the decimal values of each letter of this "aeiou" phrase to its binary value. when I run the print appears

  • The dec_to_bin function converts the decimal value of each character of a string, for example the decimal values of each letter of this phrase "aeiou" to its binary value, when I print appears Rx_binario: ['1', '1', '0', '0', '0','0', '1'] to the letter "a" in the following line Rx_binario: ['1', '1', '0', '0', '1', '0', '1'] for the letter "and" and I wanted to put it all in the same line and not in separate lines, so Rx_binario ['1', '1', '0', '0', '0','0', '1', '1', '0', '0', '1', '0', '0', '1'']

  • Information relating to the question should be added to the question with the [Edit].

  • He did not let me edit again because I made a second comment with the answer to the question

1 answer

1

It is not very clear whether 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.

As, in my view, it would not make much sense to just display on the same line and store separately, I will consider that you also want to store on the same list. To this end, considering that 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)

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

This way, the result will look like:

binario = ['1', '1', '0', '0', '0','0', '1' ,'1', '1', ...]
  • sorry what I have is this :binario = [] for n in receivedMessage: counter += 1 print (counter) binario = list(dec_to_bin(n)) print(binario) and yes the final value is the intended one

  • @Sergionunes I didn’t understand. That’s exactly what I answered or got something wrong?

  • that’s right, thanks for the help. I might ask if I might ask how to compare two bit-by-bit strings from the binary list? i have an if to make the comparison, have it so : if string!= counterword 2 += 1 print('number of errors: ' , counter2) here I can not put the code as it should be

Browser other questions tagged

You are not signed in. Login or sign up in order to post.