Python字典(Dictionary)(Python字典中的值不允许重复吗)
off999 2024-11-02 12:32 41 浏览 0 评论
Python中的Dictionary是数据值的无序集合,用于存储数据值(如映射),与其他仅将单个值作为元素的数据类型不同,Dictionary拥有key:value对。字典中提供了键值以使其更优化。
创建字典
在Python中,可以通过将元素序列放在大括号中(用逗号分隔)来创建字典。Dictionary保存一对值,一个是Key,另一个对应的pair元素是它的Key:value。字典中的值可以是任何数据类型并且可以重复,而键不能重复并且必须是不可变的。
注意:字典键区分大小写,名称相同,但键的大小写会被区别对待。
# Creating a Dictionary
# with Integer Keys
Dict = {1: 'Geeks', 2: 'For', 3: 'Geeks'}
print("\nDictionary with the use of Integer Keys: ")
print(Dict)
# Creating a Dictionary
# with Mixed keys
Dict = {'Name': 'Geeks', 1: [1, 2, 3, 4]}
print("\nDictionary with the use of Mixed Keys: ")
print(Dict) 输出:
Dictionary with the use of Integer Keys:
{1: 'Geeks', 2: 'For', 3: 'Geeks'}
Dictionary with the use of Mixed Keys:
{1: [1, 2, 3, 4], 'Name': 'Geeks'}字典也可以由内置函数dict()创建。只需放置到大括号{}即可创建空字典。
# Creating an empty Dictionary
Dict = {}
print("Empty Dictionary: ")
print(Dict)
# Creating a Dictionary
# with dict() method
Dict = dict({1: 'Geeks', 2: 'For', 3:'Geeks'})
print("\nDictionary with the use of dict(): ")
print(Dict)
# Creating a Dictionary
# with each item as a Pair
Dict = dict([(1, 'Geeks'), (2, 'For')])
print("\nDictionary with each item as a pair: ")
print(Dict) 输出:
Empty Dictionary:
{}
Dictionary with the use of dict():
{1: 'Geeks', 2: 'For', 3: 'Geeks'}
Dictionary with each item as a pair:
{1: 'Geeks', 2: 'For'}嵌套字典:
# Creating a Nested Dictionary
# as shown in the below image
Dict = {1: 'Geeks', 2: 'For',
3:{'A' : 'Welcome', 'B' : 'To', 'C' : 'Geeks'}}
print(Dict) 输出:
{1: 'Geeks', 2: 'For', 3: {'A': 'Welcome', 'B': 'To', 'C': 'Geeks'}}向字典添加元素
在Python字典中,可以通过多种方式添加元素。一次只能添加一个值到字典中,方法是定义值和键,例如Dict[key]=“value”。可以使用内置的update()方法更新字典中的现有值。嵌套的键值也可以添加到现有字典中。
注意:在添加值时,如果键值已经存在,则会更新该值,否则会向字典中添加具有该值的新键。
# Creating an empty Dictionary
Dict = {}
print("Empty Dictionary: ")
print(Dict)
# Adding elements one at a time
Dict[0] = 'Geeks'
Dict[2] = 'For'
Dict[3] = 1
print("\nDictionary after adding 3 elements: ")
print(Dict)
# Adding set of values
# to a single Key
Dict['Value_set'] = 2, 3, 4
print("\nDictionary after adding 3 elements: ")
print(Dict)
# Updating existing Key's Value
Dict[2] = 'Welcome'
print("\nUpdated key value: ")
print(Dict)
# Adding Nested Key value to Dictionary
Dict[5] = {'Nested' :{'1' : 'Life', '2' : 'Geeks'}}
print("\nAdding a Nested Key: ")
print(Dict) 输出:
Empty Dictionary:
{}
Dictionary after adding 3 elements:
{0: 'Geeks', 2: 'For', 3: 1}
Dictionary after adding 3 elements:
{0: 'Geeks', 2: 'For', 3: 1, 'Value_set': (2, 3, 4)}
Updated key value:
{0: 'Geeks', 2: 'Welcome', 3: 1, 'Value_set': (2, 3, 4)}
Adding a Nested Key:
{0: 'Geeks', 2: 'Welcome', 3: 1, 5: {'Nested': {'1': 'Life', '2': 'Geeks'}}, 'Value_set': (2, 3, 4)}从字典访问元素
要访问字典的项,请参阅字典的键名。可以在方括号内使用键。
# Python program to demonstrate
# accessing a element from a Dictionary
# Creating a Dictionary
Dict = {1: 'Geeks', 'name': 'For', 3: 'Geeks'}
# accessing a element using key
print("Accessing a element using key:")
print(Dict['name'])
# accessing a element using key
print("Accessing a element using key:")
print(Dict[1]) 输出:
Accessing a element using key:
For
Accessing a element using key:
Geeks还有一个名为get()的方法也有助于访问字典中的元素。
# Creating a Dictionary
Dict = {1: 'Geeks', 'name': 'For', 3: 'Geeks'}
# accessing a element using get()
# method
print("Accessing a element using get:")
print(Dict.get(3)) 输出:
Accessing a element using get:
Geeks访问嵌套字典的元素
要访问嵌套字典中任何键的值,请使用index[]语法。
# Creating a Dictionary
Dict = {'Dict1': {1: 'Geeks'},
'Dict2': {'Name': 'For'}}
# Accessing element using key
print(Dict['Dict1'])
print(Dict['Dict1'][1])
print(Dict['Dict2']['Name']) 输出:
{1: 'Geeks'}
Geeks
For从字典中删除元素
使用del关键字
在Python字典中,可以使用del关键字来删除键。使用del关键字,可以删除字典和整个字典中的特定值。嵌套字典中的项也可以通过使用del关键字并提供特定的嵌套键和要从该嵌套字典中删除的特定键来删除。
注意:del Dict将删除整个字典,因此在删除后打印它将导致错误。
# Initial Dictionary
Dict = { 5 : 'Welcome', 6 : 'To', 7 : 'Geeks',
'A' : {1 : 'Geeks', 2 : 'For', 3 : 'Geeks'},
'B' : {1 : 'Geeks', 2 : 'Life'}}
print("Initial Dictionary: ")
print(Dict)
# Deleting a Key value
del Dict[6]
print("\nDeleting a specific key: ")
print(Dict)
# Deleting a Key from
# Nested Dictionary
del Dict['A'][2]
print("\nDeleting a key from Nested Dictionary: ")
print(Dict) 输出:
Initial Dictionary:
{'A': {1: 'Geeks', 2: 'For', 3: 'Geeks'}, 'B': {1: 'Geeks', 2: 'Life'}, 5: 'Welcome', 6: 'To', 7: 'Geeks'}
Deleting a specific key:
{'A': {1: 'Geeks', 2: 'For', 3: 'Geeks'}, 'B': {1: 'Geeks', 2: 'Life'}, 5: 'Welcome', 7: 'Geeks'}
Deleting a key from Nested Dictionary:
{'A': {1: 'Geeks', 3: 'Geeks'}, 'B': {1: 'Geeks', 2: 'Life'}, 5: 'Welcome', 7: 'Geeks'}使用pop()方法
Pop()方法用于返回和删除指定键的值。
# Creating a Dictionary
Dict = {1: 'Geeks', 'name': 'For', 3: 'Geeks'}
# Deleting a key
# using pop() method
pop_ele = Dict.pop(1)
print('\nDictionary after deletion: ' + str(Dict))
print('Value associated to poped key is: ' + str(pop_ele)) 输出:
Dictionary after deletion: {3: 'Geeks', 'name': 'For'}
Value associated to poped key is: Geeks使用popitem()方法
popitem()返回并从字典中删除一个任意元素(键,值)对。
# Creating Dictionary
Dict = {1: 'Geeks', 'name': 'For', 3: 'Geeks'}
# Deleting an arbitrary key
# using popitem() function
pop_ele = Dict.popitem()
print("\nDictionary after deletion: " + str(Dict))
print("The arbitrary pair returned is: " + str(pop_ele)) 输出:
Dictionary after deletion: {3: 'Geeks', 'name': 'For'}
The arbitrary pair returned is: (1, 'Geeks')使用clear()方法
使用clear()方法可以一次删除字典中的所有项。
# Creating a Dictionary
Dict = {1: 'Geeks', 'name': 'For', 3: 'Geeks'}
# Deleting entire Dictionary
Dict.clear()
print("\nDeleting Entire Dictionary: ")
print(Dict) 输出:
Deleting Entire Dictionary:
{}词典方法
相关推荐
- 星星动漫网(星辰影院)
-
星蝶公主。《星蝶公主》(英语:Starvs.theForcesofEvil)是迪士尼电视动画制作的美国动画电视喜剧。该系列于2015年1月18日在迪士尼频道首映,并将于2015年3月30日在...
- 股票软件下载大全(股票软件下载大全官网)
-
股票APP要指标齐全,自选股方便,看行情方便,可以用益盟操盘手、国泰君安,看具体指标、筹码分布比较方便,有看主力资金流入情况的指标。也可以用招商证券,筹码分布更清楚,主力流入、封板时间、封涨大减指示比...
-
- 使命召唤2手机版下载(使命召唤2免费下载)
-
步骤/方式1首先进入STEAM主页界面,点击【游戏中心】。步骤/方式2在游戏中心的搜索栏,搜索【使命召唤】。步骤/方式3搜索完毕后,在下方选择【使命召唤19(战区2)】。步骤/方式4在使命召唤19:战区2主页中,点击【下载客户端】即可。...
-
2026-01-18 20:03 off999
-
- 下载电影的软件(下载电影软件排行榜前十名)
-
果断PPTV的VIP破解版本,不要升级,享受vip无限制下载,文件在pptv下download文件夹中,如果出现乱码可以对照电影时间修改名字,另推荐一个电影播放器,mxplayer,手势操作,不再需要点很多次才能把电影拖到想要的位置,请楼主...
-
2026-01-18 19:51 off999
- 最火手游排行榜2025(最火手游排行榜2020歌曲)
-
1、艾尔文:艾尔文是游戏中公认的最强角色,这方面毋庸置疑,强大的属性以及实用性。2.利昂:天赋是每移动一格就可以增加百分5的攻击力,防御力可以提升百分10,攻击之后还有一次全新移动的机会,拥有强大的移...
- 网络电视在线观看高清(网络电视在线电视直播大全)
-
如果网站已建好,可以嵌套一些现成的加密sdk小程序,实现对视频的保护,防止下载、恶意传播、播放等;1做防盗链处理,防止下载;2视频加密sdk,对视频本身进行加密处理,即使被下载,也无法播放!;3...
- cad提供的激活码16组(autocad2014永久激活码16个)
-
1.断开网络,拔除网线或禁用网卡;2.安装时输入序列号“666-69696969”,产品密钥“001H1”;3.安装完毕后启动AutoCAD2017,点击“激活”,然后选择“使用脱机方法申请激活码...
- 163com免费邮箱(163邮箱网页版入口)
-
163邮箱官网首页入口为http://mail.163.com/网易163免费邮箱--中文邮箱第一品牌.容量自动翻倍,支持50兆附件,免费开通手机号码邮箱赠送3G超大附件服务.支持各种客户端软件收发,...
- 三国老款经典单机游戏(老版的三国单机游戏)
-
《三国战记》;《三国群英传》;《三国志》;《三国杀》。游戏介绍:《三国战记》:《三国战纪:风云再起》游戏背景为东汉末年,异象四起:连年天灾、作物欠收、民不聊生,连带影响税收。以张角为首的黄...
- 视频转换器哪个好(视频转换器排行榜)
-
建议使用狸窝转换器,它功能齐全,界面简洁,体积小,速度快。嗨格式视频转换器是一款非常实用的视频文件转换工具,它可以将各种视频格式之间进行转换,例如将MP4、AVI、MOV等视频格式转换成其他常见的视频...
-
- 自动算税软件(自动算税软件怎么用)
-
1、首先,打开手机,找到appstore,在appstore内输入个人所得税。点击获取之后会在页面底部出现如下弹框,点击安装。2、之后会出现如下弹框,显示获取个人所得税app需要进行一个简短的验证才可以,点击继续按钮。在输入框内输入上面...
-
2026-01-18 18:03 off999
- 农场类模拟经营游戏(一款很老的农场游戏)
-
个人觉得《真实模拟农场3D》好玩!这是一款以经营农场为主题的模拟类游戏,你会马上成为一个农场主人。负责一块开阔农场的日常运营,把自己的农场运作的蒸蒸日上,成为最富有的农场主。玩家可以驾驶拖拉机,收割机...
-
- 做图片的软件(做图片的软件app)
-
有手机版的p图大神可以制作好玩的图片此软件专门进行图片恶搞的,手机用美图秀秀,电脑上用ps推荐7个冷门APP吧,以上APP都是朋友推荐或自己无意间发现的,如有雷同,纯属意外。1.马卡龙玩图:马卡龙玩图是一款非常有趣的修图APP,强大的抠图功...
-
2026-01-18 17:15 off999
-
- 德国vs日本视频直播(德国vs日本视频直播回放)
-
世界杯直播德国与日本的比赛是在北京时间的11月23日21点这个时间段举行,这场比赛在卡塔尔世时间则是为16:00点。历史上德国和日本曾经有过2次交手,在2004年12月(日本0-3德国)和2006年5月(德国2-2日本)两队分别进行过2场友...
-
2026-01-18 17:03 off999
欢迎 你 发表评论:
- 一周热门
-
-
抖音上好看的小姐姐,Python给你都下载了
-
全网最简单易懂!495页Python漫画教程,高清PDF版免费下载
-
飞牛NAS部署TVGate Docker项目,实现内网一键转发、代理、jx
-
Python 3.14 的 UUIDv6/v7/v8 上新,别再用 uuid4 () 啦!
-
python入门到脱坑 输入与输出—str()函数
-
Python三目运算基础与进阶_python三目运算符判断三个变量
-
(新版)Python 分布式爬虫与 JS 逆向进阶实战吾爱分享
-
失业程序员复习python笔记——条件与循环
-
系统u盘安装(win11系统u盘安装)
-
Python 批量卸载关联包 pip-autoremove
-
- 最近发表
- 标签列表
-
- python计时 (73)
- python安装路径 (56)
- python类型转换 (93)
- python进度条 (67)
- python吧 (67)
- python的for循环 (65)
- python格式化字符串 (61)
- python静态方法 (57)
- python列表切片 (59)
- python面向对象编程 (60)
- python 代码加密 (65)
- python串口编程 (77)
- python封装 (57)
- python写入txt (66)
- python读取文件夹下所有文件 (59)
- python操作mysql数据库 (66)
- python获取列表的长度 (64)
- python接口 (63)
- python调用函数 (57)
- python多态 (60)
- python匿名函数 (59)
- python打印九九乘法表 (65)
- python赋值 (62)
- python异常 (69)
- python元祖 (57)
