0

0

在Python中复制字典的方法

王林

王林

发布时间:2023-09-11 12:21:03

|

1761人浏览过

|

来源于tutorialspoint

转载

在python中复制字典的方法

Dictionary in python is a collection data type that stores information in the form of keys which have their corresponding values. It is unordered in nature and the stored data can be manipulated i.e.; it is changeable. We use dictionary to perform various operations, its application extends in the field of data base management, machine learning and web framework development.

In this article we will perform a basic dictionary-based operation explaining the different ways in which we can copy a dictionary element from an already existing dictionary. Before we dive deep into the topic, let’s quickly go through the overview of this article.

What is a dictionary?

A dictionary in python is a collection data type used to store data. Values are assigned to different keys. Keys are immutable i.e., for every they cannot be changed. Each key can contain different values but a single value cannot be associated with more than one key. For python dictionaries are objects with the data type “dict”

创建一个字典

A dictionary can be created with the help of curly braces. The syntax for this is −

立即学习Python免费学习笔记(深入)”;

dict1 = {"Name": ["ROHIT", "AJAY", "RAGHAV"],"Age": [18, 22, 25]} 

在这里,“Name”是一个具有三个值的键,类似地,“Age”也是一个具有三个值的键。这些值可以是任何数据类型。另一方面,键也可以是不同的数据类型,但条件是它应该是不可变的。例如:字符串、元组、整数

Now that we know the process of dictionary creation and various properties associated with it, we will understand the operation of copying a dictionary.

What does copying a dictionary mean?

When we say we will copy a dictionary it means we will copy the key value pairs from a dictionary source to our local dictionary. There are multiple methods that can be used to complete this operation −

使用copy()方法

This method creates a replica of the original dictionary. One noticeable detail about this method is that when we make changes to the copied dictionary, it does not reflect in the original dictionary but when the original dictionary is altered, we would observe changes in the copied version as well. Let’s see its implementation.

Example

的中文翻译为:

示例

dict1 = {"Name": ["ROHIT", "AJAY", "RAGHAV"],"Age": [18, 22, 25]}
dict2 = dict1.copy()
print (dict2)

Output

{'Name': ['ROHIT', 'AJAY', 'RAGHAV'], 'Age': [18, 22, 25]} 

现在让我们看看在操作复制的字典时会反映出哪些变化 -

示例

dict1 = {"Name": ["ROHIT", "AJAY", "RAGHAV"],"Age": [18, 22, 25]}
dict2 = dict1.copy()
dict2["Name"] = ["ARJUN", "VIJAY", "RAVI"]
print("The source dictionary is", dict1)
print("The copied dictionary is", dict2)

Output

The source dictionary is {'Name': ['ROHIT', 'AJAY', 'RAGHAV'], 'Age': [18, 22, 25]}
The copied dictionary is {'Name': ['ARJUN', 'VIJAY', 'RAVI'], 'Age': [18, 22, 25]}

As we can see, no changes are reflected in the source dictionary because of the shallow copy creation. The copied dictionary is referring to the source dictionary.

crmeb电商系统
crmeb电商系统

CRMEB 是基于Thinkphp5基础开发的以会员为中心的电商系统,开源版微信公众号商城和小程序商城数据同步,带积分、优惠券、秒杀、砍价、分销等功能,更是一套方便二次开发的商城框架(后台封装了独有快速创建表单功能,无需写表单页面、快速创建数据搜索和数据列表页、导出表格、系统权限配置控制每一个控制器方法、系统参数配置、数据字典、组合数据等)

下载

Using dictionary comprehension

这种方法使用字典推导式来迭代并将源字典中的元素添加到新字典中。

We will traverse through the source dictionary and use items() method to add the key value pairs in the new dictionary. Let’s see its implementation −

Example

的中文翻译为:

示例

以下是一个示例。在这里,

  • 我们创建了一个源字典。

  • 我们使用字典推导式遍历源字典,并借助items()方法添加键值对。

  • 我们操纵了复制的字典,并打印了两个版本。

