新的字典變數名稱 = 原字典變數名稱.copy()
註:複製的新字典所佔用的記憶體是另一記憶體位址,與原字典不共用內容,修改新字典內容,原字典內容不會跟著改變
範例:
- # -*- coding: utf-8 -*-
- """
- Created on Tue Jan 29 20:23:04 2019
- @author: 軟體罐頭
- """
- stock1 = {'1101':'台泥','1102':'亞泥','1103':'嘉泥','1104':'環泥','1108':'幸福','1109':'信大','1110':'東泥'}
- stock2 = stock1.copy()
- del stock2['1101']
- print('stock1字典內容:',stock1)
- print('stock2字典內容:',stock2)
- print()
- print('stock1字典元素個數:',len(stock1))
- print('stock2字典元素個數:',len(stock2))
- print()
- print('stock1字典記憶體位址:',id(stock1))
- print('stock2字典記憶體位址:',id(stock2))
執行結果: