简体   繁体   English

while循环崩溃中的多个python附加函数

[英]Multiple python append functions in while loop crashing

Attempting to print a composite list by appending item from three smaller lists in sequence:尝试通过按顺序附加三个较小列表中的项目来打印复合列表:

def final_xyz_lister():
    global final_xyz_list
    final_xyz_list = []
    step=0
    while step==0:
        final_xyz_list.append(carbon_final_list[step]) 
        final_xyz_list.append(oxygen_final_list[step]) 
        final_xyz_list.append(hydrogen_final_list[step]) 
        step=+1
    while 0 < step < 50:   
        final_xyz_list.append(carbon_final_list[step]) 
        final_xyz_list.append(oxygen_final_list[step]) 
        final_xyz_list.append(hydrogen_final_list[step]) 
        step=+1
    else:
        pass   

If I comment out the second while loop the first element of the list is printed in a list as expected but introduction of the second while loop results in a MemoryError.如果我注释掉第二个 while 循环,列表的第一个元素会按预期打印在列表中,但引入第二个 while 循环会导致 MemoryError。

There is no need to append the three items in 2 different while loops.无需在 2 个不同的 while 循环中附加这三个项目。 It would also be simpler if you used for loops.如果您使用 for 循环,它也会更简单。 in this case:在这种情况下:

for step in range(0, 50):
    final_xyz_list.append(carbon_final_list[step]) 
    final_xyz_list.append(oxygen_final_list[step]) 
    final_xyz_list.append(hydrogen_final_list[step]) 

Edit: Also, I just noticed the error, you use step =+ 1 , which is the same as saying step = +1 or step = 1 .编辑:另外,我刚刚注意到错误,您使用step =+ 1 ,这与说step = +1step = 1 This is why you are getting a memory error, you keep defining step as 1, which is between 0 and 50, so the while loop just keeps going.这就是为什么您会收到内存错误,您一直将 step 定义为 1,即介于 0 和 50 之间,因此 while 循环继续运行。 what you probbly wanted to write was step += 1 , this increases step by 1 and doesn't set it to 1你可能想要写的是step += 1 ,这一步增加 1 并且没有将它设置为 1

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

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