返回信息流contact = {}
contact_list = []
while 1:
contact['name'] = raw_input("please input name: ")
contact['phone'] = raw_input("please input phone number: ")
contact_list.append(contact.copy())
go_on = raw_input("continue?\n")
if go_on == "yes":
pass
elif go_on == "no":
break
else:
print "you didn't say no\n"
i = 1
for contact in contact_list:
print "%d: name=%s" % (i, contact['name'])
print "%d: phone=%s" % (i, contact['phone'])
i = i + 1
--------------------------------------------------------------
其中copy()的作用是what,有copy()和没有copy()结果不同?
[ema13]
这是一条镜像帖。来源:北邮人论坛 / cpp / #74632同步于 2013/10/18
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
【Python】初学者求问copy函数
hanhandou
2013/10/18镜像同步7 回复
订阅后,新回复会通过你的通知中心匿名送达。
7 条回复
a = {1 : 1, 2 : 2}
b = []
b.append(a)
b[0][1] = 3
print a[1]
a = {1 : 1, 2 : 2}
b = []
b.append(a.copy())
b[0][1] = 3
print a[1]
#Run it and you can see the difference
【 在 Milrivel 的大作中提到: 】
: a = {1 : 1, 2 : 2}
: b = []
: b.append(a)
: ...................
Thanks,I know there has been some difference. I wanna why?
【 在 Milrivel 的大作中提到: 】
: a = {1 : 1, 2 : 2}
: b = []
: b.append(a)
: ...................
soga,貌似懂了,多谢
If you don't use copy, the thing that b will append is just a pointer
【 在 hanhandou 的大作中提到: 】
:
: soga,貌似懂了,多谢
【 在 Milrivel 的大作中提到: 】
: If you don't use copy, the thing that b will append is just a pointer
好不容易遇到个可以回答的问题,结果被你抢先了!