Python索引到列表并得到错误我不应该得到

时间:2013-07-10 14:38:59

标签: python

我正在索引到像这样的列表

if first.Barcode == rcv_record[rcv_record_len-1][0]:
    do stuff 
  • first.Barcode是一个字符串
  • rcv_record是一个类似于此[[],[],[]]的列表
  • rcv_record_lenlen(rcv_record)

它工作正常,现在它说unsupported opperand - on int and list但是1和rcv_record_len都是整数。任何人都有任何线索?

编辑我添加到此列表的方式是通过扩展tmp列表然后附加到rcv_record

tmp.extend([first.Cost*first.Qty])
tmp.extend([first.Qty])
tmp.extend([first.Name])
tmp.extend([first.Units_case])
#tmp.extend([(first.Units_case)/(first.Cost)]) << if this is not commented werid stuff happens
import pdb; pdb.set_trace()
rcv_record.append(tmp)           
rcv_record_len = len(rcv_record)
while first != []:
    import pdb; pdb.set_trace()
    if first.Barcode == rcv_record[rcv_record_len-1][0]:

我开始认为有一个错误或什么的。取消注释并注释后,在具有此代码的另一行上弹出相同的错误

if hasattr(first,'Cga_type'):

与ints相同但与

无关

此外,我正在使用pdb并逐步执行代码,并且正在复制并粘贴有问题的部分并且它可以正常工作,但是一旦我在没有调试器的情况下运行它就会中断。

我已经删掉了

tmp.extend([first.Units_case])
tmp.extend([(first.Units_case)/(first.Cost)])

并注释掉了使用或引用它的代码,一切正常。这是为了证明问题不在于我目前所拥有的问题,而在于这个新的片段无论出于何种原因都会破坏一切。添加这些之后,我得到了奇怪的opperand int list错误。

这是野兽,这是我所知道的残骸,但是这个项目只是在我的腿上。

