python遍历数组元素进行修改的方法

在定义一个修改数组元素的函数时,自己的代码修改不成功,不知道时什么原因,请教各位:
给一个数组的每个元素前面加上'The Great '几个字符

整体代码如下:
def make_great(great_magicians):
for great_magician in great_magicians:
great_magician = 'The Great '+great_magician
print(great_magician)

magicians_list = ['a', 'b', 'c']
make_great(magicians_list)
print(magicians_list)

未提示报错,但是结果跟预期不一样 ,执行结果如下:
The Great a
The Great b
The Great c
['a', 'b', 'c']

我觉得应该是函数修改的数组没有保存在数组中? 总是感觉对数组哪里没理解

我参考了其他的代码:
def make_great(great_magicians):
for i in range(len(great_magicians)):
great_magicians[i] = 'The Great ' + great_magicians[i]
print(great_magicians[i])

magicians_list = ['a', 'b', 'c']
make_great(magicians_list)
print(magicians_list)

这样输出的结果是符合预期的,但是不知道自己的问题在哪里:
The Great a
The Great b
The Great c
['The Great a', 'The Great b', 'The Great c']

因为不指定下标的话,每次修改的没有实际生效
你写的只是单纯取值,great_magicians[i] 才能对应到实际列表

传入的对象如果不返回,则需要再对象上面修改才能生效
参考如下
有帮助请点击一下采纳,谢谢

def make_great(great_magicians):
    for index in range(len(great_magicians)):
        great_magicians[index] = 'The Great '+great_magicians[index]
    print(great_magicians)

magicians_list = ['a', 'b', 'c']
make_great(magicians_list)
print(magicians_list)

img

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632