gpt4 book ai didi

Python:字典链接数据

转载 作者:行者123 更新时间:2023-11-28 20:10:31 25 4
gpt4 key购买 nike

d1 = {"dog":"woof", "cat":"meow"}
d2 = d1
d2["dog"] = "bark"
for i in d1:
print(i, d1[i])

dog bark
cat meow

这样做的最佳方式是什么:

dog woof
cat meow

最佳答案

d1 = {"dog":"woof", "cat":"meow"}
d2 = d1.copy() # make a copy, not a reference to the same dictionary
d2["dog"] = "bark"
for i in d1:
print(i, d1[i])

# dog woof
# cat meow

关于Python:字典链接数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7052609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com