Python基本数据类型之字典

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/WandDouDou/article/details/82700210

一、Python字典的定义和特性

1、定义:

(1)、字典是另一种可变容器模型,且可存储任意类型对象。

(2)、字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如下所示:

d = {key1 : value1, key2 : value2 ... }

字典定义示例:

dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}

2、特性:

(1)、键必须是唯一的,不可变的,可以用数字,字符串,布尔值或元组充当,用列表、字典就不行;值可以取任何数据类型

(2)、字典值可以是任何的 python 对象,既可以是标准的对象,也可以是用户定义的,但键不行

(3)、不允许同一个键出现两次。创建时如果同一个键被赋值两次,只会取最后一个的值

(4)、字典是无序的

二、字典的 增、删、改、查 操作

1、增:

2、删:

(1)、单个键删除:del dict[键]    

(2)、清空字典:dict.clear()

(3)、删除字典:del dict

3、改:dict[键] = 新值

4、查:dict[键]来获取对应的值,键必须是存在的,否则会报错

三、字典内置函数&方法

1、len(dict):计算字典元素个数,即键的总数

代码示例:

testdict = {'Name': 'wangdoudou', 'Age': 7, 'Class': 'First'}
print("testdict中的元素个数为:", len(testdict))

输出结果:

2、str(dict):输出字典,以可打印的字符串表示。

代码示例:

testdict = {'Name': 'wangdoudou', 'Age': 7, 'Class': 'First'}
print("testdict转换成字符串后的样式为:", str(testdict))

输出结果:

3、type(variable):返回输入的变量类型,如果变量是字典就返回字典类型。

代码示例:

testdict = {'Name': 'wangdoudou', 'Age': 7, 'Class': 'First'}
print("testdict的类型为:", type(testdict))

输出结果:

四、字典dict类中的方法:

1、copy(self):返回一个字典的浅复制

代码示例:

testdict = {'Name': 'wangdoudou', 'Age': 7, 'Class': 'First'}
new_dict = testdict.copy()
print("新拷贝的字典内容是:", new_dict)

输出结果:

2、fromkeys(*args, **kwargs):创建一个新字典,以序列seq中元素做字典的键,val为字典所有键对应的初始值

代码示例:

new_dict1 = dict.fromkeys(["hehe", "haha", "heihei"], 123)
print("新创建的字典内容为:", new_dict1)

输出结果:

3、get(self, k, d=None):根据指定键获取对应的值,如果键不在字典中,如果指定返回值则返回指定值,如果没有则返回None

代码示例:

testdict = {'Name': 'wangdoudou', 'Age': 7, 'Class': 'First'}
key1 = testdict.get("Name")
print("获取到key1的值为:", key1)
key2 = testdict.get("hahaha")
print("获取到key2的值为:", key2)

输出结果:

代码示例:

testdict = {'Name': 'wangdoudou', 'Age': 7, 'Class': 'First'}
key1 = testdict.get("Name")
print("获取到key1的值为:", key1)
key2 = testdict.get("hahaha", 222)
print("获取到key2的值为:", key2)

输出结果:

4、items(self):以列表返回可遍历的(键, 值) 元组数组

代码示例:

testdict = {'Name': 'wangdoudou', 'Age': 7, 'Class': 'First'}
for itemkey, itemvalue in testdict.items():
    print("testdict的键是:", itemkey, "对应的值为:", itemvalue)

输出结果:

5、keys(self):返回字典中所有的键的一个列表

代码示例:

testdict = {'Name': 'wangdoudou', 'Age': 7, 'Class': 'First'}
for item in testdict.keys():
    print("当前字典的键为:", item)

输出结果:

6、values(self):返回字典中所有的值的一个列表

代码示例:

testdict = {'Name': 'wangdoudou', 'Age': 7, 'Class': 'First'}
for item in testdict.values():
    print("当前字典的值为:", item)

输出结果:

7、pop(self, k, d=None):删除字典给定键 key 所对应的值,返回值为被删除的值。key值必须给出。 否则,返回default值。

代码示例:

testdict = {'Name': 'wangdoudou', 'Age': 7, 'Class': 'First'}
print("testdict当前字典的样式为:", testdict)
tempdel = testdict.pop("Class")
print("删除的值为:", tempdel, "\ntestdict删除键Class后的样式为:", testdict)

输出结果:

8、popitem(self):随机返回并删除字典中的一对键和值(一般删除末尾对)。

代码示例:

testdict = {'Name': 'wangdoudou', 'Age': 7, 'Class': 'First'}
print("testdict当前字典的样式为:", testdict)
tempdel = testdict.popitem()
print("删除的值为:", tempdel, "\ntestdict随机删除后的样式为:", testdict)

输出结果:

9、setdefault(self, k, d=None):和get()类似, 但如果键不存在于字典中,将会添加键并将值设为default

(1)、如果修改的键存在,则返回字典中原本的值

代码示例;

testdict = {'Name': 'wangdoudou', 'Age': 7, 'Class': 'First'}
print("testdict当前字典的样式为:", testdict)
tempvalue = testdict.setdefault("Age", 15)
print("修改后的值tempvalue=", tempvalue, "\ntestdict修改后的样式为:", testdict)

输出结果;

(2)、如果修改的键不存在,则会将新添加的键值对添加到字典中

代码示例:

testdict = {'Name': 'wangdoudou', 'Age': 7, 'Class': 'First'}
print("testdict当前字典的样式为:", testdict)
tempvalue = testdict.setdefault("Number", 13512345678)
print("修改后的值tempvalue=", tempvalue, "\ntestdict修改后的样式为:", testdict)

输出结果:

10、update(self, E=None, **F):把字典dict2的键/值对更新到dict里,键存在的,更新值,键不存在的,添加键值对到字典中

写法1代码示例:

testdict = {'Name': 'wangdoudou', 'Age': 7, 'Class': 'First'}
print("testdict当前字典的样式为:", testdict)
testdict.update({'Age':15, "Number":135634654})
print("testdict更新后的样式为;", testdict)

输出结果:

写法2代码示例:

testdict = {'Name': 'wangdoudou', 'Age': 7, 'Class': 'First'}
print("testdict当前字典的样式为:", testdict)
testdict.update(Age=15, Number=124152363)
print("testdict更新后的样式为;", testdict)

输出结果:

11、in:判断某个键是否在字典中

代码示例:

testdict = {'Name': 'wangdoudou', 'Age': 7, 'Class': 'First'}
print("testdict当前字典的样式为:", testdict)
v1 = "Name" in testdict
print("Name键在字典testdict中:", v1)
v2 = "Number" in testdict
print("Number键在字典testdict中:", v2)

输出结果:

猜你喜欢

转载自blog.csdn.net/WandDouDou/article/details/82700210