首页
Flask
Flask部署
Git
linux
Python
UPS
VPN
Web
Windows
其他
append()只添加最后的文件
删除
×
是否确定删除!
### [Python]list.append()在for循环中每次添加的都是最后的一个元素 首先得知道三点 - 程序的运行是需要去内存中申请地址的。 - 赋值操作只是对于内存中某一块地址的引用。 - Python 内置的 id()函数。 该函数从概念上可以理解为得到当前生命下的内存地址。 id(object) Return the “identity” of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value. CPython implementation detail: This is the address of the object in memory. ### tip: 可以将C语言中的指针带入理解。“.append()”可以理解为添加指针到列表中 。声明一个字典,理解为申请一个地址指针。地址指针不变,那么添加的都是最后一个元素。