python学习笔记5.1

元祖的函数

  • 以下看代码

# len: 获取元祖的长度
t = (1,2,3,4,5)
len(t)
# max,min: 最大最小值
print(max(t))
print(min(t))
# tuple: 转化或创建元祖
l = [1,2,3,4,5,6,7,8,9]
t = tuple(l)
print(t)


t = tuple()
print(t)
(1, 2, 3, 4, 5, 6, 7, 8, 9)
()

元祖的函数

  • 基本跟list通用
# count: 计算指定数据出现的次数
t = (1,2,3,4,4,4,5,65)
print(t.count(4))


# index:求指定元素在元祖中的索引位置,如果数字有多个,则返回第一个
print(t.index(3))




3
2

元祖变量交换法

  • 两个变量交换值
# 两个变量交换值
a = 1
b = 3

print(a)
print(b)
print("*" * 20)
a,b = b,a
print(a)
print(b)



1
3
********************
3
1

集合-set

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

集合的特征

  • 集合数据无序,即无法使用索引和分片
  • 集合内部数据元素具有唯一性,可以用来排除重复数据
  • 集合内的数据,str ,int, float, tuple, 冰冻集合等,即内部只可以放可哈希数据

集合序列操作

# 成员检测
# in, not in
s = {4,5,"i","love"}

集合遍历操作

# for 循环
s = {4,5,"i","love"}
for i in s:
    print(i,end=" ")




love i 4 5 
# 带有元祖的集合遍历
s = {(1,2,3),("i","love","lilingling"),(4,5,6)}
for k,m,n in s:
    print(k,"--",m,"--",n)
4 -- 5 -- 6
i -- love -- lilingling
1 -- 2 -- 3

集合的内涵

# 普通集合的内涵
# 以下集合在初始化后自动过滤掉重复元素
s = {23,233,545,1,2,3,4,5,6,67,81,2,3,3,4,545}
print(s)

# 普通集合内涵
ss = {i for i in s}
print(ss)
{545, 2, 1, 3, 4, 5, 6, 67, 233, 81, 23}
{545, 2, 3, 4, 1, 5, 6, 67, 233, 81, 23}
# 带条件的集合内涵
sss = {i for i in s if i % 2 == 0}
print(sss)
{2, 4, 6}
# 多循环的集合内涵
s1 = {1,2,3,4}
s2 = {"1","love","lilingling"}

s = {m*n for m in s2 for n in s1}
print(s)

s = {m*n for m in s2 for n in s1 if n == 2}
print(s)

集合函数/关于集合的函数

# len ,max, min :跟其他基本一致
s = {1,2,3,4}
print(len(s))
print(min(s))
print(max(s))
4
1
4
# set: 生成一个集合
l = [1,2,3,4,5,6,77,89,9,3]
s = set(l)
print(s)
{1, 2, 3, 4, 5, 6, 9, 77, 89}
# add: 向集合内添加数据
s = {2,3}
s.add(234)
print(s)
{234, 2, 3}
# clear
s = {1,2,3,4,5,65}
print(id(s))
s.clear()
print(id(s))
# 结果表明只是把数据原地清空
1504493591912
1504493591912
# copy:拷贝
# remove: 移除指定的值
# discard: 移除集合中指定的值
s = {1,2,3,4,5,6}
s.remove(4)
print(s)
s.discard(1)
print(s)

print("*" * 20)
s.discard(100)
print(s)

s.remove(100)
print(s)


{1, 2, 3, 5, 6}
{2, 3, 5, 6}
********************
{2, 3, 5, 6}



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

KeyError                                  Traceback (most recent call last)

<ipython-input-29-0cdbc5637308> in <module>
     12 print(s)
     13 
---> 14 s.remove(100)
     15 print(s)


KeyError: 100
# pop 随机移除一个元素
s = {1,2,3,4,5,6}
d = s.pop()
print(d)
print(s)


1
{2, 3, 4, 5, 6}
# 集合函数
# intersection: 交集
# difference: 差集
# union:并集
# issubset: 检查一个集合是否为另一个子集
# issuperset: 检查一个集合是否为另一个超集

s1 = {1,2,3,4}
s2 = {3,4,5,6,7}
s_1 = s1.intersection(s2)
print(s_1)

s_2 = s1.difference(s2)
print(s_2)


{3, 4}
{1, 2}
# 集合的数学操作
s1 = {1,2,3,4}
s2 = {3,4,5,6,7}

