python list添加元素_[Python]list.append()在for循环中每次添加的都是最后的一个元素...

首先得知道三点。

1、程序的运行是需要去内存中申请地址的。

2、赋值操作只是对于内存中某一块地址的引用。

3、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.

由此我们可以得到以下结果:

a = 1

b = 1

c = a

d = b

print(id(1)) # value x

print(id(a)) # value x

print(id(b)) # value x

print(id(c)) # value x

print(id(d)) # value x

print(id(1) == id(a) == id(b) == id(c) == id(d)) # True

在此基础上去看 字典/dict :

当声明一个字典 info = {} 的操作时候,该字典就已经在内存中获取了某一块地址。

对该字典进行操作时,如 info['name'] = 'github' 的时候,这个字典依旧是之前所占用的地址。

可通过id 函数跟踪得到以下代码:

info = {}

print(id(info)) # value y

info['name'] = 'github'

print(id(info)) # value y

因此,对于你改进前的代码

pathlist.append(info)添加进去的始终是同一个info,准确的说,始终是同一块地址,而这个info内容在不停的修改。

参考以下代码:

info = {'name': 'github'}

pathlist = [info,]

print(id(info)) # value z

print(id(pathlist[0])) # value z

然后,对于改进后的代码

info = {} 的操作放在了循环内,结果就是每一次循环都申请使用一段新的地址,只不过依旧用info来引用。

可由一下代码对比:

info = {}

print(id(info)) # value m

info = {}

print(id(info)) # value n

两次打印的值是不等的。

另外

第一段代码中的

pathlist.append(info) #将dict添加进list中

这个注释,太 多 余 了。

希望能帮到你。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值