Python: Bitwise-like list operations

余生长醉 提交于 2021-02-16 17:00:36

问题


I'm trying to elementwise & and elementwise | 2 lists of 8 lists of 6 binary digits, and it is working very oddly. c1 and c2 start as tuples of length 8 with elements that are tuples of length 6, and res starts out as a list version of c1.

anding:

for x in range(8):
    for y in range(6):
        res[x][y] = (c1[:][x][y])*(c2[:][x][y])

oring:

for x in range(8):
    for y in range(6):
        res[x][y] = int(c1[:][x][y] or c2[:][x][y])

An example:

c1:        ((1, 0, 0, 0, 1, 1), (1, 1, 0, 0, 0, 1), (1, 1, 1, 0, 0, 0), (0, 1, 1, 1, 1, 0), (1, 0, 0, 0, 1, 1), (0, 1, 1, 0, 0, 0), (1, 1, 0, 1, 0, 0), (0, 1, 0, 0, 1, 0))
c2:        ((1, 0, 1, 1, 0, 0), (0, 1, 0, 1, 1, 0), (0, 1, 1, 0, 1, 0), (0, 0, 0, 0, 1, 1), (1, 1, 0, 0, 1, 0), (1, 0, 1, 0, 1, 0), (0, 0, 0, 1, 0, 1), (0, 0, 1, 0, 1, 0))
anding res:[[1, 0, 0, 0, 1, 0], [0, 1, 0, 0, 0, 0], [0, 1, 1, 0, 0, 0], [0, 0, 0, 0, 1, 0], [1, 0, 0, 0, 1, 0], [0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0]]
oring res: [[1, 1, 0, 0, 1, 1], [1, 1, 0, 1, 1, 1], [1, 1, 1, 0, 1, 0], [0, 1, 1, 1, 1, 1], [1, 1, 0, 0, 1, 1], [1, 1, 1, 0, 1, 0], [1, 1, 0, 1, 0, 1], [0, 1, 1, 0, 1, 0]]

Other inputs for c1 and can be messed up in much more than the first sublist.

Edit: This has been resolved. It was most likely alias issues in other parts of the code, and I just ended up using list comprehensions.


回答1:


Why not simply trying something like this using list comprehensions:

c1 = ((1, 0, 0, 0, 1, 1), (1, 1, 0, 0, 0, 1), (1, 1, 1, 0, 0, 0))
c2 = ((1, 0, 1, 1, 0, 0), (0, 1, 0, 1, 1, 0), (0, 1, 1, 0, 1, 0))

print('Bitwise or:  ', [[k | l for k, l in zip(i, j)] for i, j in zip(c1, c2)])
print('Bitwise and: ', [[k & l for k, l in zip(i, j)] for i, j in zip(c1, c2)])



回答2:


You could just use NumPy:

In [7]: import numpy as np
In [8]: c1 = np.array(c1)    
In [9]: c2 = np.array(c2)

In [10]: c1 & c2
In [11]: c1 | c2



回答3:


You can always roll your own class:

class BitList(list):
    def anditems(self,other):
        return [se & so for se,so in zip(self,other)]

    def oritems(self,other):
        return [se | so for se,so in zip(self,other)]    

    def xoritems(self,other):
        return [se ^ so for se,so in zip(self,other)]    

print BitList([1,1,0,0,1,1]).xoritems([1,1,1,1,1,1])
    # [0, 0, 1, 1, 0, 0]
print BitList([1,1,0,0,1,1]).oritems([1,1,1,1,1,1])
    # [1, 1, 1, 1, 1, 1]
print BitList([1,1,0,0,1,1]).anditems([1,1,1,1,1,1]) 
    # [1, 1, 0, 0, 1, 1]

Then just deal with the nested sublists separately:

c1=((1, 0, 0, 0, 1, 1), (1, 1, 0, 0, 0, 1), (1, 1, 1, 0, 0, 0), (0, 1, 1, 1, 1, 0), (1, 0, 0, 0, 1, 1), (0, 1, 1, 0, 0, 0), (1, 1, 0, 1, 0, 0), (0, 1, 0, 0, 1, 0))
c2=((1, 0, 1, 1, 0, 0), (0, 1, 0, 1, 1, 0), (0, 1, 1, 0, 1, 0), (0, 0, 0, 0, 1, 1), (1, 1, 0, 0, 1, 0), (1, 0, 1, 0, 1, 0), (0, 0, 0, 1, 0, 1), (0, 0, 1, 0, 1, 0))


print [BitList(t1).anditems(t2) for t1,t2 in zip(c1,c2)]
print [BitList(t1).oritems(t2) for t1,t2 in zip(c1,c2)]



回答4:


seems OK to me:

>>> c1 =  ((1, 0, 0, 0, 1, 1), (1, 1, 0, 0, 0, 1), (1, 1, 1, 0, 0, 0), (0, 1, 1, 1, 1, 0), (1, 0, 0, 0, 1, 1), (0, 1, 1, 0, 0, 0), (1, 1, 0, 1, 0, 0), (0, 1, 0, 0, 1, 0))
>>> c2 =  ((1, 0, 1, 1, 0, 0), (0, 1, 0, 1, 1, 0), (0, 1, 1, 0, 1, 0), (0, 0, 0, 0, 1, 1), (1, 1, 0, 0, 1, 0), (1, 0, 1, 0, 1, 0), (0, 0, 0, 1, 0, 1), (0, 0, 1, 0, 1, 0))
>>> res = [[None]*6 for _ in range(8)]
>>> for x in range(8):
...     for y in range(6):
...         res[x][y] = c1[x][y] & c2[x][y]
... 
>>> print res
[[1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 1, 1, 0, 0, 0], [0, 0, 0, 0, 1, 0], [1, 0, 0, 0, 1, 0], [0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0]]
>>> for x in range(8):
...     for y in range(6):
...         res[x][y] = c1[x][y] | c2[x][y]
... 
>>> print res
[[1, 0, 1, 1, 1, 1], [1, 1, 0, 1, 1, 1], [1, 1, 1, 0, 1, 0], [0, 1, 1, 1, 1, 1], [1, 1, 0, 0, 1, 1], [1, 1, 1, 0, 1, 0], [1, 1, 0, 1, 0, 1], [0, 1, 1, 0, 1, 0]]
>>> 

I used & and | for the bitwise operators, but it really shouldn't make a difference since you're just using 1s and 0s. I suspect that you initialized res = [[None]*6]*8 (or similar) which is causing some of your sublists to be referencing the same list.



来源:https://stackoverflow.com/questions/16044261/python-bitwise-like-list-operations

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!