1

以下模块对我来说一直失败,告诉我“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() 

换句话说,['Julia', '116', '119', '**']似乎在 if 上失败了len(['Julia', '116', '119', '**'])>3,我不知道为什么。

我希望我已经为你们装备了足够的东西来看到我的错误!谢谢!

4

2 回答 2

10

问题是这样的:dictOfLists[x][y]=dictOfLists[x][y].remove('**')。列表的方法在原地remove删除元素,改变原始列表,并返回无,因此您将列表设置为无。相反,只需执行.dictOfLists[x][y].remove('**')

于 2012-07-10T20:28:43.650 回答
1

@BrenBarn 得到了正确的答案,我知道这应该是评论,而不是答案;但我不能很好地在评论中发布代码。

如果在你的循环中你有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)所以我想让您有建设性(希望)反馈作为回报

于 2012-07-10T20:35:59.287 回答