2014-10-09 113 views
1

現在我試圖創建一個小函數,它需要兩個參數,一個列表和限制,然後在所述列表中更改這些數字。 2D列表應該只返回1和0。如果元素大於或等於限制它的元素變爲1,如果它小於極限就變成了0。到目前爲止,這是我想出:如何在python中追加列表到列表中?

def tempLocations(heatMat,tempMat): 
newMat=[]   
for i in heatMat: 
    for j in i: #j goes into the list within the list 
     if j >= tempMat: #if the element of j is greater than or equal to the limit of the matrix 
      j = 1 #it will turn the element into a 1 
      newMat.append(j) 
     else: 
      if j < tempMat: 
       j = 0 
       newMat.append(j)   
print newMat 


tempLocations([[12,45,33,22,34],[10,25,45,33,60]],30) 

這做我想要的大部分,除了它創建一個單一的列表,它放置所有的1和0。我試圖讓它保持二維列表樣式,同時仍然改變列表中的值,以便我最終得到的不是[0,1,1,0,1,0,0,1,1, 1]而是[[0,1,1,0,1],[0,0,1,1,1]]。我會如何去做這件事?任何幫助表示讚賞:)

+0

作爲一個方面說明,無論你的答案選擇哪種解決方案,它不是重新定義一個好主意循環變量,在這種情況下爲'j'。你可以使用一個全新的變量來存儲0或1,然後再添加它,這樣當人們閱讀代碼時,'j'只是存儲從數據列表中讀取的實際數據值很簡單。 – ely 2014-10-09 23:23:56

回答

1
list1 = [0,0,0,1,1] 
list2 = [1,1,1,0,0] 

wrap = [] 
wrap.append(list1) 
wrap.append(list2) 

然後wrap == [[0,0,0,1,1],[1,1,1,0,0]]

像這樣的事情?您隨後可以根據需要更改list1list2,並且它將反映在wrap中。

+0

用於構建列表的列表,wrap.append([]),然後換行[0] .append(0) – user3757614 2014-10-09 23:18:09

+0

我不確定...它看起來像它可以工作。我會嘗試在我的程序中實現它並看看。 – 2014-10-09 23:19:15

2

有一起簡單的方法:

data = [[12,45,33,22,34],[10,25,45,33,60]] 
mask = [[int(x > 30) for x in sub_list] for sub_list in data] 

,如果你想把它當作與閾值作爲參數的函數:

def make_mask(thresh, data): 
    return [[int(x > thresh) for x in sub_list] for sub_list in data] 

make_mask(30, data) 

並不願意投誰較真bool結果爲int(或者可能需要與0和1不同的值),這也易於閱讀:

[[1 if x > 30 else 0 for x in sub_list] for sub_list in data] 

def make_mask(thresh, data, hi=1, lo=0): 
    return [[hi if x > thresh else lo for x in sub_list] for sub_list in data] 

例如

In [97]: make_mask(30, data, "hot", "cold") 
Out[97]: [['cold', 'hot', 'hot', 'cold', 'hot'], ['cold', 'cold', 'hot', 'hot', 'hot']] 
+0

太棒了!完美的作品:) – 2014-10-09 23:27:24

1

你初始化newMat對空列表,然後添加數字到它,所以最後newMat仍然將是一個列表。一個可能的解決方案是在內循環之前使用一個用空列表初始化的中間列表,在內循環中追加元素(而不是newMat),並在內循環將這個中間列表追加到newMat之後。

1
def tempLocations(heatMat,tempMat): 
    newMat=[]   
    for i in heatMat: 
     newRow=[] 
     for j in i: 
      if j >= tempMat: 
       j = 1 
      else: 
       j = 0 
      newRow.append(j)   
     newMat.append(newRow) 
    print newMat 
0

只是一對夫婦的調整你的原代碼 - 只是內部循環之前,創建一個新的列表,並把所有的1和0的話,那麼當該列表是完成追加到名單。我還加了return語句的功能,新的列表返回到調用它的語句:

def tempLocations(heatMat,tempMat): 
    newMat = [] 
    for i in heatMat: 
     inner_list = list() 
     for j in i: #j goes into the list within the list 
      if j >= tempMat: #if the element of j is greater than or equal to the limit of the matrix 
       j = 1 #it will turn the element into a 1 
      elif j < tempMat: 
       j = 0 
      #append to the inner list 
      inner_list.append(j) 
     # append inner_list to the outer list when done 
     newMat.append(inner_list) 
    #return the new list to the calling statement 
    return newMat 

new = tempLocations([[12,45,33,22,34],[10,25,45,33,60]],30) 
print new 

>>> 
[[0, 1, 1, 0, 1], [0, 0, 1, 1, 1]] 
>>>