2014-07-24 187 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 –