2012-07-10 23 views
1

下面的模塊對我來說一直失敗,告訴我'NoneType'類型的對象沒有len() ,但似乎傳遞的對象是一個列表,而不是'NoneType'類型的對象。我在下面包含模塊和輸出。錯誤前'NoneType'類型的對象沒有len(),但我很確定我的對象是一個有效的列表

def Purge_Polyploid_MisScores(dictOfLists): 
    #print "dict getting passed to Purge_Polyploid_MisScores function", dictOfLists 
    for x in dictOfLists.keys(): 
    for y in range (0, len(dictOfLists[x])): 
     print "x", x, " and y", y 
     print dictOfLists[x][y] 
     #if not dictOfLists[x][y]: 
     #print "error at ",x,dictOfLists[str(int(x)-1)][0] 
     if len(dictOfLists[x][y])>3: 
     try: 
      dictOfLists[x][y]=dictOfLists[x][y].remove('**') 
     except: 
      for z in dictOfLists[x][y]: 
      if dictOfLists[x][y].count(z)>2: 
       print "removed ",z," at dictOfLists[",x,"][",y,"]", dictOfLists[x][y] 
       dictOfLists[x][y].remove(z) 
       #I think this produces an error: dictOfLists[x][y]=dictOfLists[x][y].remove(z) 
       print "now, it looks like", dictOfLists[x][y] 
     if len(dictOfLists[x][y])>3: 
      print "The Length is still greater than 3! at dictOfLists[",x,"][",y,"]", dictOfLists[x][y] 

      #print "the reason you have a polyploid is not a mis-score" 
      #print "dictOfLists[",x,"][",y,"]",dictOfLists[x][y] 
     print "Reached the end of the loop" 
    return dictOfLists 

錯誤/輸出:

x 449 and y 100 
['Yellow submarine', '273', '273'] 
Reached the end of the loop 
x 449 and y 101 
['Heartland', '250', '250', '250'] 
removed 250 at dictOfLists[ 449 ][ 101 ] ['Heartland', '250', '250', '250'] 
now, it looks like ['Heartland', '250', '250'] 
Reached the end of the loop 
x 449 and y 102 
['Julia', '116', '119', '**'] 
Traceback (most recent call last): 
    File "fast_run.py", line 11, in <module> 
    sample_names_list_e1_keys_as_numbers_e2=transpose.combine_allele_report_pipeline_dict(pipeline_directory, keeplist_address, rejected_samples_address) 
    File "/Users/markfisher/transpose.py", line 887, in combine_allele_report_pipeline_dict 
    samples=Purge_Polyploid_MisScores(samples) 
    File "/Users/markfisher/transpose.py", line 1332, in Purge_Polyploid_MisScores 
    if len(dictOfLists[x][y])>3: 
TypeError: object of type 'NoneType' has no len() 

換句話說,['Julia', '116', '119', '**']似乎在是否len(['Julia', '116', '119', '**'])>3要失敗的,我不知道爲什麼。

我希望我已經配備足夠的人看到我的錯誤!謝謝!

+0

'None'是'NoneType'唯一的居民。不要與此爭論,而應該看到爲什麼它的值是'None'而不是預期的:) – 2012-07-10 20:30:13

+0

使用裸「except:」是一個壞習慣:它吞下許多你希望看到的異常。 (例如,'NameError',如果您錯誤鍵入了'dictOfLists'。) – DSM 2012-07-10 20:32:03

+0

DSM,除了以下內容,您意味着什麼? – Atticus29 2012-07-10 23:47:46

回答

10

問題是這樣的:dictOfLists[x][y]=dictOfLists[x][y].remove('**')。列表的remove方法刪除元素就地,突變原始列表並返回無,因此您將列表設置爲無。相反,只要做dictOfLists[x][y].remove('**')

+0

就是這樣!謝謝,BrenBarn! – Atticus29 2012-07-10 23:14:39

1

@BrenBarn得到了正確的答案,我知道這應該是一個評論,而不是一個答案;但我無法在評論中很好地發佈代碼。

如果在你的循環中你有像9次那樣的dictOfLists[x][y],那麼結構上就是錯誤的。

  • 使用items()得到鍵和值,而不僅僅是鍵,然後查找值
  • 使用enumerate以獲取列表索引和價值,而不是遍歷range(len(

更多的東西如:

def Purge_Polyploid_MisScores(dictOfLists): 
    for key,lst in dictOfLists.items(): 
     for i,val in enumerate(lst): 
       print "key: %s index: %i val: %s"%(key,i,val) 
       if len(val)>3: 
        val.remove('**') 

對不起,如果重寫冒犯了,但你把思想放在發佈測試代碼(+1)如此我想讓你有建設性(希望)反饋

+0

謝謝,菲爾!有很多有用的東西來清理我的代碼!非常感激! – Atticus29 2012-07-10 23:14:24

+0

嘿,菲爾!如何查找%符號在您的行中的含義,「打印」鍵:%s索引:%i val:%s「%(key,i,val)」?我甚至不知道這是叫什麼,但我已經看到它足以知道我現在想要了解它。謝謝! – Atticus29 2012-07-10 23:53:59

+0

%是舊式字符串格式(請參閱http://docs.python.org/library/stdtypes.html#string-formatting)。在現代Python版本中,您應該使用字符串的「格式」方法(請參閱http://docs.python.org/library/stdtypes.html#str.format)。 – BrenBarn 2012-07-11 02:23:16

相關問題