Python系统学习第九课

本文深入探讨了Python中的元组、集合、字典等数据结构的使用方法,包括元组的基本操作、集合的特性及函数、字典的创建与操作,以及这些数据结构在实际编程中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于元组的函数

  • 基本和list一样
#获取元组长度
t = (1,2,3,4, 3)
len(t)
4
#获取元组最大最小值
print(max(t))
print(min(t))
4
1
#index 求指定元素在元祖中的索引位置, 如果中元组中有多个值,则返回的是第一个
print(t.index(3))
2

元组变量交换法

  • 两个变量交换值
a = 1
b = 3
a,b = b, a
print(a)
print(b)
3
1

集合-set

  • 集合是高中数学的一个概念
  • 一堆确定的,无序的,唯一的数据,集合中每一个数据成为一个元素
#集合的定义
s = set()
print(type(s))
<class 'set'>
#如果只是用大括号定义,则定义的是一个dict类型
d = {}
print(type(d))
d = {1,2,3,3,4,5}   #如果大括号内部有值就成为了一个集合
print(type(d))
<class 'dict'>
<class 'set'>

集合的特征

  • 集合数据无序,那么不可以索引和分片,
  • 集合内部数据具有唯一性,可以排除重复数据
  • 集合内的数据可以放字符串,数值,元组,就是说内部可以放可哈希数据

成员检测

  • in / not in
# 带有元祖的集合遍历
s = {(1,2,3),("lizi", "liuya", "tiantian"),(2,3,5)}    #集合本身不可哈希,集合内部不可有集合的元素
for i,j,k in s:
    print(i, "-", j, "-",k)
2 - 3 - 5
lizi - liuya - tiantian
1 - 2 - 3

集合的内涵

#普通的集合内涵
s = {2,3,6,5,4,12,5,8,79,4,5,6,12,3,5}
print(s)   #自动过滤重复函数
{2, 3, 4, 5, 6, 8, 12, 79}
#普通集合内涵
s1 = {i for i in s}
print(s1)
{2, 3, 4, 5, 6, 8, 12, 79}
#带条件的集合过滤
s1 = {i for i in s if i%2 == 0}
print(s1)
{2, 4, 6, 8, 12}

关于集合的函数

  • len
  • max
  • min
#set生成一个集合
s = [1,2,3,34,45,5,6,6,3,2,2,3,4,5,3]
a = set(s)
print(a)
{1, 2, 3, 34, 5, 6, 4, 45}
#add在集合内添加元素
a.add(99)
print(s)
[1, 2, 3, 34, 45, 5, 6, 6, 3, 2, 2, 3, 4, 5, 3]
#clear函数
a.clear()
print(a)   #只是原地将数据清空了,id不变
set()

#copy函数
#remove函数;移除指定的值,直接改变原有的值,如果原有的值不存在,就会报错
#discard函数;移除集合中指定的值, 如果原有的值不存在,不会报错

#pop函数,随机移除一个元素
s = {5,9,32,23,5,6,54}
d = s.pop()
print(d)
print(s)
32
{5, 6, 9, 54, 23}
#集合函数
#intersection:交集
#difference:差集
#union: 并集
#issubset:检查一个集合是否是另一个集合的子集
#issuperset:检查一个集合是不是另一个集合的超集
s1 = {1,2,3,4,5,6}
s2 = {3,4,5,6,7,8}
s3 = s1.intersection(s2)
s4 = s1.difference(s2)

print(s3)
print(s4)
print(s1.issubset(s2))
print(s1.issuperset(s2))
{3, 4, 5, 6}
{1, 2}
False
False
#frozen set :冰冻集合:冰冻集合就是不可以进行任何修改的集合,一种特殊的集合
s = {1,2,3,4,5,6}
s1 = frozenset(s)
print(s)
print(type(s))
s1.pop()   #不可修改
print(s1)
{1, 2, 3, 4, 5, 6}
<class 'set'>



---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

<ipython-input-57-4eb685a8ebbb> in <module>()
      4 print(s)
      5 print(type(s))
----> 6 s1.pop()
      7 print(s1)


