当前位置:网站首页>字典创建与复制
字典创建与复制
2022-07-23 05:38:00 【紫青宝剑】
Python 字典练习题
1.字典的创建
1.1 普通创建
d={
'name':'Allen','age':21,'gender':'male'}
print(d)
# {'name': 'Allen', 'age': 21, 'gender': 'male'}
1.2 动态分配键值
# 1.2 动态分配键值
d={
}
d['name']=' 优秀'
print(d)
1.3 字典键值表
# 1.3 字典键值表
d=dict(name='优秀',age=14)
print(d)
# {'name': '优秀', 'age': 14}
1.4 字典元组表
d=dict([('name','优秀'),('age',14),('gender','man')])
print(d)
# {'name': '优秀', 'age': 14, 'gender': 'man'}
1.5 赋予初始值
# 1.5 字典初始值
d=dict.fromkeys(['height','weight'],'初始值')
print(d)
# {'height': '初始值', 'weight': '初始值'}
2.字典中的copy方法
copy 函数的功能:
将当前字典复制成新的一个字典 copy 函数的用法
用法:
dict.copy()->该函数无参数,返回一个一模一样的内存地址不同的字典
dicts = {
'one': 1, 'two': 2, 'three': 3}
tmp = dicts.copy()
tmp['one'] = 'abc'
print(dicts)
print(tmp)
注意:字典数据类型的copy函数,当简单的值替换的时候,原始字典和复制过来的字典之间互不影响,
所以新字典的值的替换对原字典没有影响

边栏推荐
- Activiti工作流使用之项目实例
- Redis源码与设计剖析 -- 7.快速列表
- Redis source code and design analysis -- 6. Compressed list
- mysql invalid conn排查
- “我最想要的六种编程语言!”
- Redis source code and design analysis -- 10. List object
- Notes and Thoughts on the red dust of the sky (IV) invalid mutual value
- Mysql database foundation
- PyTorch(五)——PyTorch进阶训练技巧
- Concepts and differences of bit, bit, byte and word
猜你喜欢

视、音频分开的网站内容如何合并?批量下载代码又该如何编写?

赫克Hurco工控机维修WinMax数控机床控制器维修

Two strategies for building AI products / businesses (by Andrew ng)

pygame实现飞机大战游戏

Meyer burger Siemens industrial computer maintenance and machine tool maintenance

Activiti工作流使用之Activiti-app的安装及流程创建

项目部署(简版)

Basic concepts of software testing
![[visual slam] orb slam: tracking and mapping recognizable features](/img/bb/d6a99bb1bff6bbe1f781e567cc1176.png)
[visual slam] orb slam: tracking and mapping recognizable features

The 12th Blue Bridge Cup embedded design and development project
随机推荐
IO should know and should know
Visual studio 2022 interesting and powerful intelligent auxiliary coding
疫情时期加中年危机——游荡在十字街口的三个月
[Anaconda environmental management and package management]
讲师征集令 | Apache DolphinScheduler Meetup分享嘉宾,期待你的议题和声音!
Notifier Nordic fire engine power supply maintenance and daily maintenance
FFmpeg 音频编码
Redis源码与设计剖析 -- 10.列表对象
面试必备之数据库专题
wps中的word中公式复制完后是图片
Data Lake: introduction to delta Lake
The 12th Blue Bridge Cup embedded design and development project
vim常用命令总结
Analyse du code source et de la conception de redis - - 7. Liste rapide
Data Lake: introduction to Apache iceberg
7. Texture mapping
简述redis特点及其应用场景
部署storageclass踩坑记录
9、光线追踪
Chapter 1 Overview - Section 1 - 1.2 overview of the Internet