运算符
公共方法
容器类型转换
一、运算符
运算符 | 描述 | 支持的容器类型 |
---|---|---|
+ | 合并 | 字符串、列表、元组 |
* | 复制 | 字符串、列表、元组 |
in | 元素是否存在 | 字符串、列表、元组、字典 |
not in | 元素是否不存在 | 字符串、列表、元组、字典 |
+ 号,只支持字符串、列表、元组,不支持字典
str1 = ('aa', 'bb', 'cc')
str2 = ('cc', 'dd', 'ee')
tuple1 = (1, 3, 4, 5)
tuple2 = (4, 5, 6, 8)
list1 = [1, 2, 3, 4, 5]
list2 = [6, 7, 8, 9, 10]
dict1 = {'name': 'test', 'sex': '男'}
dict2 = {'age': 23}
# + 合并的含义
print(str1 + str2)
# 输出 ('aa', 'bb', 'cc', 'cc', 'dd', 'ee')
print(tuple1 + tuple2)
# 输出 (1, 3, 4, 5, 4, 5, 6, 8)
print(list1 + list2)
# 输出[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
* 号 ,只支持字符串、列表、元组,不支持字典
str1 = 'a'
list1 = ['hello']
t1 = ('world',)
new_str1 = str1 * 3
print(new_str1)
# 输出 aaa
new_list1 = list1 * 5
print(new_list1)
# 输出 ['hello', 'hello', 'hello', 'hello', 'hello']
new_t1 = t1 * 4
print(new_t1)
# 输出('world', 'world', 'world', 'world')
in,元素是否存在,not in 元素是否不存在
支持字符串、列表、元组、字典
str1 = 'abcdsd'
list1 = ['1', '2', '3', '4']
t1 = (1, 2, 3, 4)
dict1 = {'name': 'test', 'age': 23}
print('a' in str1)
print('1' in str1)
print('2' in list1)
print(3 in t1)
print('name' in dict1)
二、公共方法
len() 统计序列中元素的个数
del或者del() 删除
max() 返回容器中的元素最大值
min()返回容器中的元素最小值
range(start, end ,step) 生成从start 到end的数字,步长为step,供for循环使用
enumerate()函数用于将一个可遍历的对象(如列表、元组、或字符串)组合为一个索引序列,同时列出数据和数据下标。
len()
str1 = 'abcdef'
tuple1 = (1, 2, 3, 4, 5)
list1 = [2, 3, 4, 5, 4]
s1 = {1, 2, 4, 8, 4}
dict1 = {"name": 'test', 'age': 12, 'sex': "男"}
print(len(str1))
print(len(tuple1))
print(len(list1))
print(len(s1))
print(len(dict1))
del()
str1 = 'abcdef'
tuple1 = (1, 2, 3, 4, 5)
list1 = [2, 3, 4, 5, 4]
s1 = {1, 2, 4, 8, 4}
dict1 = {"name": 'test', 'age': 12, 'sex': "男"}
# del 目标或del(目标)
# del str1
# print(str1)
del(list1[0])
print(list1)
del(dict1)
print(dict1)
max()和min()
str1 = 'abcdef'
tuple1 = (1, 2, 3, 4, 5)
list1 = [2, 3, 4, 5, 4]
s1 = {1, 2, 4, 8, 4}
# max 最大值
print(max(str1))
print(max(list1))
print(max(s1))
# min 最小值
print(min(str1))
print(min(tuple1))
print(min(list1))
print(min(s1))
range(start, end ,step)
注意生成的序列,包含左侧的值,不包含右侧的值,同切片类似,如果不写开始,默认从0开始,如果不写步长,默认为1。同for 一起使用
# range 生成从start到end数字,步长为step,供for循环
# for i in range(1, 10, 2):
# print(i)
# 输出
# 1
# 3
# 5
# 7
# 9
for i in range(1, 10):
print(i)
# 输出
# 1
# 2
# 3
# 4
# 5
# 6
# 7
# 8
# 9
for i in range(10):
print(i)
# 输出
# 0 1 2 3 4 5 6 7 8 9
enumerate()
语法
enumerate(可遍历对象,start=0)
注意,start参数用来设置遍历数据的起始值,默认为0
# enumerate(可遍历对象,start=0)
list1 = {'a', 'b', 'c', 'd', 'e', 'f'}
for i in enumerate(list1, 3):
print(i)
# 输出
# (3, 'f')
# (4, 'a')
# (5, 'c')
# (6, 'd')
# (7, 'e')
# (8, 'b')
三、容器类型转化
3.1 tuple()
将某个序列转换成元组
tuple1 = (1, 2, 3, 4, 5)
list1 = ['a', 'b', 'c', 'd']
set1 = {100, 200, 300, 400}
# tuple
print(tuple(list1))
print(tuple(set1))
# 输出
# ('a', 'b', 'c', 'd')
# (200, 100, 400, 300)
3.2 list()
将某个序列转成列表
tuple1 = (1, 2, 3, 4, 5)
list1 = ['a', 'b', 'c', 'd']
set1 = {100, 200, 300, 400}
# list1
print(list(tuple1))
print(list(set1))
# 输出
# [1, 2, 3, 4, 5]
# [200, 100, 400, 300]
3.3 set()
将某个序列转成集合,集合会自动去重
tuple1 = (1, 2, 3, 4, 5)
list1 = ['a', 'b', 'c', 'd']
set1 = {100, 200, 300, 400}
# set 集合会自动去重
print(set(tuple1))
print(set(list1))
# 输出
# {1, 2, 3, 4, 5}
# {'a', 'c', 'd', 'b'}