“NoneType”类型的对象没有len(),但我确信我的对象是有效的lis

2024-10-01 07:42:08 发布

您现在位置:Python中文网/ 问答频道 /正文

下面的模块总是失败,告诉我“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

错误前的错误/输出:

^{pr2}$

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

我希望我已经为你们准备了足够的装备,让你们看到我的错误!谢谢!在


Tags: 模块the对象in类型forlenif
2条回答

@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),所以我想让你建设性的(希望)反馈回来

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

相关问题 更多 >