2013-04-16 67 views
1

我試圖元素化&和elementwise | 2個8個列表,包含6個二進制數字,並且工作非常奇怪。 c1和c2以長度爲8的元組開始,長度爲6的元素的元素,res開始爲c1的列表版本。Python:逐位列表操作

安定:

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

O形環:

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

一個例子:

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]] 

爲C1,並且可以在比第一子列表更會混亂的其它輸入。

編輯:這已解決。這很可能是代碼其他部分的別名問題,而我最終只使用了列表解析。

+2

在Python中通過索引循環通常是一個非常糟糕的主意。改用[列表解析](http://www.youtube.com/watch?v=pShL9DCSIUw)來實現這種功能。 –

+0

你在哪裏「初始化」資源? – mgilson

+0

使用'&'進行ANDing和使用'|'進行ORing。 –

回答

8

爲什麼不只是想這樣的應用列表解析:

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)]) 
+4

因爲我偶爾編寫代碼作爲愛好,而我不知道它們。 – IronBeard

1

似乎確定以我:

>>> 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]] 
>>> 

我用位運算符&|,但因爲你僅僅使用1和0,真的不應該有所作爲。我懷疑你初始化了res = [[None]*6]*8(或類似的),這導致你的一些子列表引用同一個列表。

+1

你做了需要思考的部分:) – unutbu

+0

我不知道它需要太多的思考......大多隻是複製粘貼像一個訓練有素的猿。 – mgilson

+0

這也是我的想法,但我在任何地方插入了[:],包括我知道它是superfluosu的地方,但它仍然無效。 – IronBeard

17

你可以只使用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 
+1

+1。接下來我會提出建議。 – mgilson

+0

如果我有[c1,c2,c3,... c10],我想要c1&c2&...&c10,該怎麼辦?有沒有捷徑可以做到這一點? – flutefreak7

+0

'np.logical_and.reduce([c1,c2,...,c10])' – unutbu

0

你總是可以推出自己的類:

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] 

然後,分別處理嵌套的子列表:

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)]