使用for循环追加到列表时获取MemoryError

def show_magicians(magicians_list):
    """Print the name of each magician in a list."""
    for magician in magicians_list:
        print(magician.title())

def make_great(magicians_list):
    """Make each magician great again."""
    for magician in magicians_list:
        magician = magician + " " + "the Great"
        magicians_list.append(magician)

list_of_dudes = ['houdini','david blaine','jebus']

make_great(list_of_dudes)

show_magicians(list_of_dudes)

print(list_of_dudes)

为什么第二个功能不起作用?我正在尝试将帅哥列表中的每个魔术师更改为“ [[Magician] The Great”,但我一直遇到存储错误。有什么建议吗?

谢谢你, 困惑的n00b

wsw123wsw1 回答:使用for循环追加到列表时获取MemoryError

您不应该追加到列表中,而应将每个元素替换为其“伟大”版本。

window.Echo = new Echo({
    broadcaster: 'pusher',key: 'e992c96bf63f43766490',cluster: 'us2',wsHost: window.location.hostname,wsPort: 6001
    // disableStats: true,});

您的版本将添加到要迭代的列表中。结果,它永远不会结束,因为它会继续遍历新元素,并使它们变得更大(def make_great(magicians_list): """Make each magician great again.""" for i,magician in enumerate(magicians_list): magician = magician + " " + "the Great" magicians_list[i] = magician ),然后遍历这些元素(houdini the Great the Great),依此类推,直到用完为止记忆。

本文链接:https://www.f2er.com/3156116.html

大家都在问