4,元组

元组是一个有序且不可变的容器,可以存放多个不同类型的元素

4.1定义

tuple = (1,2,3,4,[1,2,3,4])
#这个元组不能改变,里面的列表也不能更换,但是可以给列表增加元素
# 面试题
# 1. 比较值 v1 = (1) 和 v2 = 1 和 v3 = (1,) 有什么区别?
v1 = (1)
print(v1)
print(type(v1))

v2 = 1
print(v2)
print(type(v2))

v3 = (1,)
print(v3)
print(type(v3))
#v1和v2都是整型,v3是元组

# 2. 比较值 v1 = ( (1),(2),(3) ) 和 v2 = ( (1,) , (2,) , (3,),) 有什么区别?
v1 = ( (1),(2),(3) )
print(v1)
print(type(v1))
#区别:输出的结果不同,v1输出的结果是整型,v2输出的结果是元组
v2 = ( (1,) , (2,) , (3,),)
print(v2)
print(type(v2))

4.2公共功能

1,相加,两个元组相加生成一个新的元组

t2 = ("赵","刘") + ("宋","范")
print(t2) # ("赵","刘","宋","范")

t = ("赵","刘")
t1 = ("宋","范")
t3 = t1 + t
print(t3) # ("赵","刘","宋","范")

2,相乘,元组*整型,将元组中的元素再创建N份并生成一个新的列表

t = ("赵","刘") * 2
print(t) # ("赵","刘","赵","刘")

t1 = ("赵","刘")
t2 = t1 * 2
print(t1) # ("赵","刘")
print(t2) # ("赵","刘","赵","刘")

3,获取长度

tuple = ("范","刘",'尼古拉斯',)
print( len(tuple) )

4,索引

t = ("范","刘",'尼古拉斯',)
print( t[0] )
print( t[2] )
print( t[3] )   #报错,超出索引范围

5,切片

t = ("范","刘",'尼古拉斯',)
print( t[0:2] )
print( t[1:] )
print( t[:-1] )

6,步长

t = ("范","刘",'尼古拉斯',"宋","刘")
print( t[1:4:2] )
print( t[0::2] )
print( t[1::2] )
print( t[4:1:-1] )
#所有的步长产生的都是新的数据
# 字符串 & 元组只能通过步长来反转

t = ("范","刘",'尼古拉斯',"宋","刘")
data = t[::-1]

# 列的两种反转方式
user_list = ["范","刘",'尼古拉斯',"宋","刘"]
data = user_list[::-1]

user_list.reverse()
print(user_list)

7,for循环

t = ("范","刘",'尼古拉斯',"宋","刘")

for item in t:
    print(item)

t = ("范","刘",'尼古拉斯',"宋","刘")

for item in t:
 if item == '刘':
     continue
 print(name)

目前:只有 str、list、tuple 可以被for循环。 "xxx" [11,22,33] (111,22,33)

# len + range + for + 索引
t = ("范","刘",'尼古拉斯',"宋","刘")

for index in range(len(t)):
    item = t[index]
    print(item)

4.3,转换

只有字符串和列表可以转换为元组

# data = tuple(str/list)

name = "木木木"
data = tuple(name)
print(data) # 输出 ("木","木","木")

name = ["木木",18,"python"]
data = tuple(name)

print(data) # 输出 ("木木",18,"pythonav")

4.4其他

tu = ( '1', '2', ('3','4') )
tu1 = tu[0]
tu2 = tu[1]
tu3 = tu[2][0]
tu4 = tu[2][1]
tu5 = tu[2][1][3]

print(tu1) # 1
print(tu2) # 2
print(tu3) # 3
print(tu4) # 4

5,集合

5.1定义

集合是一个无序,可变,不允许数据重复的容器

s1 = { 11, 22, 33, "mm" }

无序,无法通过索引取值

可变,可以添加和删除元素

不允许数据重复

注意!:

定义空集合时,只能用a = set(),不能用a = {}

#定义空列表
l1 = []
l2 = list()

#定义空元组
t1 = ()
t2 = tuple()

#定义空集合
s1 = set()

#定义空字典
d1 = {}
d2 = dict()

5.2独有功能

1,添加元素

set.add(element)

2,删除元素

set.discard(element)

3,交集

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

s3 = s1.intersection(s2)    #取交集方式一
s4 = s1 & s2                #取交集方式二
print(s3,s4)

4,并集

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

s3 = s1.union(s2)           #取并集方式一
s4 = s1 | s2                #取并集方式二
print(s3,s4)

5,差集

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

s3 = s1.difference(s2)          #方式一,s1中有且s2中没有的值
s4 = s1 - s2                    #方式二,s1中有且s2中没有的值
print(s3,s4)

s5 = s2 - s1                    #方式一:差集,s2中有且s1中没有的值
s6 = s2.difference(s1)          #方式二:差集,s2中有且s1中没有的值

5.3公共功能

1,减,计算差集

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

s4 = s1 - s2    
s5 = s2 - s1    
print(s4, s5)

2,&计算交集

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

s4 = s1 & s2                #取交集方式二
print(s4)

3, | 计算并集

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

s4 = s1 | s2                
print(s4)

4,长度

s1 = {1,2,3,4,5}
data = len(s1)

print(data)

5,for循环

s1 = {1,2,3,4,5}

for data in s1:
    print(data)

5.4类型转换

lis1 = [1,2,2,3,44,5]
l = set(lis1)

print(l)
#{1,2,3,44,5},转换为集合会去重

5.5其他

5.5.1集合存储原理

v = set()
v.add("1")

1,利用哈希函数把“1”转换成一个数值

2,对这个数值取余数

3,将元素放在哈希表的第(余数)个索引位置

5.5.2元素必须可哈希

可哈希的数据类型:int,bool,str,tuple,

不可哈希的数据类型:set,list

#判断方法
v = hash("1")
print(v)

5.5.3查找速度快

因存储原理特殊,集合的查找效率非常高(数据量大了才明显)。

user_list = ["小明","mm","呼叫"]

if "alex" in user_list:
    print("在")
else:
    print("不在")
    
    
user_tuple = ("小明","mm","呼叫")

if "alex" in user_tuple:
    print("在")
else:
    print("不在")
  • 效率高
user_set = {"小明","mm","呼叫"}

if "alex" in user_set:
    print("在")
else:
    print("不在")

5.5.4 对比和嵌套

类型

是否可变

是否有序

元素要求

是否可哈希

转换

定义空

list





list(其他)

v=[]或v=list()

tuple





tuple(其他)

v=()或v=tuple()

set



可哈希


set(其他)

v=set()

data_list = [
    "mm",
    11,
    (11, 22, 33, {"alex", "eric"}, 22),
    [11, 22, 33, 22],
    {11, 22, (True, ["中国", "北京"], "稍等哈"), 33}
]

注意:由于True和False本质上存储的是 1 和 0 ,而集合又不允许重复,所以在整数 0、1和False、True出现在集合中会有如下现象:

v1 = {True, 1}
print(v1)  # {True}

v2 = {1, True}
print(v2)  # {1}

v3 = {0, False}
print(v3)  # {0}

v4 = {False, 0}
print(v4)  # {False}

None

1,节省空间

v1 = None
V2 = None
#...
v1 = [1,2,3]
v2 = [4,5,6]

2,None转换为布尔值为False