简体   繁体   English

追加函数在 for 循环 Python 3 中不起作用

[英]append function not working in for loop Python 3

I have a for loop running that creates a new list of select Unix times from elements of another list containing multiple Unix times, the index of those elements is in turn given by another list.我有一个 for 循环运行,它从包含多个 Unix 时间的另一个列表的元素创建一个新的选择 Unix 时间列表,这些元素的索引依次由另一个列表给出。 My problem is that within this for loop the append function is not working and I have no idea why as I get no errors.我的问题是在这个 for 循环中, append 函数不起作用,我不知道为什么,因为我没有收到错误。 The print function is simply ignored after the for loop.在 for 循环之后,print 函数被简单地忽略了。 I'm not sure what I'm doing wrong.我不确定我做错了什么。 Could someone help me out?有人可以帮我吗?

Here is my code:这是我的代码:

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)

When I run this, print is ignored and the rest of the code executes as if the statement was not there.当我运行它时,print 被忽略,其余的代码就像语句不存在一样执行。 I don't understand why it is not appending or printing.我不明白为什么它不附加或打印。

Thank you for your time.感谢您的时间。

When I run this code I get an IndexError at the line当我运行此代码时,我在该行收到一个 IndexError

ppst.append(processed_start_times[int(adjusted_exc_pass_numbers[element])])

The reason for this is because as you are iterating, element will be 0,1,2,3..., adjusted_exc_pass_numbers[element] will be 0,6,9,16,..., and eventually you will be trying to get index 48 from processed_start_times , a list which only has 44 entries.这样做的原因是因为在您迭代时,元素将是 0,1,2,3..., adjusted_exc_pass_numbers[element]将是 0,6,9,16,...,最终您将尝试从processed_start_times获取索引 48,一个只有 44 个条目的列表。 Not sure why you are completing the loop without any errors but this is the problem as far as I can tell.不知道为什么你在没有任何错误的情况下完成循环,但据我所知,这是问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM