python - 带列表的 for 循环
问题描述
编码
def f(x, l=[] ):
for i in range(x):
l.append(i*i)
print(l)
f(2)
f(2, [3,2,1])
f(3)
程序结果:
[0]
[0, 1]
[3, 2, 1, 0]
[3, 2, 1, 0, 1]
[0, 1, 0]
[0, 1, 0, 1]
[0, 1, 0, 1, 4]
我不知道为什么 f(3) 打印:
[0, 1, 0]
[0, 1, 0, 1]
[0, 1, 0, 1, 4]
代替
[0]
[0, 1]
[0, 1, 4]
为什么f(2, [3,2,1])改变了f(3)的结果,但f(2)不干扰f(2, [3,2,1])?
解决方案
推荐阅读
- c++ - CPP 数组内存分配
- python - 如何使用 discord.py 机器人在我的服务器上抓取和发送任何人的个人资料图片?
- java - 无法在 Java 中的另一个对象中看到 ArrayList 元素
- android - 什么是相当于Executor服务的Android协程
- c++ - 将文件读入没有字符串头文件的字符串对象
- c - C18 标准中的矛盾(关于字符集)?
- javascript - Heroku 托管数据库
- react-native - 数字字段未在 React 本机短信链接中显示数字
- python - 如何让用户在kivy应用程序中保持登录状态
- python - 在矩阵中生成数独游戏而不是绘制 GUI