s_1 = s1 - s2
print(s_1)
{1, 2}

frozen set: 冰冻集合

  • 冰冻集合就是不可以修改的集合
# 创建冰冻集合
s = frozenset()
print(type(s))
print(s)
<class 'frozenset'>
frozenset()

dict字典

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

d = dict()
print(d)

# 创建有值的字典,每一组数据用冒号隔开,每一对键值对用逗号隔开
d = {"one":1,"two":2,"three":3}
print(d)

# 用dict创建有内容字典1
d = dict({"one":1,"two":2,"three":3})
print(d)

# 用dict创建有内容字典2
# 利用关键字参数
d = dict(one=1,two=2,three=3)
print(d)

#
d = dict([("one",1),("two",2),("three",3)])

{}
<class 'dict'>
{}
{'one': 1, 'two': 2, 'three': 3}
{'one': 1, 'two': 2, 'three': 3}
{'one': 1, 'two': 2, 'three': 3}

字典的特征

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

字典常用操作

# 访问数据
d = {"one":1,"two":2,"three":3}
# 注意访问格式
# 中括号内是键值
print(d["one"])


d["one"] = "eins"
print(d)

# 删除某个操作
# 使用del操作
del d["one"]
print(d)
1
{'one': 'eins', 'two': 2, 'three': 3}
{'two': 2, 'three': 3}
# 成员检测, in , not in
# 成员检测检测的是key的内容
d = {"one":1,"two":2,"three":3}
if 2 in d:
    print("value")

if "two" in d:
    print("key")

    
if("two",2) in d:
    print("kv")




key
# 遍历在python2和3中区别较大,代码不通用
# 按 key来使用for循环
d = {"one":1,"two":2,"three":3}

# 使用for循环,直接按key值访问
for k in d:
    print(k,d[k])
    
# 上述代码可以改写成如下
for k in d.keys():
    print(k,d[k])
    
# 只访问字典的值
for v in d.values():
    print(v)
    
# 注意以下特殊用法
for k,v in d.items():
    print(k,'--',v)
one 1
two 2
three 3
one 1
two 2
three 3
1
2
3
one -- 1
two -- 2
three -- 3

字典生成式

#常规字典生成式
d = {"one":1,"two":2,"three":3}
dd = {k:v for k,v in d.items()}
print(dd)

#加限制条件字典生成式
d = {"one":1,"two":2,"three":3}
dd = {k:v for k,v in d.items() if v % 2 == 0}
print(dd)
{'one': 1, 'two': 2, 'three': 3}
{'two': 2}

字典相关函数

# 通用函数:len ,max , min ,dict
# str(字典): 返回字典的字符串格式

d = {"one":1,"two":2,"three":3}
print(str(d))


{'one': 1, 'two': 2, 'three': 3}
# clear: 清空字典
# items: 返回字典的键值对组成的元祖格式
d = {"one":1,"two":2,"three":3}
i = d.items()
print(type(i))
print(i)

<class 'dict_items'>
dict_items([('one', 1), ('two', 2), ('three', 3)])
# keys:返回字典的键组成的一个结构
d = {"one":1,"two":2,"three":3}
i = d.keys()
print(type(i))
print(i)
<class 'dict_keys'>
dict_keys(['one', 'two', 'three'])
# values:返回字典的值组成的一个结构
d = {"one":1,"two":2,"three":3}
i = d.values()
print(type(i))
print(i)
<class 'dict_values'>
dict_values([1, 2, 3])
# get: 根据指定键返回相应值,好处是,可以设置默认值
d = {"one":1,"two":2,"three":3}
print(d.get("one333"))

# get默认值是None,可以设置
print(d.get("one",100))
print(d.get("one333",100))

# 体会和上面代码的区别
print(d["one333"])
None
1
100



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

KeyError                                  Traceback (most recent call last)

<ipython-input-15-79cae3ab4456> in <module>
      8 
      9 # 体会和上面代码的区别
---> 10 print(d["one333"])


KeyError: 'one333'
# fromkeys:使用指定的序列作为键,使用一个值作为字典的所有值
l = {"eins","sdsdd","dsdd"}
# 注意fromkeys两个参数的类型
# 注意fromkeys的调用主体
d = dict.fromkeys(l,"hahahahahah")
print(d)
{'sdsdd': 'hahahahahah', 'eins': 'hahahahahah', 'dsdd': 'hahahahahah'}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值