python基础入门(9)之字典

目录

Python字典

一、字典理解

1.1)创建字典与访问

1.2)字典长度

1.3)数据类型

二、访问字典

2.1)访问键名

2.2)访问健值

三、更改字典各种方法

四、添加字典项各种方法

五、删除字典的各种方法

​六、遍历字典

七、复制字典

八、嵌套字典

九、练习


Python字典

一、字典理解

基本形式:

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}

字典用于在键值对中存储数据值。字典是有序*、可变且不允许重复的集合。(从 Python 3.7 版开始,字典是有序的。在 Python 3.6 及更早版本中,字典是无序的。)

1.1)创建字典与访问

字典是用大括号写的,有键和值。
创建并打印字典:

thisdict = {
  "brand": "China",
  "model": "zhangsan",
  "year": 1688
}
print(thisdict)

 输出:

字典项是有序的、可变的,并且不允许重复。字典项以键值对的形式呈现,可以使用键名进行引用。
例如打印brand的值

thisdict = {
  "brand": "China",
  "model": "zhangsan",
  "year": 1688,
}
print(thisdict["brand"])

 输出:

字典不能有两个具有相同键的项目:重复值将覆盖现有值

thisdict = {
  "brand": "China",
  "model": "zhangsan",
  "year": 1688,
  "year": 2021
}
print(thisdict)

返回:

1.2)字典长度

还是用用len函数

thisdict = {
  "brand": "China",
  "model": "zhangsan",
  "year": 1688,
}
print(len(thisdict))

 输出:

1.3)数据类型

字典项中的值可以是任何数据类型:
例如:

thisdict = {
  "brand": "China",
  "electric": False,
  "year": 1688,
  "colors": ["red", "white", "blue"]
}

类型:dict()
打印字典的数据类型:

thisdict = {
  "brand": "China",
  "electric": False,
  "year": 1688,
  "colors": ["red", "white", "blue"]
}

print(type(thisdict))

返回:

 

补充巩固:
在这里插入图片描述

二、访问字典

2.1)访问键名

您可以通过引用方括号内的键名来访问字典的项目:

thisdict = {
  "name": "笨小孩",
  "address": "河北",
  "year": 2000
}
x = thisdict["name"]
print(x)

 返回:

还有一个被调用的方法get()会给你同样的结果:

thisdict = {
  "name": "笨小孩",
  "address": "河北",
  "year": 2000
}
x = thisdict["name"]
y=thisdict.get('name')
print(x)
print(y)

返回:

2.2)访问健值

keys()方法将返回字典中所有键的列表。

thisdict = {
  "name": "笨小孩",
  "address": "河北",
  "year": 2000
}
x = thisdict.keys()
print(x)

 返回:

向原始字典添加一个新项目,并看到键列表也得到更新:

thisdict = {
  "name": "笨小孩",
  "address": "河北",
  "year": 2000
}
thisdict['age']=20
print(thisdict)

返回:

获取值
values()方法将返回字典中所有值的列表。

x = thisdict.values()

items()方法将返回字典中的每个项目,作为列表中的元组。

thisdict = {
  "name": "笨小孩",
  "address": "河北",
  "year": 2000
}
thisdict['age']=20
print(thisdict)
x = thisdict.items()
print(x)

返回元祖:

要确定字典中是否存在指定的键,请使用in关键字:

thisdict = {
  "name": "笨小孩",
  "address": "河北",
  "year": 2000
}
if 'name' in thisdict:
    print('name在字典')

返回:

三、更改字典各种方法

前面我们讲到了一部分更改内容,这里我们具体讲一下。
例如我要把川川改为川川菜鸟:

thisdict = {
  "name": "笨小孩",
  "address": "河北",
  "year": 2000
}
thisdict['name'] = '笨洋洋'
print(thisdict)

返回:​​​​​​​

或者我们使用update()方法:

thisdict = {
  "name": "笨小孩",
  "address": "河北",
  "year": 2000
}
thisdict.update({'name':'笨洋洋'})
print(thisdict)

效果一样:​​​​​​​

