Skip to main content

Python 字典的复制--copy函数

Published 1 min readMarkdown
Python 字典的复制--copy函数

字典的复制--copy函数

功能

  • 将当前字典复制一个新的字典

用法

  • dict.copy() -> 该函数无参数, 返回一个一模一样的内存地址不同的字典
    • != 可以判断两个成员是否相同

代码

# coding:utf-8

fruits = {
    'apple': 30,
    'banana': 50,
    'pear': 100
}

real_fruits = fruits.copy()
fruits['apple'] = 60
print(real_fruits)

real_fruits['orange'] = 50
real_fruits.update({'cherry': 100})

print(real_fruits)
# print(fruits)
real_fruits['apple'] = real_fruits['apple'] - 5
print(real_fruits)
real_fruits['pear'] -= 3
print(real_fruits)

real_fruits.clear()
print(real_fruits)
print('第二天......')
real_fruits = fruits.copy()
print(real_fruits)

6 views

No comments yet. Be the first to comment.

More from this blog