-1

我正在索引这样的列表

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'):

但这与整数相同的错误无关

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

我已经编辑了

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

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

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

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 ,您将找到所有产生问题的片段

4

2 回答 2

2

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

一般来说,如果你想弄清楚一个对象是什么类型,以便将来避免类似的问题,你可以这样调用:

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

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

isinstance(rcv_record_len, list)
>>> True
于 2013-07-10T14:42:24.903 回答
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是列表。

于 2013-07-11T12:20:29.200 回答