Python数据序列的公共方法与函数、以及推导式

1、公共方法

1.1    ‘+’

  • 加法运算适用于所有的基础数据类型(int float bool)

  • 加法运算所有两侧要是同种数据类型

  • 加法运算再容器类型中是拼接的意思,不是相加计算值

# +法运算,都可以用于哪些数据类型之间
# int float  bool 肯定可以用于加法运算,不再赘述
print(1 + 12.3)  # 13.3

# str 可以相加么? 可以
str1 = 'hello'
str2 = ' python'
# 字符串相加,可以快速将字符串进行拼接
print(str1 + str2)
# 相加过后,str1 和str2 没有发生变化,可以推断+法运算产生了新的数据,源数据不变化
print(str1, str2, sep='\n')

# list 可以相加么? 可以
list1 = [1, 2, 3]
list2 = [4, 5, 6]
# 列表相加可以将列表进行拼接,效果类似于extend
print(list1 + list2)  # [1, 2, 3, 4, 5, 6]
# list相加后,原数据不发生变化,将产生一个新的数据
print(list1)  # [1, 2, 3]
print(list2)  # [4, 5, 6]

# tuple 可以相加么?  可以
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
print(tuple1 + tuple2)  # (1, 2, 3, 4, 5, 6)
# 由于元组内部元素不能修改,索引相加肯定没有对源数据产生影响,而是生成了新的数据

# set
# set1 = {1, 2, 3}
# set2 = {4, 5, 6}
# # TypeError: unsupported operand type(s) for +: 'set' and 'set'
# # set之间不能进行加法运算
# print(set1 + set2)

# dict
# TypeError: unsupported operand type(s) for +: 'dict' and 'dict'
# dict 类型间不能进行加法运算
# dict1 = {'name': '小明'}
# dict2 = {'age':18}
# print(dict1 + dict2)

# 结论: 基础数据类型都可以进行加法运算,容器类型间只有列表,元组,字符串可以进行加法运算


# 不同容器类型间可以相加么?
list1 = [1,2,3]
tuple1 = (1,2,3)
set1 = {1,2,3}
dict1 = {'name': 'xiaoming'}

# TypeError: can only concatenate list (not "tuple") to list
# print(list1 + tuple1)
# TypeError: can only concatenate list (not "set") to list
# print(list1 + set1)
# TypeError: can only concatenate tuple (not "set") to tuple
print(tuple1 + set1)

# 结论,数据类型布偶无法进行加法运算(特指容器类型之间)

1.2   ‘*’

  • 基础数据类型(int float bool)都可以进行乘法运算

  • 容器类型只能和int类型数据进行乘法运算

  • 容器类型进行乘法运算,就是将容器复制指定次数,并进行拼接

# * 效果是设么?
# * 什么容器类型可以使用*

# 基础数据类型  int  float  bool都可以使用*法运算
print(12.1 * 2)

# 容器类型的乘法运算
# 格式: 容器类型 * int类型数据
# 乘法运算的 效果,就是讲容器类型复制指定次数,并拼接到一起

# list 可以使用*法运算么?  可以
list1 = [1, 2, 3]
# 将list1 复制3次并进行拼接
print(list1 * 3)  # [1, 2, 3, 1, 2, 3, 1, 2, 3]
# 使用list 类型乘以float类型可以实现么?
# TypeError: can't multiply sequence by non-int of type 'float'
# 乘法运算不能让容器与非int类型相乘
# print(list1 * 1.3)
# list 能乘以负数么?  可以相乘,但是结果为空列表
print(list1 * -3)  # []
# 可以与0 相乘,结果为空列表
print(list1 * 0)  # []

# tuple 可以使用*法运算么? 可以
tuple1 = (1, 2, 3, 4)
print(tuple1 * 2)  # (1, 2, 3, 4, 1, 2, 3, 4)
# tuple 只能与int类型相乘


# set可以使用 * 法运算么?  不可以
set1 = {1, 2, 3}
# TypeError: unsupported operand type(s) for *: 'set' and 'int'
# 集合类型数据不能做乘法运算
# print(set1 * 3)

# dict 能够使用 * 法运算么?  不可以
dict1 = {'name': 'jack'}
# TypeError: unsupported operand type(s) for *: 'dict' and 'int'
# 字典不能做乘法运算
# print(dict1 * 3)

