学习笔记-190405-python内置数据结构(tuple+set+dict)

元组-tuple

  • 元组可以看成是一个不可更改的list
# 创建空元组
t = ()
print(type(t))
print("*" * 20)

# 创建一个只有一个值的元组
t = (1) # 此时是整型
print(type(t))
print(t)
print("*" * 20)

t = (1,)
print(type(t))
print(t)
print("*" * 20)

t = 1,
print(type(t))
print(t)
print("*" * 20)

t = (1,2,3,4,5)
print(type(t))
print(t)
print("*" * 20)

t = 1,2,3,4,5
print(type(t))
print(t)
print("*" * 20)

# 使用其他结构创建
l = [1,2,3,4,5]
t = tuple(l)
print(type(t))
print(t)
运行结果:
<class 'tuple'>
********************
<class 'int'>
1
********************
<class 'tuple'>
(1,)
********************
<class 'tuple'>
(1,)
********************
<class 'tuple'>
(1, 2, 3, 4, 5)
********************
<class 'tuple'>
(1, 2, 3, 4, 5)
********************
<class 'tuple'>
(1, 2, 3, 4, 5)

元组的特性

  • 是序列表,有序
  • 元组数据值可以访问,不能修改
  • 元组数据可以是任意类型
  • 总之,list所有特性,除了可修改外,元组都具有
  • 也就意味着,list具有的一些操作,比如索引,切片,序列相加,相乘,成员资格操作等,一模一样

索引操作

t = 1,2,3,4,5
print(t[4])
运行结果:
5
t = 1,2,3,4,5
t1 = t[1::2]
print(t1)
运行结果:
(2, 4)

序列相加

t1 = (1,2,3)
t2 = (5,6,7)
print(id(t1))
print(t1)
# 传址操作
t1 = t1 + t2
print(id(t1))
print(t1)

# 以上操作,类似于
t1 = (1,2,3)
t1 = (1,2,3,4,5,6,7)

# tuple的不可修改,指的是内容的不可修改
运行结果:
83351880
(1, 2, 3)
56241224
(1, 2, 3, 5, 6, 7)

元组相乘

t = (1,2,3)
t = t * 3
print(t)
运行结果:
(1, 2, 3, 1, 2, 3, 1, 2, 3)

成员检测

t = (1,2,3)
if 2 in t:
    print('yes')
else:
    print('no')
运行结果:
yes

元组遍历,一般采用for

# 单层元组遍历
t = (1,2,3,'love','zhangsan')
for i in t:
    print(i, end=" ")
运行结果:
1 2 3 love zhangsan 
# 双层元组的遍历
t = ((1,2,3), (2,3,4),('i', 'love', 'zhangsan'))
for i in t:
    print(i)
    
for k,v,m in t:
    print(k, "---", v,"---", m)
运行结果:
(1, 2, 3)
(2, 3, 4)
('i', 'love', 'zhangsan')
1 --- 2 --- 3
2 --- 3 --- 4
i --- love --- zhangsan

关于元组的函数

# len: 获取元组的长度
t = (1,2,3,4,5)
print(len(t))

# max,min:最大最小值
print(max(t))
print(min(t))
运行结果:
5
5
1
# tuple: 转化或创建元组
l = [1,2,3,4,5]
t = tuple(l)
print(t)

t = tuple()
print(t)
运行结果:
(1, 2, 3, 4, 5)
()

元组的函数

  • 基本跟list通用
# count
t = (2,6,5,7,8,6,5,1,2,6,2,3,4,5,6,2,4,5,3,6,)
print(t.count(2))

# index : 求指定元素在元组中的索引位置
print(t.index(7))
运行结果:
4
3

元组变量交换法

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

a,b = b,a
print(a)
print(b)
运行结果:
3
1

集合-set

  • 一堆确定的无序的唯一的数据,集合中每一个数据成为一个元素
# 集合的定义
s = set()
print(type(s))
print(s)

# 此时,大括号内一定要有值,否则定义出的是一个dict
s = {1,2,3,4,5,6}
print(type(s))
print(s)
运行结果:
<class 'set'>
set()
<class 'set'>
{1, 2, 3, 4, 5, 6}

如果只是用大括号定义,则定义的是一个dict类型

d = {}
print(type(d))
print(d)
运行结果:
<class 'dict'>
{}

集合的特性

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

集合序列操作

# 成员检测
# in, not in
s = {1,2,3, 'I', 'love', 'lisi'}
print(s)

if 'love' in s:
    print('爱')
    
if 'haha' not in s:
    print("哈哈")
运行结果:
{1, 2, 3, 'I', 'love', 'lisi'}
爱
哈哈

集合的遍历操作

# for 循环
s = {1,2,3, 'I', 'love', 'lisi'}

for i in s:
    print(i, end=" ")
运行结果:
1 2 3 I love lisi 
# 带有元组的集合遍历
s = {(1,2,3), (4,5,6), ("I", "love", "lisi")}

for k, v, m in s:
    print(k, "---", v, "---", m)
    
for k in s:
    print(k)
运行结果:
I --- love --- lisi
4 --- 5 --- 6
1 --- 2 --- 3
('I', 'love', 'lisi')
(4, 5, 6)
(1, 2, 3)

