目录
一、运算
1. 对数字、字符串及列表进行比较
# 导入 operator 模块
import operator
# 数字
x = 10
y = 20
print("x:",x, ", y:",y)
print("operator.lt(x,y): ", operator.lt(x,y))
print("operator.gt(y,x): ", operator.gt(y,x))
print("operator.eq(x,x): ", operator.eq(x,x))
print("operator.ne(y,y): ", operator.ne(y,y))
print("operator.le(x,y): ", operator.le(x,y))
print("operator.ge(y,x): ", operator.ge(y,x))
print()
# 字符串
x = "Google"
y = "Runoob"
print("x:",x, ", y:",y)
print("operator.lt(x,y): ", operator.lt(x,y))
print("operator.gt(y,x): ", operator.gt(y,x))
print("operator.eq(x,x): ", operator.eq(x,x))
print("operator.ne(y,y): ", operator.ne(y,y))
print("operator.le(x,y): ", operator.le(x,y))
print("operator.ge(y,x): ", operator.ge(y,x))
print()
# 查看返回值
print("type((operator.lt(x,y)): ", type(operator.lt(x,y)))
# 列表
a = [1, 2]
b = [2, 3]
c = [2, 3]
print("operator.eq(a,b): ", operator.eq(a, b))
print("operator.eq(c,b): ", operator.eq(c, b))
输出:
x: 10 , y: 20
operator.lt(x,y): True
operator.gt(y,x): True
operator.eq(x,x): True
operator.ne(y,y): False
operator.le(x,y): True
operator.ge(y,x): True
x: Google , y: Runoob
operator.lt(x,y): True
operator.gt(y,x): True
operator.eq(x,x): True
operator.ne(y,y): False
operator.le(x,y): True
operator.ge(y,x): True
type((operator.lt(x,y)): <class 'bool'>
operator.eq(a,b): False
operator.eq(c,b): True
Process finished with exit code 0
2. 对数字进行计算
# 导入 operator 模块
import operator
# 初始化变量
a = 4
b = 3
# 使用 add() 让两个值相加
print ("add() 运算结果 :",end="");
print (operator.add(a, b))
# 使用 sub() 让两个值相减
print ("sub() 运算结果 :",end="");
print (operator.sub(a, b))
# 使用 mul() 让两个值相乘
print ("mul() 运算结果 :",end="");
print (operator.mul(a, b))
输出:
add() 运算结果 :7
sub() 运算结果 :1
mul() 运算结果 :12
operator更多方法:
运算 | 语法 | 函数 |
---|---|---|
加法 |
|
|
字符串拼接 |
|
|
包含测试 |
|
|
除法 |
|
|
除法 |
|
|
按位与 |
|
|
按位异或 |
|
|
按位取反 |
|
|
按位或 |
|
|
取幂 |
|
|
标识 |
|
|
标识 |
|
|
索引赋值 |
|
|
索引删除 |
|
|
索引取值 |
|
|
左移 |
|
|
取模 |
|
|
乘法 |
|
|
矩阵乘法 |
|
|
取反(算术) |
|
|
取反(逻辑) |
|
|
正数 |
|
|
右移 |
|
|
切片赋值 |
|
|
切片删除 |
|
|
切片取值 |
|
|
字符串格式化 |
|
|
减法 |
|
|
真值测试 |
|
|
比较 |
|
|
比较 |
|
|
相等 |
|
|
不等 |
|
|
比较 |
|
|
比较 |
|
|
二、operator下的itemgetter使用
1. 取值
对列表、元组、字典进行取值
from operator import itemgetter
list_a = [10, 20, 30, 40] # 定义一个列表
tuple_b = (1, 2, 3, 4) # 定义一个元组
dict_c = {"name": "john", "age": 22, "score": 99} # 定义一个字典
w = itemgetter(0) # 相当于定义了函数w,用来获取对象下标为0的值
x = itemgetter(1, 2)
y = itemgetter("name")
z = itemgetter("name", "score")
# 对列表和元组取下下标为0的值
print(w(list_a))
print(w(tuple_b))
# 对列表和元组取下下标为1和2的值,返回元组
print(x(list_a))
print(x(tuple_b))
# 对字典按key取值
print(y(dict_c))
print(z(dict_c))
输出:
10
1
(20, 30)
(2, 3)
john
('john', 99)
2. 排序
- 对列表嵌套元组进行排序
from operator import itemgetter
tuple_in_list = [("zhao", 22, 99), ("zhang", 33, 88), ("wang", 25, 95), ("zhao", 20, 70)]
# sorted: 临时排序
print(sorted(tuple_in_list, key=itemgetter(1))) # 按元组里下标为1的值来排序
print(sorted(tuple_in_list, key=itemgetter(0, 1))) # 先按元组里下标为0的值排序,再按下标为1的值排序
# sort: 永久排序
tuple_in_list.sort(key=itemgetter(1))
print(tuple_in_list)
输出:
[('zhao', 20, 70), ('zhao', 22, 99), ('wang', 25, 95), ('zhang', 33, 88)]
[('wang', 25, 95), ('zhang', 33, 88), ('zhao', 20, 70), ('zhao', 22, 99)]
[('zhao', 20, 70), ('zhao', 22, 99), ('wang', 25, 95), ('zhang', 33, 88)]
- 对列表嵌套字典进行排序
from operator import itemgetter
dict_in_list = [{"name": "john", "age": 22, "score": 99},
{"name": "gao", "age": 55, "score": 100},
{"name": "gao", "age": 22, "score": 77}]
# sorted: 临时排序
print(sorted(dict_in_list, key=itemgetter("name"))) # 按元组里下标为1的值来排序
print(sorted(dict_in_list, key=itemgetter("name", "age"))) # 先按元组里下标为0的值排序,再按下标为1的值排序
# sort: 永久排序
dict_in_list.sort(key=itemgetter("age"), reverse=True)
print(dict_in_list)
输出:
[{'name': 'gao', 'age': 55, 'score': 100}, {'name': 'gao', 'age': 22, 'score': 77}, {'name': 'john', 'age': 22, 'score': 99}]
[{'name': 'gao', 'age': 22, 'score': 77}, {'name': 'gao', 'age': 55, 'score': 100}, {'name': 'john', 'age': 22, 'score': 99}]
[{'name': 'gao', 'age': 55, 'score': 100}, {'name': 'john', 'age': 22, 'score': 99}, {'name': 'gao', 'age': 22, 'score': 77}]
总结:
operator可以对数字,字符串,列表,元组等进行比较、计算等更多操作
operator.itemgetter 还可以用在排序中,对于列表、元组或列表嵌套元组时itemgetter可指定一个或多个下标进行取值。对于字典或列表嵌套字典时itemgetter可指定一个或多个字典的键(key)来进行取值