我是靠谱客的博主 甜蜜项链,这篇文章主要介绍Python语法基础刻意练习:Task06(字典与集合),现在分享给大家,希望可以做个参考。

字典

一个简单的字典

复制代码
1
2
3
4
alien_0={'color':'green','points':5} print(alien_0['color']) #green print(alien_0['points']) #5

使用字典
字典是一系列键—值对。每个键都与一个值相关联,你可以使用键来访问与之相关联的值。与键相关联的值可以是数字、字符串、列表乃至字典,事实上,可将任何python的对象用作字典中的值。
在python中,字典用放在花括号{}中的一系列键—值对表示。如前面示例所示:

复制代码
1
2
alien_0={'color':'green','points':5}

键—值对是两个相关联的值。指定键时,python将返回与之相关联的值。键和值之间用冒号分隔,而键—值对之间用逗号分隔。

访问字典中的值
要获取与键相关联的值,可依次指定字典名和放在方括号内的键,如:

复制代码
1
2
3
alien_0={'color':'green'} print(alien_0['color']) #green
复制代码
1
2
3
4
alien_0={'color':'green','points':5} new_points=alien_0['points'] print('you just earned '+str(new_points)+' points!') #you just earned 5 points!

添加键—值对
字典是一种动态结构,可随时在其中添加键—值对。要添加键—值对,可依次指定字典名、用方括号括起的键和相关联的值。

复制代码
1
2
3
4
5
6
7
alien_0={'color':'green','points':5} print(alien_0) #{'color':'green','points':5} alien_0['x_position']=0 alien_0['y_position']=25 print(alien_0) #{'color':'green','points':5,'x_position':0,'y_position':25}
复制代码
1
2
3
4
5
6
alien_0={} alien_0['x_position']=0 alien_0['y_position']=25 print(alien_0) #{'x_position':0,'y_position':25}

修改字典中的值
要修改字典中的值,可依次指定字典名、用方括号括起的键以及与该键相关联的新值。如:

复制代码
1
2
3
4
5
alien_0={'color':'green'} print('the alien is '+alien_0['color']) #the alien is green alien_0['color']='yellow' print('the alien is now '+alien_0['color']) #the alien is now yellow

外星人的颜色发生改变。
下面我们对一个能够以不同速度移动的外星人的位置进行追踪。为此,我们将存储该外星人的当前速度,并据此确定该外星人将向右移动多远。:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
alien_0={'x_position':0,'y_position':25,'speed':'medium'} print("original x_position: "+str(alien_0['x_position'])) #original x_position: 0 #向右移动外星人 #据外星人当前速度决定将其移动多远 if alien_0['speed']=='slow': x_increment=1 elif alien_0['speed']=='medium': x_increment=2 else: x_increment=3 alien_0['x_position']+=x_increment print('new x_position: '+str(alien_0['x_position'])) #new x_position: 2

删除键—值对
对于字典中不需要的信息,可使用del语句将对应的键—值对彻底删除。使用del语句时,必须指定字典名和要删除的键。

复制代码
1
2
3
4
5
6
alien_0={'color':'green','points':5} print(alien_0) #{'color':'green','points':5} del alien_0['points'] print(alien_0) #{'color':'green'}

遍历字典
鉴于字典可能包含大量的数据,python支持字典遍历。字典可用于以各种方式存储信息,因此有多种遍历字典的方式:可遍历字典的所有键—值对、键或值。

  • 遍历所有的键—值对
    下面这个字典存储有关网站用户的信息:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
user_0={ 'username':'efermi', 'first':'enrico', 'last':'fermi' } for key,value in user_0.items(): print('nKey: '+key) print('Value: '+value) #Key: username #Value: efermi #Key: first #Value: enrico #Key: last #Value: fermi

编写了用于遍历字典的for循环,声明了两个变量,用于存储键—值对中的键和值。对于这两个变量,可使用任何名称。在for循环中,还包含字典名和方法items(),它返回键—值对列表。在for循环内,打印键和值。

  • 遍历字典中所有的键
    在不需要使用字典中的值时,方法keys()很有用:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
user_0={ 'username':'efermi', 'first':'enrico', 'last':'fermi' } for key in user_0.keys(): print(key) #username #first #last

遍历字典时,会默认遍历所有的键,因此,如果上述代码中的for key in user_0.keys():替换为for key in user_0:,输出将不变。

复制代码
1
2
3
4
5
6
7
8
9
10
user_0={ 'username':'efermi', 'first':'enrico', 'last':'fermi' } if 'last' in user_0: print('it is true.') #it is true.
  • 遍历字典中所有的值
    可使用方法value()遍历所有的值:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
user_0={ 'username':'efermi', 'first':'enrico', 'last':'fermi' } for value in user_0.values(): print(value) #efermi #enrico #fermi

嵌套
有时候,需要将一系列字典存储在列表中,或将列表作为值存储在字典中,这称为嵌套。你可以在列表中嵌套字典、在字典中嵌套列表甚至在字典中嵌套字典。

字典列表

复制代码
1
2
3
4
5
6
7
8
9
10
11
alien_0={'color':'green','points':5} alien_1={'color':'yellow','points':10} alien_2={'color':'red','points':15} aliens=[alien_0,alien_1,alien_2] for alien in aliens: print(alien) #{'color':'green','points':5} #{'color':'yellow','points':10} #{'color':'red','points':15}

先创建了三个字典,再将这三个字典放到列表里,最后遍历列表,打印字典。

在字典中存储列表

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
pizza={ 'crust':'thick', 'toppings':['mushrooms','extra cheese'], } print(pizza) #{'crust':'thick','toppings':['mushrooms','extra cheese'],} print('you ordered a '+pizza['crust']+'-crust pizza '+'with the following toppings:') for topping in pizza['toppings']: print('t'+topping) # you ordered a thick-crust pizza with the following toppings: mushrooms extra cheese

在字典中存储字典

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
users={ 'aeinstein':{ 'first':'albert', 'last':'einstein', 'location':'princeton', }, 'mcurie':{ 'first':'marie', 'last':'curie', 'location':'pairs', } } print(users) #'aeinstein':{'first':'albert','last':'einstein','location':'princeton',},'mcurie':{'first':'marie','last':'curie','location':'pairs',}}

集合

定义:1.不同元素组成 2.无序 3。集合中的元素必须是不可变类型。
创建集合

复制代码
1
2
s={1,2,3,4,5,6,7}

集合运算
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
集合方法
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
集合内置函数
在这里插入图片描述

最后

以上就是甜蜜项链最近收集整理的关于Python语法基础刻意练习:Task06(字典与集合)的全部内容,更多相关Python语法基础刻意练习内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(72)

评论列表共有 0 条评论

立即
投稿
返回
顶部