dict1 = {"Name": ["ROHIT", "AJAY", "RAGHAV"],"Age": [18, 22, 25]}
dict2 = {keys: values for keys, values in dict1.items()}
print("The source dictionary is", dict1)
dict2["Age"] = [33, 23, 21]
print("The copied dictionary is", dict2)

Output

The source dictionary is {'Name': ['ROHIT', 'AJAY', 'RAGHAV'], 'Age': [18, 22, 25]}
The copied dictionary is {'Name': ['ROHIT', 'AJAY', 'RAGHAV'], 'Age': [33, 23, 21]}

使用dict()方法

在这个方法中,我们将使用dict()方法创建一个新的字典。在参数中,我们将传递源字典。传递的字典将自动复制。让我们看看它的实现。

Example

的中文翻译为:

示例

以下示例使用dict()方法复制字典的内容。在这里,

  • We changed the value- “ROHIT” of the key- “Name” to “MAHI”.

  • After copying, we printed both the dictionaries.

dict1 = {"Name": ["ROHIT", "AJAY", "RAGHAV"],"Age": [18, 22, 25]}
dict2 = dict(dict1)
print("The source dictionary is", dict1)
dict2["Name"] = ["MAHI", "AJAY", "RAGHAV"]
print("The copied dictionary is", dict2)

Output

The source dictionary is {'Name': ['ROHIT', 'AJAY', 'RAGHAV'], 'Age': [18, 22, 25]}
The copied dictionary is {'Name': ['MAHI', 'AJAY', 'RAGHAV'], 'Age': [18, 22, 25]}

结论

在本文中,我们讨论了从源中复制字典涉及的各种方法。我们了解了浅拷贝的概念,并观察了键值对的行为。

相关文章

python速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
python开发工具
python开发工具

php中文网为大家提供各种python开发工具,好的开发工具,可帮助开发者攻克编程学习中的基础障碍,理解每一行源代码在程序执行时在计算机中的过程。php中文网还为大家带来python相关课程以及相关文章等内容,供大家免费下载使用。

759

2023.06.15

python打包成可执行文件
python打包成可执行文件

本专题为大家带来python打包成可执行文件相关的文章,大家可以免费的下载体验。

639

2023.07.20

python能做什么
python能做什么

python能做的有:可用于开发基于控制台的应用程序、多媒体部分开发、用于开发基于Web的应用程序、使用python处理数据、系统编程等等。本专题为大家提供python相关的各种文章、以及下载和课程。

761

2023.07.25

format在python中的用法
format在python中的用法

Python中的format是一种字符串格式化方法,用于将变量或值插入到字符串中的占位符位置。通过format方法,我们可以动态地构建字符串,使其包含不同值。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

618

2023.07.31

python教程
python教程

Python已成为一门网红语言,即使是在非编程开发者当中,也掀起了一股学习的热潮。本专题为大家带来python教程的相关文章,大家可以免费体验学习。

1265

2023.08.03

python环境变量的配置
python环境变量的配置

Python是一种流行的编程语言,被广泛用于软件开发、数据分析和科学计算等领域。在安装Python之后,我们需要配置环境变量,以便在任何位置都能够访问Python的可执行文件。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

548

2023.08.04

python eval
python eval

eval函数是Python中一个非常强大的函数,它可以将字符串作为Python代码进行执行,实现动态编程的效果。然而,由于其潜在的安全风险和性能问题,需要谨慎使用。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

579

2023.08.04

scratch和python区别
scratch和python区别

scratch和python的区别:1、scratch是一种专为初学者设计的图形化编程语言,python是一种文本编程语言;2、scratch使用的是基于积木的编程语法,python采用更加传统的文本编程语法等等。本专题为大家提供scratch和python相关的文章、下载、课程内容,供大家免费下载体验。

709

2023.08.11

高德地图升级方法汇总
高德地图升级方法汇总

本专题整合了高德地图升级相关教程,阅读专题下面的文章了解更多详细内容。

43

2026.01.16

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
麻省理工大佬Python课程
麻省理工大佬Python课程

共34课时 | 5.1万人学习

【web前端】Node.js快速入门
【web前端】Node.js快速入门

共16课时 | 2万人学习

国外Web开发全栈课程全集
国外Web开发全栈课程全集

共12课时 | 1.0万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号