# 乘号左右两侧的数据可以互换位置么?  可以
print(3 * list1)  # [1, 2, 3, 1, 2, 3, 1, 2, 3]

1.3  in 和 not in

  • 判断元素是否在数据序列当中

  • 字符串,列表,元组,集合 ,字典都可以使用

# in 判断元素是否存在于容器当中
list1 = [1, 2, 3]
tuple1 = (1, 2, 3)
set1 = {1, 2, 3}

print(3 in list1)  # True
print(3 in tuple1)  # True
print(3 in set1)  # True
# 如果要判断是否在set当中,要注意被判断的元素必须可以保存在set当中,如果是列表,字典,集合,则不能判断
# print([1, 2] in list1)  # False  可以判断,引为[1,2] 可以保存在list1当中
# print([1, 2] in set1)  # TypeError: unhashable type: 'list' 不能判断,因为[1,2]不能保存到set1当中


# str类型可以使用in么? 可以
str1 = '123'
# TypeError: 'in <string>' requires string as left operand, not int
# 字符串判断时,左侧的元素只能是字符串类型,否则报错
# print(1 in str1)
print('1' in str1)  # True

# dict 是否可以使用in???
dict1 = {'name': 123}
# dict使用in判断的是当前元素是否是dict中存在的键
print(123 in dict1)  # False
print('name' in dict1)  # True

# 如果使用此方法则不能判断字典  列表 集合
# TypeError: unhashable type: 'list'
# print([1,2] in dict1)

# not in  : 所有用法和in相同,只是取反而已

# 结论:
# 1.使用in 和not in  被判断的元素在关键字左侧, 数据序列在关键字右侧
# 2.如果想要判断该元素是否在容器内,该元素必须能保存到容器内,比如集合不能保存列表,字典,集合 所以就不能判断其类型的元素是否在集合内
# 3.字典判断的是元素是否在keys内,也就是是否是其中的键

1.4  切片

  • 通过切片按照规则获取数据序列中的一部分元素

  • tuple list str 可以使用切片

  • dict set 不可以使用切片

# 切片:通过切片方法可以按照一定规则截取容器的一部分数据

# str切片
str1 = 'abcde'
# 格式:[起始位置:终止位置:步长]
# 不会修改原有字符串,而是产生了一个新的字符串
print(str1[2:])  # cde

# list可以切片么?
list1 = [1, 2, 3, 4]
# list切片方式方法和str完全相同
# list切片后不会在原有数据上进行修改,而是产生了一个新的列表
print(list1[1:3:1])  # [2, 3]
print(list1)

# tuple 可以切片么?
tuple1 = (1, 2, 3, 4)
# tuple1切片方式方法和str完全相同
# 切片后不会在原有数据上进行修改,而是产生了一个新的列表
print(tuple1[1:3:1])  # (2, 3)
print(tuple1)

# dict可以切片么?  肯定不行,因为不能使用索引获取数据
# set 可以切片么?  肯定不行,因为不能使用索引获取数据

# 结论:
# 1.list str tuple 可以使用切片,格式是:[起始位置:终止位置:步长],三者使用方式完全一致
# 2.所有的切片都不会在原有的数据上进行修改,而是产生一个新的数据序列
# 3.集合和字典无法切片,因为不能使用索引获取数据元素

2、公共函数

  • len :获取容器内元素个数

  • del:删除容器内元素

  • max :获取容器内数据的最大值

  • min : 获取容器内元素的最小值

  • enumerate : 获取容器内元素时可以携带序号

  • range:根据一定规则获取整数序列

# len  获取容器类型的元素个数,  或者说获取容器的长度
str1 = '123'
list1 = [1, 2, 3]
tuple1 = (1, 2, 3)
set1 = {1, 2, 3}
dict1 = {'name': 123, 'age': 18}
# 使用len可以获取list  str  tuple set中的元素个数
print(len(str1))
print(len(list1))
print(len(tuple1))
print(len(set1))
# 使用len可以获取dict中的键值对的个数
print(len(dict1))

# len() 可以写成  容器.__len__()
print(list1.__len__())

# del
# 删除容器内指定的元素
# list
# del list1[0]
# print(list1)

# tuple
# del tuple1[0]
# TypeError: 'tuple' object doesn't support item deletion
# 元组内元素不能被删除
# print(tuple1)

# set
# for i in set1:
#     del i

# dict
# del dict1['name']
# del 在dict中删除的是键值对
print(dict1)

