我有程序:
def append(aList):
return aList.append(10)
def main():
mylist = [1, 2, 3]
newlist = append(mylist)
print(newlist,",", mylist)
main()
当我运行此程序时,输出为:
None , [1, 2, 3, 10]
为什么变量newlist中没有存储信息?
答案 0 :(得分:1)
答案 1 :(得分:0)
如上所述append()
返回None
。
append()可以使用 inplace
。
所以这是一个解决方案:
from copy import copy
def append(aList):
aList = copy(aList)
aList.append(10)
return aList
def main():
mylist = [1, 2, 3]
newlist = append(mylist)
print(newlist,",", mylist)
main()
<强>输出:强>
[1, 2, 3, 10] , [1, 2, 3]