Python索引在2D列表中超出范围

时间:2015-02-09 21:20:59

标签: python list indexing range

target_adds = []
tgt_start = 0
tgt_end = 0

for i in range(len(targets)):   

  start = targets[i][0]
  length = targets[i][1]
  start_pkt = math.ceil(start / 512.0)
  len_off = (length * 2) - 4

  samples_left = 512 - (start - (start // 512) * 512)
  len_minus_samples_left = length - samples_left
  len_pkt  = ((len_minus_samples_left / 512) + 1)

  tgt_start = (start * 2)  + (start_pkt * data_start)
  tgt_end = tgt_start + len_off + (len_pkt * data_start)


  target_adds.append([i, tgt_start, tgt_end])

for i in range(len(target_adds)): 
  print "i = %d, SPN: %2d - Target: %d   Start: 0x%04x   End: 0x%04x" % (i, spn, target_adds[i][0], target_adds[i][1], target_adds[i][2])

#tgt_start = target_adds[0][1]

我遇到了Python中的多维列表问题。上面的代码打印出来:

i = 0, SPN:  3 - Target: 0   Start: 0x0220   End: 0x02e4
i = 1, SPN:  3 - Target: 1   Start: 0x03b0   End: 0x05cc

但如果我取消注释最后一行,我会收到索引错误:

Traceback (most recent call last):
File "./run_fpga_test.py", line 259, in <module>
y = data_check(targets, rsg_stims[cmd_idx-1].length, TGT_VAL0, rfp_st.spn)
File "/home/tcg/utils/kh/util_datacap.py", line 155, in data_check
tgt_start = target_adds[0][1]
IndexError: list index out of range

任何人都可以帮我理解为什么吗?我索引列表和上面2行相同的方式,并没有得到错误。我错过了什么?

0 个答案:

没有答案