ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

DAY 08冲击蓝桥杯——Python基础08python字典

2022-01-29 23:06:38  阅读:162  来源: 互联网

标签:name Python 08 蓝桥 thisdict year print hg 字典


文章目录

9.1 字典的基本概念

基本形式:

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

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

9.1.1 创建字典与访问

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

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

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

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
print(thisdict["brand"])

9.1.2 字典长度

还是用用len函数

hisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964,
  "year": 2020
}
print(len(thisdict))

9.1.3 数据类型

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

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

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

thisdict = {
  "brand": "Ford",
  "electric": False,
  "year": 1964,
  "colors": ["red", "white", "blue"]
}
print(type(thisdict))

(3)与其他数据类型的区别
列表是有序且可变的,允许重复元素;
元组是有序且不可更改的,允许重复元素;
集合是无序且无索引的,无重复元素;
字典是有序且可变的,无重复元素。

9.2 字典的“增”

9.2.1 关键词索引添加

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

thisdict = {
  "name": "hg",
  "address": "长沙",
  "year": 2000
}
thisdict['age']=21
print(thisdict)

9.2.2 使用update

thisdict = {
  "name": "hg",
  "address": "长沙",
  "year": 2000
}
thisdict['age']=21
print(thisdict)
thisdict.update({'age':'21岁'})
print(thisdict)

9.3 字典的“删”

9.3.1 pop()方法

删除具有指定键名的项。
比如我要删除地址项目:

thisdict = {
  "name": "hg",
  "address": "长沙",
  "year": 2000
}
print(thisdict)
thisdict.pop("address")
print(thisdict)

9.3.2 popitem()方法

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

thisdict = {
  "name": "hg",
  "address": "长沙",
  "year": 2000
}
thisdict.pop("address")
print(thisdict)
thisdict.popitem()
print(thisdict)

9.3.3 del关键字

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

thisdict = {
  "name": "hg",
  "address": "长沙",
  "year": 2000
}
del thisdict['name']
print(thisdict)

也可以完全删除该字典。

9.3.4 clear()方法

清空字典:

thisdict = {
  "name": "hg",
  "address": "长沙",
  "year": 2000
}
thisdict.clear()
print(thisdict)

9.4 字典的“改”

9.4.1 关键词索引

thisdict = {
  "name": "hg",
  "address": "长沙",
  "year": 2000
}
thisdict['name'] = 'hg菜鸟'
print(thisdict)

9.4.2 使用update()方法:

thisdict = {
  "name": "hg",
  "address": "长沙",
  "year": 2000
}
thisdict.update({'name':'hg菜鸟'})
print(thisdict)

9.5 字典的“查”

9.5.1 访问键名

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

thisdict = {
  "name": "hg",
  "address": "长沙",
  "year": 2000
}
x = thisdict["name"]
print(x)

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

thisdict = {
  "name": "hg",
  "address": "长沙",
  "year": 2000
}
x = thisdict["name"]
y=thisdict.get('name')
print(x)
print(y)

9.5.2 访问键值

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

thisdict = {
  "name": "hg",
  "address": "长沙",
  "year": 2000
}
x = thisdict.keys()
print(x)

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

thisdict = {
  "name": "hg",
  "address": "长沙",
  "year": 2000
}
thisdict['age']=20
print(thisdict)

(3)获取值

thisdict = {
  "name": "hg",
  "address": "长沙",
  "year": 2000
}
x = thisdict.values()
print(x)

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

thisdict = {
  "name": "hg",
  "address": "长沙",
  "year": 2000
}
thisdict['age']=20
print(thisdict)
x = thisdict.items()
print(x)

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

thisdict = {
  "name": "hg",
  "address": "长沙",
  "year": 2000
}
if 'name' in thisdict:
    print('name在字典')

9.6 遍历字典

9.6.1 打印字典中的所有键名

thisdict = {
  "name": "hg",
  "address": "长沙",
  "year": 2000
}
for x in thisdict:
  print(x)

9.6.2 打印字典中的所有值:

thisdict = {
  "name": "hg",
  "address": "长沙",
  "year": 2000
}
for x in thisdict:
  print(thisdict[x])

9.6.3 使用values()方法返回字典的值:

thisdict = {
  "name": "hg",
  "address": "长沙",
  "year": 2000
}
for x in thisdict.values():
  print(x)

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

thisdict = {
  "name": "hg",
  "address": "长沙",
  "year": 2000
}
for x in thisdict.keys():
  print(x)

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

thisdict = {
  "name": "hg",
  "address": "长沙",
  "year": 2000
}
for x, y in thisdict.items():
  print(x, y)

9.7 复制字典

9.7.1 用copy()函数

thisdict = {
  "name": "hg",
  "address": "长沙",
  "year": 2000
}
mydict=thisdict.copy()
print(mydict)

9.7.2 内置dict()函数

thisdict = {
  "name": "hg",
  "address": "长沙",
  "year": 2000
}
mydict=dict(thisdict)
print(mydict)

9.8 字典的嵌套

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

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)

练习题

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

car =	{
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
print(car.get("model"))

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

car =	{
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
car["year"]=2020
print(car)

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

car =	{
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
car["color"]="red"
print(car)

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

car =	{
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
car.pop("model")
print(car)

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

car =	{
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
car.clear()
print(car)

answer都经过python调试。

  1. https://chuanchuan.blog.csdn.net/article/details/120419754?spm=1001.2014.3001.5502

  2. 用Python玩转数据——中国大学mooc

标签:name,Python,08,蓝桥,thisdict,year,print,hg,字典
来源: https://blog.csdn.net/f1220684378/article/details/122748748

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有