AttributeError: 'frozenset' object has no attribute 'pop'

字典

  • 字典是一种组合数据,没有顺序的组合数据,数据以“键值对”形式出现
#字典的创建
d = {}
print(d)

#创建空字典
d1 = dict()
print(d1)

#创建有值的字典
d2 = {"one":1, "second":2, "three":3}
print(d)

#创建有值的字典2
d3 = dict({"one":1, "second":2, "three":3})
print(d3)

#创建有值的字典3(使用关键字参数)
d4 = dict(one=1, second=2, three=3)
print(d4)

#创建有值的字典4
d5 = dict([("one",1),("second", 2),("three", 3)])
print(d5)

{}
{}
{}
{'one': 1, 'second': 2, 'three': 3}
{'one': 1, 'second': 2, 'three': 3}
{'one': 1, 'second': 2, 'three': 3}

字典的特征

  • 字典是序列类型,但是是无序序列, 所有没有分片和索引
  • 字典中的数据每个都有键值对组成,即kv对
  • key: 必须是可哈希的值,比如int,float,tuple,但是,list,set, dict不行
  • value: 可以是任何值

字典常见操作

# 访问数据
d = {'one': 1, 'second': 2, 'three': 3}
print(d["second"])
2
#赋值
d = {'one': 1, 'second': 2, 'three': 3}
d["one"] = 4
print(d)
d["one"] = "aaa"
print(d)
{'one': 4, 'second': 2, 'three': 3}
{'one': 'aaa', 'second': 2, 'three': 3}
#删除
d = {'one': 1, 'second': 2, 'three': 3}
del d["one"]
print(d)  #将整个键值对都删除了
{'second': 2, 'three': 3}
#成员检测, 成员检测检测的将是关键字
d = {'one': 1, 'second': 2, 'three': 3}
if 2 in d:
    print("value")
if "second" in d:
    print("key")
if ("two", 2) in d:
    print("kv")

key
#遍历, 遍历是按照key来执行for循环的。
d = {'one': 1, 'second': 2, 'three': 3}
for i in d:
    print(i, d[i])
    
for i in d:
    print(d.values())
    
#也可以进行下边的循环
for i in d.values():
    print(i)
    
for i in d.keys():
    print(i)
    
for i,j in d.items():
    print(i, "--", j)
one 1
second 2
three 3
dict_values([1, 2, 3])
dict_values([1, 2, 3])
dict_values([1, 2, 3])
1
2
3
one
second
three
one -- 1
second -- 2
three -- 3
#加限制条件的字典生成式
d = {'one': 1, 'second': 2, 'three': 3}
d1 = {k:v for k,v in d.items() if v%2 == 0}
print(d1)
{'second': 2}
#字典相关函数:len,max,min,dict
#str(字典):返回字典的字符串格式
d = {'one': 1, 'second': 2, 'three': 3}
print(str(d))
{'one': 1, 'second': 2, 'three': 3}
#clear:请空字典
#items: 返回子弟的键值对组成的元组格式
d = {'one': 1, 'second': 2, 'three': 3}
i = d.items()
print(i)
dict_items([('one', 1), ('second', 2), ('three', 3)])
k = d.keys()
print(k)  #返回一个可循环的结构
dict_keys(['one', 'second', 'three'])
v = d.values()
print(v)  #返回一个可循环的结构
dict_values([1, 2, 3])
#get函数;根据指定键返回响应的值,并且可以设定默认值
d = {'one': 1, 'second': 2, 'three': 3}
print(d.get("one"))
print(d.get("one222"))   #没找到指定值,并不报错,如果这样写就会报错,print(d["one222"])   #会报错

print(d.get("one", 100))  #如果能找到one就打印出来,如果找不到,就返回一个默认值,就是100

print(d.get("one22", 100))

print(d.get("one", 100))
1
None
1
100
1
#fromkeys: 使用指定的序列作为键key,使用一个值作为字典的所有的键的值value
d = {'one', 'second', 'three'}
d1 = dict.fromkeys(d,"asderdds")
print(d1)
{'one': 'asderdds', 'three': 'asderdds', 'second': 'asderdds'}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值