if Type == 'burn rate':
       RcvRecords = []
       RmvRecords = []    
       while broken_item_len > 0:
           if broken_item[broken_item_len-1][0] == 'Chemicals':
              RcvRecords.extend(ChemicalRecord.objects.filter(Date__range=[From,To]).filter(Barcode=Chemicals.objects.get(id=broken_item[broken_item_len-1][1]).Barcode).filter(Action='Receiving').filter(Valid=True))
              RmvRecords.extend(ChemicalRecord.objects.filter(Date__range=[From,To]).filter(Barcode=Chemicals.objects.get(id=broken_item[broken_item_len-1][1]).Barcode).filter(Action='Removing').filter(Valid=True))
           if broken_item[broken_item_len-1][0] == 'Supplies':
              RcvRecords.extend(SupplyRecord.objects.filter(Date__range=[From,To]).filter(Barcode=Supplies.objects.get(id=broken_item[broken_item_len-1][1]).Barcode).filter(Action='Receiving').filter(Valid=True))
              RmvRecords.extend(SupplyRecord.objects.filter(Date__range=[From,To]).filter(Barcode=Supplies.objects.get(id=broken_item[broken_item_len-1][1]).Barcode).filter(Action='Removing').filter(Valid=True))
           if broken_item[broken_item_len-1][0] == 'Gasses':
              RcvRecords.extend(GasRecord.objects.filter(Date__range=[From,To]).filter(Barcode=Gasses.objects.get(id=broken_item[broken_item_len-1][1]).Barcode).filter(Action='Receiving').filter(Cga_type=Gasses.objects.get(id=broken_item[broken_item_len-1][1]).Cga_type).filter(Size=Gasses.objects.get(id=broken_item[broken_item_len-1][1]).Size).filter(Valid=True))
              RmvRecords.extend(GasRecord.objects.filter(Date__range=[From,To]).filter(Barcode=Gasses.objects.get(id=broken_item[broken_item_len-1][1]).Barcode).filter(Action='Removing').filter(Cga_type=Gasses.objects.get(id=broken_item[broken_item_len-1][1]).Cga_type).filter(Size=Gasses.objects.get(id=broken_item[broken_item_len-1][1]).Size).filter(Valid=True))
           broken_item_len = broken_item_len - 1 
       RcvRecords_len = len(RcvRecords)
       RmvRecords_len = len(RmvRecords)
       tmp_RcvRecords = RcvRecords
       tmp_RcvRecords_len = len(tmp_RcvRecords)
       tmp_RmvRecords = RmvRecords
       rcv_record = []
       tmp = []
       debugg = ''
       #import pdb; pdb.set_trace()
       while tmp_RcvRecords_len > 0:
             if tmp_RcvRecords:
                 first = tmp_RcvRecords.pop()
                 tmp_RcvRecords_len = len(tmp_RcvRecords)
                 debugg = 'pop'
                 #import pdb; pdb.set_trace()
             if rcv_record == []:
                 tmp.append(first.Barcode)
                 debugg = 'check barcode'
                 #import pdb; pdb.set_trace()
                 if hasattr(first,'Cga_type'):
                     tmp.extend(first.Cga_type)
                     tmp.extend(first.Size)
                     debugg = 'cga exists'
                     #import pdb; pdb.set_trace()
                 else:
                     tmp.extend(['',''])   
                 tmp.extend([first.Cost*first.Qty])

                 tmp.extend([first.Qty])

                 tmp.extend([first.Name])
                 #tmp.extend([first.Units_case])  << THIS PIECE
                 #tmp.extend([(first.Units_case)/(first.Cost)]) << THIS PIECE
                 import pdb; pdb.set_trace()
                 rcv_record.append(tmp)
                 tmp = []
                 debugg = 'herp'                    
             else:
                  rcv_record_len = len(rcv_record)
                  while first != []:
                      import pdb; pdb.set_trace()
                      if first.Barcode == rcv_record[rcv_record_len-1][0]:
                          import pdb; pdb.set_trace()
                          if hasattr(first,'Cga_type'):
                              if first.Cga_type == rcv_record[rcv_record_len-1][1]:
                                  if first.Size == rcv_record[rcv_record_len-1][2]:
                                      rcv_record[rcv_record_len-1][3] = rcv_record[rcv_record_len-1][3]+(first.Cost*first.Qty)                              
                                      rcv_record[rcv_record_len-1][4] = rcv_record[rcv_record_len-1][4]+first.Qty##edited this
                                      #if rcv_record[rcv_record_len-1][6] <> first.Units_case or rcv_record[rcv_record-1][3] <> first.Cost: << THIS PIECE
                                          #rcv_record[rcv_record_len-1][7] = (rcv_record[rcv_record_len-1][7]+(first.Units_case/first.Cost))/2.0##edited this<< THIS PIECE
                                      #rcv_record_len = len(rcv_record)
                                      debugg = 'first'
                                      import pdb; pdb.set_trace()
                                      first =[]                                          
                                  else:
                                      tmp = [first.Barcode,first.Cga_type,first.Size,first.Qty*first.Cost,first.Qty,first.Name]#,first.Units_case,(first.Units_case/first.Cost)]<< THIS PIECE AFTER COMMENTS
                                      rcv_record.append(tmp)
                                      #rcv_record_len = len(rcv_record)
                                      debugg = 'second'
                                      import pdb; pdb.set_trace()
                                      first = []                                         
                              else:
                                  import pdb; pdb.set_trace()
                                  tmp = [first.Barcode,first.Cga_type,first.Size,first.Qty*first.Cost,first.Qty,first.Name]#,first.Units_case,(first.Units_case/first.Cost)]      << THIS PIECE AFTER COMMENTS
                                  rcv_record.append(tmp)
                                  #rcv_record_len = len(rcv_record)
                                  first = []
                                  debugg = 'third'
                                  import pdb; pdb.set_trace()

                          else:
                              rcv_record[rcv_record_len-1][3] = rcv_record[rcv_record_len-1][3] + (first.Qty*first.Cost)
                              rcv_record[rcv_record_len-1][4] = rcv_record[rcv_record_len-1][4] + first.Qty ## edited this here
                              #if rcv_record[rcv_record_len-1][6] <> first.Units_case or rcv_record[rcv_record-1][3] <> first.Cost: << THIS PIECE
                                          #rcv_record[rcv_record_len-1][7] = (rcv_record[rcv_record_len-1][7]+(first.Units_case/first.Cost))/2.0##edited this << THIS PIECE
                              debugg = '4th'
                              import pdb; pdb.set_trace()
                              first = []

                      else:
                          if hasattr(first,'Cga_type'):
                              tmp = [first.Barcode,first.Cga_type,first.Size,first.Cost*first.Qty,first.Qty,first.Name]#,first.Units_case,(first.Units_case/first.Cost)] << THIS PIECE
                              rcv_record.append(tmp)
                              #rcv_record_len = len(rcv_record)
                              debugg = '5th'
                              import pdb; pdb.set_trace()
                              first = []
                          else:
                              tmp =[first.Barcode,'','',first.Cost*first.Qty,first.Qty,first.Name]#,first.Units_case,(first.Units_case/first.Cost)] << THIS PIECE
                              rcv_record.append(tmp)
                              #rcv_record_len=len(rcv_record)
                              debugg = '6th'
                              import pdb; pdb.set_trace()
                              first = []
                      rcv_record_len = rcv_record_len - 1

如果您为此执行ctrl -f,您会发现所有产生问题的片段

2 个答案:

答案 0 :(得分:2)

嗯,由于您没有发布相关的代码,因此很难调试,但是从错误消息我可以告诉您出于某种原因rcv_record_len是一个列表。发布更多代码,我可以给出更详细的答案。甚至可以解决你的问题。

一般情况下,如果您想弄清楚对象的类型,以便将来可以避免这样的问题,可以这样调用:

type(rcv_record_len)
>>> <type 'list'>

或者如果你想要更专业一点,你应该致电isinstance

isinstance(rcv_record_len, list)
>>> True

答案 1 :(得分:1)

问题是你在标记为问题的每一行都有拼写错误:

if rcv_record[rcv_record_len-1][6] <> first.Units_case or rcv_record[rcv_record-1][3] <> first.Cost: << THIS PIECE

请注意那里的最终索引是rcv_record - 1,其中rcv_record是一个列表。