集合的内涵

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

# 普通集合内涵
ss = {i for i in s}
print(ss)

# 带条件的集合内涵
sss = {i for i in s if i % 2 == 0}
print(sss)
运行结果:
{1, 2, 3, 4, 5, 6}
{1, 2, 3, 4, 5, 6}
{2, 4, 6}
# 多循环的集合内涵
s1 = {1,2,3,4}
s2 = {'I', 'love', 'lisi'}

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)
运行结果:
{'lisilisi', 'I', 'II', 'lisilisilisilisi', 'lovelove', 'love', 'lisi', 'lovelovelove', 'III', 'lisilisilisi', 'lovelovelovelove', 'IIII'}
{'lisilisi', 'lovelove', 'II'}

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

# len,max,min
s = {2,3,5,4,6,5,1,2}
print(len(s)) #去重后剩余1,2,3,4,5,6,共6个
print(max(s))
print(min(s))
运行结果:
6
6
1

set: 生成一个集合

l = [1,2,3,4,63,32,1,65,4,5,1,2,3,5,63]
s = set(l)
print(s)
运行结果:
{32, 1, 2, 3, 4, 65, 5, 63}

add: 向集合添加元素

s = {1}
s.add(222)
print(s)
运行结果:
{1, 222}

clear

s = {1,2,3,4,5}
print(id(s))
s.clear()
print(id(s))
print(s)
运行结果:
83385704
83385704
set()

copy: 拷贝

s = {1,2,3,4,5,6}
s1 = s.copy()
print(s1)
运行结果:
{1, 2, 3, 4, 5, 6}

remove:移除指定的值,直接改变原有值,如果要删除的值不存在,报错

discard:移除集合中指定的值,跟remove一样,但是如果要删除的值不存在,不报错

s = {1,2,3,4,5,6}
s.remove(4)
print(s)
s.discard(3)
print(s)
print("*" * 20)
s.discard(8)
print(s)
s.remove(8)
print(s)

运行结果:
学习笔记-190405-python内置数据结构(tuple+set+dict)

pop 随机移除一个元素

s = {1,2,3,4,5,6,7}
d = s.pop()
print(d)
print(s)
运行结果:
1
{2, 3, 4, 5, 6, 7}

集合函数

  • intersection:交集
  • difference: 差集
  • union:并集
  • issubset:检查一个集合是否为另一个子集
  • issuperset:检查一个集合是否另一个超集
s1 = {1,2,3,4,5,6}
s2 = {5,6,7,8,9}
s_1 = s1.intersection(s2)
print(s_1)

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

s_3 = s1.union(s2)
print(s_3)

s_4 = s1.issubset(s2)
print(s_4)
运行结果:
{5, 6}
{1, 2, 3, 4}
{1, 2, 3, 4, 5, 6, 7, 8, 9}
False

集合的数学操作

s1 = {1,2,3,4,5,6}
s2 = {5,6,7,8,9}

s_1 = s1 - s2
print(s_1)

s_2 = s1 + s2
print(s_2)

运行结果:
学习笔记-190405-python内置数据结构(tuple+set+dict)

frozen set:冰冻集合

  • 冰冻集合就是不可以进行任何修改的集合
  • frozenset是一种特殊集合
# 冰冻集合创建
s = frozenset()
print(type(s))
print(s)
运行结果:
<class 'frozenset'>
frozenset()

dict字典

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

# 创建空字典2
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)

# 用dict创建有内容字典3
# 利用元组
d = dict([("one",1), ("two",2), ("three",3)])
print(d)
运行结果:
<class 'dict'>
{}
{}
{'one': 1, 'two': 2, 'three': 3}
{'one': 1, 'two': 2, 'three': 3}
{'one': 1, 'two': 2, 'three': 3}
{'one': 1, 'two': 2, 'three': 3}

字典的特征

  • 字典是序列类型,但是是无序序列,所以没有切片和索引
  • 字典中的数据每个都由键值对组成,即kv对
    • key:必须是课哈希的值,比如int,string,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)

# 加限制条件的字典生成式
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:清空字典
d = {"one":1, "two":2, "three":3}
d.clear()
print(d)
运行结果:
{}

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}
k = d.keys()
print(type(k))
print(k)
运行结果:
<class 'dict_keys'>
dict_keys(['one', 'two', 'three'])

values: 同理,一个可迭代的结构

d = {"one":1, "two":2, "three":3}
v = d.values()
print(type(v))
print(v)
运行结果:
<class 'dict_values'>
dict_values([1, 2, 3])

get: 根据指定键返回相应的值,好处是,可以设置默认值

d = {"one":1, "two":2, "three":3}
print(d.get('on'))
# get默认值是None,可以设置
print(d.get('one'))
print(d.get('on',100))
运行结果:
None
1
100

formkeys: 使用指定的序列作为键,使用一个值作为字典的所有的键的值

l = ["1", "2", "3"]
# 注意fromkeys两个参数的类型
# 注意fromkeys的调用主体
d = dict.fromkeys(l,"hahaha")
print(d)
运行结果:
{'1': 'hahaha', '2': 'hahaha', '3': 'hahaha'}