python operator模块及其itemgetter的详细使用

本文介绍了Python的operator模块,包括数字、字符串和列表的比较运算,以及operator.itemgetter的使用,如取值和用于列表、元组、字典的排序。operator模块提供了许多内建操作的函数版本,方便在编程中进行比较和计算操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

一、运算

1. 对数字、字符串及列表进行比较

2. 对数字进行计算

二、operator下的itemgetter使用

1. 取值

2. 排序


一、运算

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更多方法:

运算

语法

函数

加法

a + b

add(a, b)

字符串拼接

seq1 + seq2

concat(seq1, seq2)

包含测试

obj in seq

contains(seq, obj)

除法

a / b

truediv(a, b)

除法

a // b

floordiv(a, b)

按位与

a & b

and_(a, b)

按位异或

a ^ b

xor(a, b)

按位取反

~ a

invert(a)

按位或

a | b

or_(a, b)

取幂

a ** b

pow(a, b)

标识

a is b

is_(a, b)

标识

a is not b

is_not(a, b)

索引赋值

obj[k] = v

setitem(obj, k, v)

索引删除

del obj[k]

delitem(obj, k)

索引取值

obj[k]

getitem(obj, k)

左移

a << b

lshift(a, b)

取模

a % b

mod(a, b)

乘法

a * b

mul(a, b)

矩阵乘法

a @ b

matmul(a, b)

取反(算术)

- a

neg(a)

取反(逻辑)

not a

not_(a)

正数

+ a

pos(a)

右移

a >> b

rshift(a, b)

切片赋值

seq[i:j] = values

setitem(seq, slice(i, j), values)

切片删除

del seq[i:j]

delitem(seq, slice(i, j))

切片取值

seq[i:j]

getitem(seq, slice(i, j))

字符串格式化

s % obj

mod(s, obj)

减法

a - b

sub(a, b)

真值测试

obj

truth(obj)

比较

a < b

lt(a, b)

比较

a <= b

le(a, b)

相等

a == b

eq(a, b)

不等

a != b

ne(a, b)

比较

a >= b

ge(a, b)

比较

a > b

gt(a, b)

 参考:Python3 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)来进行取值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

湿透剪自布

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值