binary_list = [['0', '0', '1', '0', '1'], ['1', '0', '1', '0', '1'], ['0', '0', '0', '0', '0'], ['0', '1', '0', '0', '1'], ['1', '1', '0', '0', '0'], ['0', '0', '1', '0', '1'], ['0', '0', '0', '1', '0'], ['0', '0', '1', '0', '0']]
cipher_value = []
cipher_num = 0
for list in binary_list:
for num in range(len(list)):
if num == 0 and list[num] == 1:
cipher_num += 16
elif num == 0 and list[num] == 0:
continue
if num == 1 and list[num] == 1:
cipher_num += 8
elif num == 1 and list[num] == 0:
continue
if num == 2 and list[num] == 1:
cipher_num += 4
elif num == 2 and list[num] == 0:
continue
if num == 3 and list[num] == 1:
cipher_num += 2
elif num == 3 and list[num] == 0:
continue
if num == 4 and list[num] == 1:
cipher_num += 1
elif num == 4 and list[num] == 0:
continue
cipher_value.append(cipher_num)
cipher_num = 0
break
Asked
Active
Viewed 110 times
1

BrokenBenchmark
- 18,126
- 7
- 21
- 33

john sims
- 13
- 2
-
Can you explain a bit more in detail what you are trying to do here ? – SARAN SURYA Jan 11 '22 at 02:34
-
I have written a few different for loops but each one (including the one above) results in outputting cipher_value = [0,0,0,0,0,0,0,0] I don't understand why the cipher_num is not incrementally increasing when the requirements are met. I'm just learning so any and all help is appreciated!! – john sims Jan 11 '22 at 02:34
-
Can you explain the question in a bit more detail ? The title alone doesn't help – SARAN SURYA Jan 11 '22 at 02:35
-
While others are posting alternative solutions, note that in your original solution, `list[num] == 1` is comparing a string to an int, so it will always be false. You could change it to `list[num] == '1'`. – Dennis Jan 11 '22 at 02:39
-
Yeah, the title only let me put so much lol So I am trying to iterate through each nested list in "binary_list" and append their representative value to the list "cipher_value" – john sims Jan 11 '22 at 02:51
-
Wow! Thank you, Dennis! I feel like a total dingleberry! I need more coffee! – john sims Jan 11 '22 at 02:53
2 Answers
2
You can use int(..., 2)
to convert the binary representation (in string type) of a number into an integer. So, with help of join
and list comprehension:
cipher_value = [int(''.join(sublist), 2) for sublist in binary_list]
print(cipher_value) # [5, 21, 0, 9, 24, 5, 2, 4]

j1-lee
- 13,764
- 3
- 14
- 26
0
For each element in the list, you can convert the binary representation to a decimal integer by using bit-shifting. This has the advantage of being able to work with a variable number of binary numbers, as well as variable-length binary numbers:
binary_list = [['0', '0', '1', '0', '1'], ['1', '0', '1', '0', '1'], ['0', '0', '0', '0', '0'], ['0', '1', '0', '0', '1'], ['1', '1', '0', '0', '0'], ['0', '0', '1', '0', '1'], ['0', '0', '0', '1', '0'], ['0', '0', '1', '0', '0']]
cipher_values = []
for binary_repr in binary_list:
cipher_num = 0
for bit in binary_repr:
cipher_num <<= 1
cipher_num += int(bit)
cipher_values.append(cipher_num)
print(cipher_values)

BrokenBenchmark
- 18,126
- 7
- 21
- 33