即使我获得有效的打印,但仍然得到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),并使代码更加清晰。感谢所有为您的建议
也许不相关的问题,但:'如果((INT(len' ......最难得的是不是len'的'结果已经是一个。 ?诠释 – Kevin
'lstSHADOW_LOG_TABLE [index_shadow_log]'*不是一回事*为'lstSHADOW_LOG_TABLE [index_shadow_log:]' –
圣不必要的括号,蝙蝠侠 –