# str
# TypeError: 'str' object doesn't support item deletion
# str 不能够使用del 删除内部元素
# 注意 :str内部的元素也是不可修改的,类似于元组
# del str1[0]
# print(str1)

# 结论:
# 1.列表,字典可以使用del删除内部元素,但是,列表中是删除元素,字典中是删除键值对
# 2.使用del 没法循环遍历删除set中的元素,因为引用机制问题
# 3.str  tuple内部的元素都不可更改所以不能使用del删除元素


# max  min
# list tuple set str可以使用max  min获取容器内的最大最小值
print(max(list1))
print(max(tuple1))
print(max(set1))
print(max(str1))
# dict是使用max和min获取键的最大最小值
print(max(dict1))

# enumerate  枚举函数:获取容器内数据时添加序号(默认序号从0开始可以作为索引使用)

list2 = [1, 2, 3, 4, 5, 6, 7, 8]

for i in list2:
    print(i)

# 可不可以同时获取元素的值和元素的索引?  可以 使用enumerate

# for i in enumerate(list2):
#     # 直接打印,获取的是以(索引,值)组成的元组
#     print(i)

# list
for index, value in enumerate(list2):
    print(index, value, sep=' : ')

# tuple
for index, value in enumerate(tuple1):
    print(index, value, sep=' : ')

# set
for index, value in enumerate(set1):
    print(index, value, sep=' : ')

# str
for index, value in enumerate(str1):
    print(index, value, sep=' : ')

# dict  
for index, value in enumerate(dict1):
    print(index, value, sep=' : ')
    
# 结论:所有的容器和课迭代类型都可以使用enumerate,并且产生序号,这个序号并不是索引值,而是在生成序号时默认从0开始,碰巧可以在list,str,tuple中当做索引使用

3、推导式

  • 列表推导式

    • 格式:[要插入的值 for 临时变量 in 数据序列 if 条件]

  • 集合推导式

    • 格式:{要插入的值 for 临时变量 in 数据序列 if 条件}

  • 字典推导式

    • 格式:{要插入的键:要插入的值 for 临时变量 in 数据序列 if 条件 }

  • 没有元组推导式和字符串推导式,因为其内部元素无法被修改

# 推导式:通过一定的规则快速构建数据序列
# 列表推导式
# 获取从0 到9的数据序列
# while
list1 = []
i = 0
while i < 10:
    list1.append(i)
    i += 1
print(list1)

# for
list2 = []
for i in range(10):
    list2.append(i)
print(list2)

# 推导式
# 格式: [要插入列表的表达式 for 临时变量 in 数据序列]
list3 = [i for i in range(10)]
print(list3)

# 使用推导式,创建一个从1-100的偶数的数据序列

# for
list4 = []
for i in range(1, 101):
    if i % 2 == 0:
        list4.append(i)

print(list4)

# 推导式
# 格式: [要插入列表的表达式 for 临时变量 in 数据序列 if  条件]
list5 = [i for i in range(1, 101) if i % 2 == 0]
print(list5)

# 练习:
# 用推导式进行九九乘法表的生成,将所有的算式放入列表中
list6 = []
for i in range(1, 10):
    for j in range(1, i + 1):
        list6.append(f'{j} * {i} = {j * i}')

print(list6)

# 改写为推导式:
list7 = [f'{j} * {i} = {j * i}' for i in range(1, 10) for j in range(1, i + 1)]
print(list7)

# 集合推导式
# 集合推导式和列表推导式完全一致,只不过使用推导式时,外层用{}包裹,并且在序列中会去重
set1 = {i for i in range(10)}
print(set1)

# 获取从1-10 的偶数集合
set2 = {i for i in range(1, 11) if i % 2 == 0}
print(set2)

# 字典推导式
keys = ['name', 'age', 'gender', 'id']
values = ['xiaoming', 18, '女', '001']

# 需求想将key 和value以一对应,形成一个字典
dict1 = {}
for i in range(len(keys)):
    dict1[keys[i]] = values[i]

print(dict1)

# 改写推导式
# 格式:{要插入的键:要插入的值  for 临时变量 in 数据序列  if 条件}
dict2 = {keys[i]: values[i] for i in range(len(keys))}
print(dict2)

# 所有的推导式都可以使用for循环改写,所以我们进行推导式的时候先不要急于求成,多改写几次就不用再改写了直接可以写出推导式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值