在定义一个修改数组元素的函数时,自己的代码修改不成功,不知道时什么原因,请教各位:
给一个数组的每个元素前面加上'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']