簡體   English   中英

類型為“ NoneType”的對象沒有len(),但是我很確定我的對象是有效列表

[英]object of type 'NoneType' has no len(), but I'm pretty sure my object is a valid list

以下模塊對我來說一直失敗,告訴我'NoneType'類型的對象沒有len(),但是似乎傳遞的對象是列表,而不是'NoneType'類型的對象。 我在下面包括模塊和輸出。

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

換句話說,如果len(['Julia', '116', '119', '**'])>3['Julia', '116', '119', '**']似乎失敗了len(['Julia', '116', '119', '**'])>3 ,我不知道為什么。

我希望我已經為你們提供了足夠的裝備,讓我看到我的錯誤! 謝謝!

問題是這樣的: dictOfLists[x][y]=dictOfLists[x][y].remove('**') 列表的remove方法將就地刪除元素,使原始列表發生變異,並返回None,因此您將列表設置為None。 相反,只需執行dictOfLists[x][y].remove('**')

@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),所以我想給您帶來建設性的(希望)回報

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM