Reputation: 738
I have the following code of two binary lists and I want to obtain a resulting list in which the element i
will be the OR
operation applied to the i
elements of the two lists:
from operator import ior
l_0 = [01100]
l_1 = [11000]
print map(ior, l_0, l_1)
And I was expecting a result of [11100]
, but the result is:
[11000]
I have checked ior operator and the documentation says that it performs the operation:
a = ior(a, b) is equivalent to a |= b
So I tried the following to check as well:
print ior(0,0)
print ior(1,0)
print ior(0,1)
print ior(1,1)
Getting as results:
0
1
1
1
Which makes sense, but doesn't coincide with the result obtained in the 3rd position of the lists. I don't understand why the result of the map operation of above is not [11100]
. I am missing something here and I hope that you can throw some light on it.
Upvotes: 0
Views: 796
Reputation: 369364
01100
is octal representation (576 in decimal). 11000
is decimal representation. They are not binary representations.
To represent binary, prefix them with 0b
:
[`01100`, `11000`]
And to get binary representation from the number use bin
:
bin(num)
or
'{:b}'.format(num)
Upvotes: 0
Reputation: 882226
[11000]
(for example) is not a list of five binary digits, it's a list of one decimal number, 11000
.
Similarly, 01100
is a single octal number equal to 576
decimal, so:
11000d = 0010101011111000b
01100o = 576d = 0000001001000000b
-----------------
perform or: 0010101011111000b = 11000d
That's why you're getting 11000
as the answer, exactly the same as if you'd done:
[l_0[i] | l_1[i] for i in range(len(l_0))]
If you want to process a list of five binary digits, that would be something like:
>>> l_0 = [0,1,1,0,0]
>>> l_1 = [1,1,0,0,0]
>>> [l_0[i] | l_1[i] for i in range(len(l_0))]
[1, 1, 1, 0, 0]
Upvotes: 3