公共方法
文章目录
① 公共操作(运算符、公共函数)
1、 运算符
运算符 | 描述 | 支持的容器类型 |
---|---|---|
+ | 合并 | 字符串、列表、元组 |
* | 复制 | 字符串、列表、元组 |
in | 元素是否存在 | 字符串、列表、元组、字典 |
not in | 元素是否不存在 | 字符串、列表、元组、字典 |
1.1、+ 合并
实例:
str1 = 'aa'
str2 = 'bb'
list1 = [1,2]
list2 = [10,20]
t1 = (1,2)
t2 = (10,20)
dict1 = {'name':'Tom'}
dict2 = {'age':30}
# 合并
print(str1 + str2) # 字符串:aabb
print(list1 + list2) # 列表:[1,2,10,20]
print(t1 + t2) # 元组:(1,2,10,20)
-------------------------------------------------------
# 输出结果
aabb
[1, 2, 10, 20]
(1, 2, 10, 20)
注意:字典
不支持
使用运算符+
,否则会报错。
1.2、* 复制
实例:
str1 = 'a'
list1 = ['hello']
t1 = ('world',)
# *:复制
print(str1 * 5) # 字符串
print(list1 * 5) # 列表
print(t1 * 5) # 元组
# 打印10个-
print('-' * 10)
-------------------------------------------------------
# 输出结果
aaaaa
['hello', 'hello', 'hello', 'hello', 'hello']
('world', 'world', 'world', 'world', 'world')
----------
注意:字典
不支持
使用运算符*
,否则会报错。
1.3、判断数据是否存在
实例:
# in 和 not in
str1 = 'abcd'
list1 = [10,20,30,40]
t1 = (100,200,300,400)
dict1 = {'name':'Python','age':18}
# 1. 字符a 是否存在
print('a' in str1) # True
print('a' not in str1) # False
# 2. 数据10是否存在
print(10 in list1) # True
print(10 not in list1) # False
# 3. 元组里100是否存在
print(100 in t1) # True
print(100 not in t1) # False
# 4. 字典name是否存在
print('name' in dict1)
print('name' not in dict1)
print('name' in dict1.keys())
print('name' in dict1.values())
-------------------------------------------------------
# 输出结果
# 字符
True
False
# 列表
True
False
# 元组
True
False
# 字典
True
False
True
False
2、 公共函数
函数 | 描述 |
---|---|
len() | 计算容器元素个数 |
del 或 del() | 删除 |
max() | 返回容器元素最大值 |
min() | 返回容器元素最小值 |
range(strat,end,step) | 生成从start 到end 的数字,步长 为step,供应for循环使用 |
enumerate() | 函数用于将一个可遍历 的数据对象(如列表、元组或字符串)组合成一个索引序列,同时列出数据和数据下标,一般用在for循环 中。 |
2.1、len 计算元素个数
实例:
str1 = 'abcd'
list1 = [10,20,30,40]
t1 = (100,200,300,400)
dict1 = {'name':'Python','age':18}
print(len(str)) # 字符串:4
print(len(dict1)) # 字典:2
-------------------------------------------------------
# 输出结果
4
2
2.2、del 删除
实例:
str1 = 'abcd'
list1 = [10,20,30,40]
t1 = (100,200,300,400)
dict1 = {'name':'Python','age':18}
# 字典
del dict1['name']
# 列表
del list1[1]
print(list1)
# 删除整个列表
del list1
print(list1)
-------------------------------------------------------
# 输出结果
{'age': 18}
[10, 30, 40]
Traceback (most recent call last):
File "F:\Feature_Detection\test.py", line 15, in <module>
print(list1)
NameError: name 'list1' is not defined
2.3、max min 最大值和最小值
实例:
str1 = 'abcdefg'
list1 = [10,20,30,40,50,60]
# max() :最大值
print(max(str1)) # g
print(max(list1)) # 60
# min() :最小值
print(min(str1)) # a
print(min(list1)) # 10
-------------------------------------------------------
# 输出结果
g
60
a
10
2.4、range 生成序列
# 1 2 3 4 5 6 7 8 9
for i in range(1,10,1):
print(i)
# 1 3 5 7 9
for i in range(1,10,2):
print(i)
# 0 1 2 3 4 5 6 7 8 9
for i in range(10): # 不指定start从0开始
print(i)
注意:range()生成的
序列
不包含end
2.5、enumerate 生成索引序列
-
函数用于将一个
可遍历
的数据对象(如列表、元组或字符串)组合成一个索引序列
,同时列出数据
和数据下标
,一般用在for循环
中。语法:
enumerate(可遍历对象,start=0)
注意:start参数用来设置
遍历数据
的下标
的起始值
,默认是0
实例:
list1 = ['a','b','c','d','e'] for i in enumerate(list1, start =1): print(i) ------------------------------------------------------- # 输出结果 (1, 'a') (2, 'b') (3, 'c') (4, 'd') (5, 'e')