2012-07-10 97 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

相关问题