簡體   English   中英

while循環崩潰中的多個python附加函數

[英]Multiple python append functions in while loop crashing

嘗試通過按順序附加三個較小列表中的項目來打印復合列表:

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   

如果我注釋掉第二個 while 循環,列表的第一個元素會按預期打印在列表中,但引入第二個 while 循環會導致 MemoryError。

無需在 2 個不同的 while 循環中附加這三個項目。 如果您使用 for 循環,它也會更簡單。 在這種情況下:

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]) 

編輯:另外,我剛剛注意到錯誤,您使用step =+ 1 ,這與說step = +1step = 1 這就是為什么您會收到內存錯誤,您一直將 step 定義為 1,即介於 0 和 50 之間,因此 while 循環繼續運行。 你可能想要寫的是step += 1 ,這一步增加 1 並且沒有將它設置為 1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM