今天复习的内容是元组,字典相关知识
一.元组
1.简介
python 中的元组和列表一样,区别在于:元组中的元素不能修改,元组用的是小括号,而列表用的是中括号。
元组可以包括数字,字符串,元组,列表。
tu = (1, 'a', [2, 3], (4, 5))
print(tu)
print(type(tu))
运行结果:
写元组的时候要在末尾加一个逗号,不然区分不了是元组还是参数。
# tu2 = () # <class 'tuple'>
# tu2 = (1) # <class 'int'>
tu2 = (1, ) # <class 'tuple'>
print(type(tu2))
运行结果:
访问元组:
a = (0,1,2)
print(a[0])
print(a[1])
运行结果:
2.修改元组
说明:python 中不允许修改元组中的数据,也不能删除其中的元素。
tua = (1, 2, 3)
tua[0] = 4
print(tua)
运行结果:
3. index , count
元组中 index 和 count 的用法和列表,字符串中的一样。
pyhton 元组中index() 方法可以找出某个对象第一个匹配项的索引值,如果该对象不在元组中则会抛出异常。
(1)index () 中的参数
obj:指定检索的对象
start :开始索引,默认为0,可以单独指定,可选参数
end:结束索引,默认为元组的程度,可选参数,不可以单独指定
(2)count ()函数用来统计某个对象在元组中出现的次数
第一个例子:
a = ('a', 'b', 'c', 'a', 'b')
print(a.index('a', 1, 3)) # 注意是左闭右开区间
运行结果:

第二个例子:
a = ('a', 'b', 'c', 'a', 'b')
print(a.index('a', 1, 4))
运行结果:

第三个例子:
a = ('a', 'b', 'c', 'a', 'b')
print(a.count('b'))
print(a.count('d'))
运行结果:
第四个例子:
tua = (1, 'a', 3, 'b', 5)
print(tua.index('a', 1, 2)) # 1
tu = ('a', 'b', 'c', 'a', 'e')
print(tu.count('a')) # 2
二.字典
1.简要介绍
dic = {'name': 'zs', 'age': 18}
- 字典和列表一样,也能够存储多个数据,字典是键值对形式。
- 字典中的键具备唯一性,而值可重复.
- 字典的每个元素由2部分组成,键:值。
例如 'name':'班长' ,'name'为键,'班长'为值 - 列表中找某个元素时,是根据下标进行的;字典中找某个元素时,是根据'名字'(就是冒号:前面的那个值,例如上面代码 中的'name'、'id'、'sex')。
2.根据键访问值
dic = {'name': 'zs', 'age': 18, 'sex': 'nv'}
print(dic['name'])
print(dic['age'])
运行结果:
若访问不存在的键,则会报错:
dic = {'name': 'zs', 'age': 18, 'sex': 'nv'}
print(dic['name1'])
运行结果:
在我们不确定字典中是否存在某个键而又想获取其值时,可以使用get方法,还可以设置默认值:
dic = {'name': 'zs', 'age': 18, 'sex': 'nv'}
age1 = dic.get('age1')
print(type(age1))
age1= dic.get('age1', 18) # 若不存在'age1'这个键,就返回默认值20
print('age1')
运行结果:
另一个例子:
dic = {'name': 'zs', 'age': 20}
ad = dic.get('ade')
ad2 = dic.get('ade', '不存在')
print(ad) # None
print(ad2) # 不存在
运行结果:
3.字典常见操作1
(1)查看元素
在字典操作中,不仅可以用key查找元素,还可以用get来获取元素
info = {'name':'吴彦祖','age':18}
print(info['age']) # 获取年龄
# print(info['sex']) # 获取不存在的key,会发生异常
print(info.get('sex')) # 获取不存在的key,获取到空的内容,不会出现异常 None
运行结果:
(2) 修改元素
字典中的每个元素都是可以修改的,只要能通过key找到,就可以修改
dic = {'name': 'zs', 'age': 18}
nage = int(input('请输入您的年龄:'))
dic['age'] = nage
print('修改之后的age为%d' % dic['age'])
运行结果:
(3)添加元素
# 访问不存在的数据
dic = {'name': 'zs', 'age': 18}
print(dic['id'])
运行结果:
如果在使用 变量名['键'] = 数据 时,这个“键”在字典中,不存在,那么就会新增这个元素
dic = {'name': 'zs', 'age': 18}
print(dic)
dic['id'] = 10
print(dic)
运行结果:
(4) 删除元素
对字典进行删除操作,有一下几种:del、clear()
-
del删除指定的元素
dic = {'name': 'zs', 'age': 18, 'id': 10}
print('删除前:%s' % dic)
del dic['id']
print('删除后:%s' % dic)
运行结果:
-
del删除整个字典
info = {'name':'monitor', 'sex':'f', 'address':'China'}
print('删除前,%s'%info)
del info
print('删除后,%s'%info) # NameError: name 'dic' is not defined
运行结果:
-
clear清空整个字典
dic = {'name': 'zs', 'age': 18, 'id': 10}
print('清空前:%s' % dic)
dic.clear()
print('清空后:%s' % dic)
运行结果:
4.字典的常见操作2
-
len()
测量字典中,键值对的个数
dic = {'name': 'zs', 'age': 18, 'id': 10}
print(len(dic)) # 3
运行结果:
-
keys
返回一个包含字典所有KEY的列表
dic = {'name': 'zs', 'age': 18, 'id': 10}
print(dic.keys())
运行结果:
-
values
返回一个包含字典所有value的列表
dic = {'name': 'zs', 'age': 18, 'id': 10}
print(dic.values())
运行结果:
-
items
返回一个包含所有(键,值)元祖的列表
dic = {'name': 'zs', 'age': 18, 'id': 10}
print(dic.items())
运行结果:
三.遍历
通过for ... in ... 我们可以遍历字符串、列表、元组、字典等
(1)字符串遍历
str2 = 'hello'
for i in str2:
print(i, end=' ') # h e l l o
运行结果:
(2) 列表遍历
li = [1, 2, 3]
for i in li:
print(i)
运行结果:
(3) 元组遍历
tu = (1, 2, 3)
for i in tu:
print(i)
运行结果:
(4) 字典遍历
dic = {'name': 'zs', 'age': 20}
# 遍历字典的key
for key in dic.keys():
print(key)
# 遍历字典的value
运行结果:
综合例子
dic = {'name': 'zs', 'age': 20}
for val in dic.values():
print(val)
# 遍历字典的项(元素)
for item in dic.items():
print(item)
# # 遍历字典的键值对
for key, val in dic.items():
print("key=%s,value=%s" % (key, val))
运行结果:
四.enumerate()--枚举
enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标, 一般用在 for 循环当中。
语法:enumerate(sequence, [start=0]) sequence -- 一个序列、迭代器或其他支持迭代对象。 start -- 下标起始位置。
li = ['a', 'b', 'c', 'd']
for i, j in enumerate(li):
print(i, j)
运行结果:
五.集合
集合是无序的,集合中的元素是唯一的,集合一般用于元组或者列表中的元素去重。
1.定义一个空集合
set1 = set() # set()函数:创建一个无序不重复元素集
# 注意以下写法为一个空字典
set2 = {}
s1 = set()
print(type(s1)) # <class 'set'>
a = set([1, 2, 3, 4]) # 需要放入可迭代对象
print(a) # {1, 2, 3, 4}
运行结果:
2.添加元素
(1)add
s2 = {1, 2, 3, 4}
#添加元素
print(s2) # {1, 2, 3, 4}
s2.add(8)
print(s2) # {1, 2, 3, 4, 8}
运行结果:
(2)update
s2 = {1, 2, 3, 4}
#是把要传入的元素拆分,做为个体传入到集合中
print(s2) # {1, 2, 3, 4}
s2.add('rty')
print(s2) # {1, 2, 3, 4, 'rty'}
s2.update('abcd')
print(s2) # {1, 2, 3, 4, 'd', 'a', 'b', 'c'}
运行结果:
(3) 删除元素(remove,pop,discard)
-
remove
s2 = {1, 2, 3, 4}
# 使用remove删除集合中的元素 如果有 直接删除 如果没有 程序报错
s2.remove(1)
print(s2)
运行结果:
-
pop
set 集合的 pop 方法会对集合进行无序的排列,然后将这个无序排列集合的左面第一个元素进行删除。
# 使用pop删除是随机删除集合中的元素
s2 = {1, 2, 4, 5, 6}
# s2 = {'a', 'c', 'b', 'd'}
s2.pop()
print(s2) # {2, 4, 5, 6}
运行结果:
# 为什么整数不会无序排列呢?
字典和集合无序的实现方式是hash表。没错,他们是通过hash值来将对象放入hash表中,从而达到无序的操作(众所周知对 象的hash值是不断变化的)
-
discard
# 使用discard删除 如果元素存在 直接删除 如果元素不存在 不做任何操作
a = {1, 2, 3, 4}
a.discard(1)
print(a) # {2, 3, 4}
a.discard(5)
print(a)
运行结果:
3.交集和并集( & 和 | )
(1) 交集
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
# 交集
se = a & b
print(se)
# {3, 4} # 如果没有的话,显示set()
运行结果:
(2) 并集
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
se2 = a | b
print(se2)
# {1, 2, 3, 4, 5, 6}
运行结果:

(3) 差集
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
se = a - b
print(se)
# {1, 2}
运行结果:
不行了,爆肝写完的,明天继续!
本文详细介绍了Python中的元组和字典概念,包括元组的不可变性、访问与修改方法,以及字典的键值对结构、常用操作如获取、修改、添加和删除,以及集合的交集、并集和差集。
1万+

被折叠的 条评论
为什么被折叠?