四、添加字典项各种方法

比如我要添加一个年龄为20:

thisdict = {
  "name": "笨小孩",
  "address": "河北",
  "year": 2000
}
thisdict['age']=20
print(thisdict)

返回:

或者还是使用update:

thisdict = {
  "name": "笨小孩",
  "address": "河北",
  "year": 2000
}
thisdict['age']=20
print(thisdict)
thisdict.update({'age':'20岁'})
print(thisdict)

返回:

五、删除字典的各种方法

pop()方法删除具有指定键名的项。
比如我要删除地址项目:

thisdict = {
  "name": "笨小孩",
  "address": "河北",
  "year": 2000
}
thisdict.pop("address")
print(thisdict)

返回:

popitem()方法删除最后插入的项目(在 3.7 之前的版本中,将删除随机项目):

thisdict = {
  "name": "笨小孩",
  "address": "河北",
  "year": 2000
}
thisdict.pop("address")
print(thisdict)
thisdict.popitem()
print(thisdict)

返回:

del关键字删除与指定键名称的项目:

thisdict = {
  "name": "笨小孩",
  "address": "河北",
  "year": 2000
}
del thisdict['name']
print(thisdict)

返回:

​​​​​​​

del关键字也可以删除字典完全:

del thisdict

clear()方法清空字典:

thisdict = {
  "name": "笨小孩",
  "address": "河北",
  "year": 2000
}
thisdict.clear()
print(thisdict)

返回空:


六、遍历字典

将字典中的所有键名,一一打印出来:

thisdict = {
  "name": "笨小孩",
  "address": "河北",
  "year": 2000
}
for x in thisdict:
  print(x)

 输出:

 

一一打印字典中的所有值:

thisdict = {
  "name": "笨小孩",
  "address": "河北",
  "year": 2000
}
for x in thisdict:
  print(thisdict[x])

返回:

您还可以使用该values()方法返回字典的值:

thisdict = {
  "name": "笨小孩",
  "address": "河北",
  "year": 2000
}
for x in thisdict.values():
  print(x)

输出: 

 

您可以使用该keys()方法返回字典的键:

thisdict = {
  "name": "笨小孩",
  "address": "河北",
  "year": 2000
}
for x in thisdict.keys():
  print(x)

返回:

使用以下 方法循环遍历keys和valuesitems():

thisdict = {
  "name": "笨小孩",
  "address": "河北",
  "year": 2000
}
for x, y in thisdict.items():
  print(x, y)

返回:

​​​​​​​

七、复制字典

用copy()函数

thisdict = {
  "name": "笨小孩",
  "address": "河北",
  "year": 2000
}
mydict=thisdict.copy()
print(mydict)

输出:

 

内置dict()函数

thisdict = {
  "name": "笨小孩",
  "address": "河北",
  "year": 2000
}
mydict=dict(thisdict)
print(mydict)

效果都一样:

八、嵌套字典

创建一个包含三个字典的字典:

myfamily = {
  "child1" : {
    "name" : "Emil",
    "year" : 2004
  },
  "child2" : {
    "name" : "Tobias",
    "year" : 2007
  },
  "child3" : {
    "name" : "Linus",
    "year" : 2011
  }
}
print(myfamily)

返回:

创建三个字典,然后创建一个包含其他三个字典的字典:

child1 = {
  "name" : "Emil",
  "year" : 2004
}
child2 = {
  "name" : "Tobias",
  "year" : 2007
}
child3 = {
  "name" : "Linus",
  "year" : 2011
}

myfamily = {
  "child1" : child1,
  "child2" : child2,
  "child3" : child3
}
print(myfamily)

效果一样:

九、练习

1-使用get方法打印汽车字典的“model”键的值。

car =	{
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
print

2-将“year”值从 1964 更改为 2020。

car =	{
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}

3-将键/值对 “color” : “red” 添加到汽车字典中。

car =	{
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}

4-使用 pop 方法从汽车字典中删除“model”。

car =	{
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}

5-使用clear方法清空car字典。

car =	{
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值