In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article is about what a dictionary means in Python. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.
1 What is a dictionary?
Dictionary is the only mapping type in Python. The mapping type object (Key) and the only desired object (Value) are one-to-many relationships, often thought of as mutable hash tables. A dictionary object is mutable; it is a container type that can store any number of Python objects, including other container types.
Popular explanation: there are many "small spaces" in a "cabinet," which can be divided into "small spaces for books,""small spaces for clothes,""small spaces for shoes," etc. according to their functions, and these "small spaces" have corresponding books, clothes, shoes, etc. according to their functions, which constitute a dictionary. A picture is worth a thousand words, as follows:
学过C++、Java的小伙伴,你可能会想到Map容器,其实Python中的字典与C++中的map容器很相似,都是键值对的形式存储,然而Python中对字典的操作远比C++中对map的操作要方便的多。
二、字典有什么用
想想一下当你需要快速的获取对应key的value的时候,就可以使用python的字典了。比如:根据上面我举得关于柜子的例子,让你去柜子里面拿一本书'围城',你就可以直接根据关键字(key)'放书的小空间'找到这本书' 围城',你过你没有这个关键字,你可能要去整个柜子里面去找。这样就会相当的麻烦。
再比如一个人是有名字,但是这个人还有其他的属性,例如:年龄,性别等等。这个人就会被封装成一个对象。如果有很多人的时候,我们需要快速的根据一个人的名字获取对应名字的对象,这个时候字典就有用了。如果采用数组,我们需要遍历整个数组,才可以根据名字找到这个人。如果是字典(以名字为key,以人的对象为value),就可以直接根据名字得到这个对象,就不要遍历操作了。
根据上面的两个例子可以看到,使用字典管理数据对象,存取数据对象的时候会很有用。
三、创建和使用字典
1、字典创建
(1)字典由键及其对应的值组成,其中键和值之间由冒号(:)分开,且键和值都用上单引号(')引起来,这种键值对被称为项,第一项和第二项之间由逗号(,)隔开。如下所示:
phonebook={'Jane':'123','Danny':'2321','Ming':'3232'}
(2)字典的创建除了上面一种方式,还可以使用dict函数来创建。例如:
>>> obje=[('name','Danny'),('age','12')]
>>> d=dict(obje)
>>> d
{'name': 'Danny', 'age': '12'}
2、字典操作函数
(1)获取字典键值个数
>>> len(d)
2
(2)d[k]返回与键k相关联的值
>>> d['name']
'Danny'
(3)d[k]=v将值v关联到键k
>>> d['age']=19
>>> d
{'name': 'Danny', 'age': 19}
(4)k in d 检查字典d是否包含键为k的项
>>> 'name' in d
True
(5)del d[k]删除键为k的项
>>> del d['age']
>>> d
{'name': 'Danny'}
(6)方法clear() 删除字典中的所有项
>>> d
{'name': 'Danny'}
>>> d.clear()
>>> d
{}
(7)方法copy() 返回一个新的字典(但是这是浅拷贝,因为值本身是原件,而非副本)
>>> x = {'username': 'admin', 'machines': ['foo', 'bar', 'baz']}
>>> y = x.copy()
>>> y['username'] = 'mlh'
>>> y['machines'].remove('bar')
>>> y
{'username': 'mlh', 'machines': ['foo', 'baz']}
>>> x
{'username': 'admin', 'machines': ['foo', 'baz']}
(8)方法fromkeys 创建一个新的字典,其中包含指定的键,且每个键值对应的是none
>>> x
{'username': 'admin', 'machines': ['foo', 'baz']}
>>> x.fromkeys(['pxj','zt'])
{'pxj': None, 'zt': None}
(9)方法get,访问字典中的键对应的值,如果没有则返回None
>>> x
{'username': 'admin', 'machines': ['foo', 'baz']}
>>> print(x.get('pxj'))
None
(10)方法pop,获取指定键相关联的值,并将该键值对删除
>>> x
{'username': 'admin', 'machines': ['foo', 'baz']}
>>> x.pop('username')
'admin'
>>> x
{'machines': ['foo', 'baz']}
(11)方法popitem,随机的弹出一个键值对,并在字典中删除
>>> y
{'username': 'mlh', 'machines': ['foo', 'baz'], 'hello': 'word', 'hao': 'de'}
>>> y.popitem()
('hao', 'de')
>>> y
{'username': 'mlh', 'machines': ['foo', 'baz'], 'hello': 'word'}
感谢各位的阅读!关于"Python中字典是什么意思"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.