追加函数不适用于for循环Python 3

时间:2018-02-21 21:55:26

标签: python python-3.x list for-loop append

我有一个for循环运行,它从包含多个Unix时间的另一个列表的元素创建一个新的选择Unix时间列表,这些元素的索引又由另一个列表给出。我的问题是在这个for循环中,append函数不起作用,我不知道为什么我没有错误。在for循环之后,简单地忽略print函数。我不确定我做错了什么。有人可以帮助我吗?

这是我的代码:

adjusted_exc_pass_numbers = [0, 6, 9, 16, 19, 22, 25, 32, 35, 41, 48]
processed_start_times = [1519275660, 1519287600, 1519325040, 1519336920, 1519360080, 1519365900, 1519371900, 1519409400, 1519415340, 1519421280, 1519450260, 1519456200, 1519499700, 1519534680, 1519540560, 1519546620, 1519584060, 1519596000, 1519619160, 1519624920, 1519630920, 1519668420, 1519674360, 1519680360, 1519709340, 1519715220, 1519758720, 1519793760, 1519799580, 1519805700, 1519843080, 1519855020, 1519878180, 1519884000, 1519890000, 1519927500, 1519939380, 1519968360, 1519974300, 1520017800, 1520052780, 1520058660, 1520064720, 1520102160]
ppst = []
for element in range(len(adjusted_exc_pass_numbers)):
    ppst.append(processed_start_times[int(adjusted_exc_pass_numbers[element])])
print(ppst)

当我运行它时,将忽略print,并执行其余代码,就好像语句不在那里一样。我不明白为什么它没有附加或打印。

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

当我运行此代码时,我在

行获得了一个IndexError
ppst.append(processed_start_times[int(adjusted_exc_pass_numbers[element])])

原因是因为在迭代时,元素将是0,1,2,3 ......,adjusted_exc_pass_numbers[element]将是0,6,9,16,...,最后你将尝试从processed_start_times获取索引48,该列表仅包含44个条目。不知道为什么你没有任何错误地完成循环,但据我所知这是问题所在。