2014-07-24 141 views
-2

即使我获得有效的打印,但仍然得到List Index超出范围。在那里我得到 “超出范围” 列表中的错误是 “lstSHADOW_LOG_TABLE”列表索引超出范围:PYTHON

if((int(len(lstSHADOW_LOG_TABLE[index_shadow_log][1]))) > 1): 
    print("Length={0} and Value={1}".format(len(lstSHADOW_LOG_TABLE[index_shadow_log][1]), lstSHADOW_LOG_TABLE[index_shadow_log][1])) 
    for l_inner_element in (lstSHADOW_LOG_TABLE[index_shadow_log:][1]): 
     if(lstSHADOW_LOG_TABLE[index_shadow_log][1] == lstCAN_LOG_TABLE[index_can_log][1]): 
      #Some Calculation 
     else: 
      break 

OUTPUT:

Length=3 and Value=340 
Traceback (most recent call last): 
    for l_inner_element in (lstSHADOW_LOG_TABLE[index_shadow_log:][1]): 
IndexError: list index out of range 

下编辑HERE(CODE修改以合并建议): 列表“lstSHADOW_LOG_TABLE”是列表的列表。现在,让我们说我想的比较开始从指数“index_shadow_log”鲜(SUBLIST“index_shadow_log”起)

为l_inner_element在(lstSHADOW_LOG_TABLE [index_shadow_log:]):

谢谢您的回答,我现在明白是的for循环的含义将启动列表“lstSHADOW_LOG_TABLE”迭代从指数开始“index_shadow_log:”

这是我的抽出代码:

for index in range(len(lstCAN_LOG_TABLE)): 
    for l_index in range(len(lstSHADOW_LOG_TABLE)): 
     #print(lstSHADOW_LOG_TABLE[l_index][0]) 
     #print(lstSHADOW_LOG_TABLE[l_index][1]) 
     if(lstSHADOW_LOG_TABLE[l_index][1] == lstCAN_LOG_TABLE[index][1]): #Consider for comparison only CAN IDs 
      print("matching") 
      #print(lstCAN_LOG_TABLE[index][0]) 
      #print(lstSHADOW_LOG_TABLE[l_index][0]) 
      index_can_log = index           #Position where CAN Log is to be compared 
      index_shadow_log = l_index          #Position from where CAN Shadow Log is to be considered 
      print("Length={0} and Value={1}".format(len(lstSHADOW_LOG_TABLE[index_shadow_log][1]), lstSHADOW_LOG_TABLE[index_shadow_log][1])) 
      bMatchFound = 1 
      for l_inner_element in (lstSHADOW_LOG_TABLE[index_shadow_log:]): #Start comparison 
       if(lstSHADOW_LOG_TABLE[index_shadow_log][1] == lstCAN_LOG_TABLE[index_can_log][1]): #Compare individual element 
        dump_file.write("\n") 
        dump_file.write("SHADOW: " + str(lstSHADOW_LOG_TABLE[index_shadow_log]))    #Dump if equal 
        writer_two.writerow(lstSHADOW_LOG_TABLE[index_shadow_log][0])        #Update CSV File     
        dump_file.write("\n") 
        dump_file.write("CAN: " + str(lstCAN_LOG_TABLE[index_can_log]))      #Dump if equal 
        writer_one.writerow(lstCAN_LOG_TABLE[index_can_log][0])        #Update CSV File     
        if(index_can_log < (input_file_one_row_count - 1)):         #Update CAN LOG Index 
         index_can_log = index_can_log + 1 
        if(index_can_log >= (input_file_one_row_count - 1)): 
         break 
       else: 
        bMatchFound = 0 
        break 
      if(bMatchFound == 0): 
       break 
dump_file.close() 

我需要摆脱括号(SOR ry来自C/C++背景,我们喜欢使用大括号和括号:-P),并使代码更加清晰。感谢所有为您的建议

+2

也许不相关的问题,但:'如果((INT(len' ......最难得的是不是len'的'结果已经是一个。 ?诠释 – Kevin

+2

'lstSHADOW_LOG_TABLE [index_shadow_log]'*不是一回事*为'lstSHADOW_LOG_TABLE [index_shadow_log:]' –

+0

圣不必要的括号,蝙蝠侠 –

回答

2

比较:

lstSHADOW_LOG_TABLE[index_shadow_log][1] 

lstSHADOW_LOG_TABLE[index_shadow_log:][1] 

第一指标lstSHADOW_LOG_TABLE,然后索引不管它返回。第二张切片lstSHADOW_LOG_TABLE;这会返回一个新的列表。然后,您将该切片列表编入索引。如果该分片列表只有1个元素,则索引第二个元素将失败。

你真的需要削减这里的括号,并简化代码。使用一个临时变量来存储索引的元素:

value = lstSHADOW_LOG_TABLE[index_shadow_log][1] 
if value: 
    print("Length={0} and Value={1}".format(len(value), value)) 
    for l_inner_element in value: 
     if value == lstCAN_LOG_TABLE[index_can_log][1]: 
      #Some Calculation 
     else: 
      break 
+0

感谢Martijn。我可以区分切片和索引,并修改了代码:)。现在它工作正常。 P.S:我的代码仍然很难看:-P –