- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Python中operator模块的操作符使用示例总结由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
operator模块是python中内置的操作符函数接口,它定义了一些算术和比较内置操作的函数。operator模块是用c实现的,所以执行速度比python代码快.
逻辑操作 。
1
2
3
4
5
6
7
8
9
10
11
12
|
from
operator
import
*
a
=
[
1
,
2
,
3
]
b
=
a
print
'a ='
, a
print
'b ='
, b
print
print
'not_(a) :'
, not_(a)
print
'truth(a) :'
, truth(a)
print
'is_(a, b) :'
, is_(a, b)
print
'is_not(a, b) :'
, is_not(a, b)
|
打印结果:
1
2
3
4
5
6
|
a = [1, 2, 3]
b = [1, 2, 3]
not_(a) : False
truth(a) : True
is_(a, b) : True
is_not(a, b): False
|
可以通过结果知道,operator的一些操作函数与原本的运算是相同的.
比较操作符 operator提供丰富的比较操作.
1
2
3
4
5
6
7
8
|
a
=
3
b
=
5
print
'a ='
, a
print
'b ='
, b
print
for
func
in
(lt, le, eq, ne, ge, gt):
print
'{0}(a, b):'
.
format
(func.__name__), func(a, b)
|
打印结果 。
1
2
3
4
5
6
7
8
9
|
a = 3
b = 5
lt(a, b): True
le(a, b): True
eq(a, b): False
ne(a, b): True
ge(a, b): False
gt(a, b): False
|
这些函数等价于<、<=、==、>=和>的表达式语法.
算术操作符 处理数字的算术操作符也得到支持.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
a, b, c, d
=
-
1
,
2
,
-
3
,
4
print
'a ='
, a
print
'b ='
, b
print
'c ='
, c
print
'd ='
, d
print
'\nPositive/Negative:'
print
'abs(a):'
,
abs
(a)
print
'neg(a):'
, neg(a)
print
'neg(b):'
, neg(b)
print
'pos(a):'
, pos(a)
print
'pos(b):'
, pos(b)
|
打印结果 。
1
2
3
4
5
6
7
8
9
10
11
|
a = -1
b = 2
c = -3
d = 4
Positive/Negative:
abs(a): 1
neg(a): 1
neg(b): -2
pos(a): -1
pos(b): 2
|
abs返回值得绝对值,neg返回(-obj), pos返回(+obj).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
a
=
-
2
b
=
5.0
print
'a ='
, a
print
'b ='
, b
print
'\nArithmetic'
print
'add(a, b) :'
, add(a, b)
print
'div(a, b) :'
, div(a, b)
print
'floordiv(a, b) :'
, floordiv(a, b)
print
'mod(a, b) :'
, mod(a, b)
print
'mul(a, b) :'
, mul(a, b)
print
'pow(a, b) :'
,
pow
(a, b)
print
'sub(a, b) :'
, sub(a, b)
print
'truediv(a, b) :'
, truediv(a, b)
|
打印结果 。
1
2
3
4
5
6
7
8
9
10
11
12
|
a = -2
b = 5.0
Arithmetic
add(a, b) : 3.0
div(a, b) : -0.4
floordiv(a, b) : -1.0
mod(a, b) : 3.0 # 查看负数取模
mul(a, b) : -10.0
pow(a, b) : -32.0
sub(a, b) : -7.0
truediv(a, b) : -0.4
|
mod表示取模, mul 表示相乘,pow是次方, sub表示相减 。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
a
=
2
b
=
6
print
'a ='
, a
print
'b ='
, b
print
'\nBitwise:'
print
'and_(a, b) :'
, and_(a, b)
print
'invert(a) :'
, invert(a)
print
'lshift(a, b) :'
, lshift(a, b)
print
'or_(a, b) :'
, or_(a, b)
print
'rshift(a, b) :'
, rshift(a, b)
print
'xor(a, b) :'
, xor(a, b)
|
打印结果 。
1
2
3
4
5
6
7
8
9
10
|
a = 2
b = 6
Bitwise:
and_(a, b) : 2
invert(a) : -3
lshift(a, b) : 128
or_(a, b) : 6
rshift(a, b) : 0
xor(a, b) : 4
|
and 表示按位与, invert 表示取反操作, lshift表示左位移, or表示按位或, rshift表示右位移,xor表示按位异或.
原地操作符 即in-place操作, x += y 等同于 x = iadd(x, y), 如果复制给其他变量比如z = iadd(x, y)等同与z = x; z += y.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
a
=
3
b
=
4
c
=
[
1
,
2
]
d
=
[
'a'
,
'b'
]
print
'a ='
, a
print
'b ='
, b
print
'c ='
, c
print
'd ='
, d
print
a
=
iadd(a, b)
print
'a = iadd(a, b) =>'
, a
print
c
=
iconcat(c, d)
print
'c = iconcat(c, d) =>'
, c
|
属性和元素的获取方法 operator模块最特别的特性之一就是获取方法的概念,获取方法是运行时构造的一些可回调对象,用来获取对象的属性或序列的内容,获取方法在处理迭代器或生成器序列的时候特别有用,它们引入的开销会大大降低lambda或Python函数的开销.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
from
operator
import
*
class
MyObj(
object
):
def
__init__(
self
, arg):
super
(MyObj,
self
).__init__()
self
.arg
=
arg
def
__repr__(
self
):
return
'MyObj(%s)'
%
self
.arg
objs
=
[MyObj(i)
for
i
in
xrange
(
5
)]
print
"Object:"
, objs
g
=
attrgetter(
"arg"
)
vals
=
[g(i)
for
i
in
objs]
print
"arg values:"
, vals
objs.reverse()
print
"reversed:"
, objs
print
"sorted:"
,
sorted
(objs, key
=
g)
|
结果:
1
2
3
4
|
Object: [MyObj(0), MyObj(1), MyObj(2), MyObj(3), MyObj(4)]
arg values: [0, 1, 2, 3, 4]
reversed: [MyObj(4), MyObj(3), MyObj(2), MyObj(1), MyObj(0)]
sorted: [MyObj(0), MyObj(1), MyObj(2), MyObj(3), MyObj(4)]
|
属性获取方法类似于 。
1
|
lambda
x, n
=
'attrname'
:
getattr
(x,nz)
|
元素获取方法类似于 。
1
|
lambda
x,y
=
5
:x[y]
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
from
operator
import
*
l
=
[
dict
(val
=
-
1
*
i)
for
i
in
xrange
(
4
)]
print
"dictionaries:"
, l
g
=
itemgetter(
"val"
)
vals
=
[g(i)
for
i
in
l]
print
"values: "
, vals
print
"sorted:"
,
sorted
(l, key
=
g)
l
=
[(i,i
*
-
2
)
for
i
in
xrange
(
4
)]
print
"tuples: "
, l
g
=
itemgetter(
1
)
vals
=
[g(i)
for
i
in
l]
print
"values:"
, vals
print
"sorted:"
,
sorted
(l, key
=
g)
|
结果如下:
1
2
3
4
5
6
|
dictionaries: [{'val': 0}, {'val': -1}, {'val': -2}, {'val': -3}]
values: [0, -1, -2, -3]
sorted: [{'val': -3}, {'val': -2}, {'val': -1}, {'val': 0}]
tuples: [(0, 0), (1, -2), (2, -4), (3, -6)]
values: [0, -2, -4, -6]
sorted: [(3, -6), (2, -4), (1, -2), (0, 0)]
|
除了序列之外,元素获取方法还适用于映射.
结合操作符和定制类 operator模块中的函数通过相应操作的标准Python接口完成工作,所以它们不仅适用于内置类型,还适用于用户自定义类型.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
from
operator
import
*
class
MyObj(
object
):
def
__init__(
self
, val):
super
(MyObj,
self
).__init__()
self
.val
=
val
return
def
__str__(
self
):
return
"MyObj(%s)"
%
self
.val
def
__lt__(
self
, other):
return
self
.val < other.val
def
__add__(
self
, other):
return
MyObj(
self
.val
+
other.val)
a
=
MyObj(
1
)
b
=
MyObj(
2
)
print
lt(a, b)
print
add(a,b)
|
结果如下所示:
1
2
|
True
MyObj(3)
|
类型检查 operator 模块还包含一些函数用来测试映射、数字和序列类型的API兼容性.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
from
operator
import
*
class
NoType(
object
):
pass
class
MultiType(
object
):
def
__len__(
self
):
return
0
def
__getitem__(
self
, name):
return
"mapping"
def
__int__(
self
):
return
0
o
=
NoType()
t
=
MultiType()
for
func
in
[isMappingType, isNumberType, isSequenceType]:
print
"%s(o):"
%
func.__name__, func(o)
print
"%s(t):"
%
func.__name__, func(t)
|
结果如下:
1
2
3
4
5
6
|
isMappingType(o): False
isMappingType(t): True
isNumberType(o): False
isNumberType(t): True
isSequenceType(o): False
isSequenceType(t): True
|
但是这些测试并不完善,因为借口没有严格定义.
获取对象方法 使用methodcaller可以获取对象的方法.
1
2
3
4
5
6
7
8
9
10
11
12
|
from
operator
import
methodcaller
class
Student(
object
):
def
__init__(
self
, name):
self
.name
=
name
def
getName(
self
):
return
self
.name
stu
=
Student(
"Jim"
)
func
=
methodcaller(
'getName'
)
print
func(stu)
# 输出Jim
|
还可以给方法传递参数:
1
2
3
4
5
6
7
8
|
f
=
methodcaller(
'name'
,
'foo'
, bar
=
1
)
f(b)
# return b.name('foo', bar=1)
methodcaller方法等价于下面这个函数:
def
methodcaller(name,
*
args,
*
*
kwargs):
def
caller(obj):
return
getattr
(obj, name)(
*
args,
*
*
kwargs)
return
caller
|
最后此篇关于Python中operator模块的操作符使用示例总结的文章就讲到这里了,如果你想了解更多关于Python中operator模块的操作符使用示例总结的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
操作符 分类 算术操作符 移位操作符 位操作符 赋值操作符 单目操作符 关系操作符 逻辑操作符 条件操作符 逗号表达式 下标引用、
大家可以与Java中的 == 操作符相互印证一下,加深一下对引用和对象的理解。原问题: Python为什么直接运行和在命令行运行同样语句但结果却不同,他们的缓存机制不同吗? 其实,高票答案已经说得
a: 1---2-3-4--5---6 b: ------T--------- o: ------1234-5---6 使用RxJS,有没有什么算子可以完成上图?我有一个流 A,它是一个随机的事件流,
请查看下面的代码并帮助我理解这个概念equals 方法仅比较对象的值,而 == 运算符比较对象的引用,那么为什么在代码中 == 运算符返回 false,即使创建了两个单独的对象 Ex Obj1
我在love2d引擎上做了一个无关紧要的lua控制台。我正在尝试升级元表以实现更多功能(配对功能,更改另一个表上的元表而不是目标表等),我正在制作的插件之一是 __changeindex修改现有索引时
今天分享一个比较少见的用法,那就是 Python 是有 ,= 这种用法的,至于是它是否称得上操作符,这个不重要。 咱们先一起回顾一下逗号相关的用法吧: 1,元组的组成部分 没有逗号的括号,
按照要求进行的排序: 复制代码 代码如下: #!/bin/perl @number=qw/5 10 15 3 2 4 8 6 /; my @d
请注意,UNION 内部的每个 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每个 SELECT 语句中的列的顺序必须相同。 SQL UNION 语法 下面的 SQL
运算符“!”有什么区别?和“-z”应用于字符串? #Example 1 if [ ! STRING ]; then ... #Example 2 if [ -z STRING ]; then ...
我想定义一个形式为 x /==> y 的新运算符, 在哪里 运营商/==>被视为例如/@ Map 的运营商, 和 翻译成 MyFunction[x, y] .有一个重要的方面:我 希望结果运算符在前端
我有一个Bitset类,用于存储chars的vector,并且我希望每当使用cout bit = std::vector ((X + 7)/ 8); 上市: / *构造函数* / friend st
我事先确实在这里找到了一个确切的问题,但我没有相同的问题原因(或者至少我认为是这样)。我的 AnimatedSprite类有 Timer成员,使其不可复制(明确删除了复制 ctor 和 operato
这个问题已经有答案了: What does C++ struct syntax "a : b" mean (5 个回答) ":" (colon) in C struct - what does it
我想过滤我的 json,我尝试使用通配符“%”模拟简单的 mysql 运算符“like”,但使用一个……某种“技巧”。 我们有一些元素: var items = [ {id: 1,name:
我正在使用 Flink v.1.4.0。 我希望能够在 Flink UI 中命名运算符。我知道要这样做,我只需要在 DataSet 或 DataStream 上使用 .name() 方法。例如,像这样
在我的任务中,我被要求创建 Product 类,我已经完成了除“非成员 IO 运算符”之外的所有实现。我发现这个问题非常模糊,它要求我重载 >运算符与 ostream 和 istream 一起从控制台
我想要一个 QTableWidget带有定制的某些电池 QProgressBar s,我希望可以对包含这些的列进行排序。 我的定制QProgressBar继承自 QProgressBar和 QTabl
这在 Ruby 中如何实现?不重复变量就可以做到吗?Javascript: b = a || 7 如果 a 不是 0 则分配 a 否则分配 7 一个具体情况是将 date.wday 转换为 7,如果它
在使用 withLatestFrom 时,我正在努力将参数传递给选择器,这是之前从加载操作有效负载映射的 loadLocalSubServices$: Observable = this.action
我每秒将 cronjob 作为 jstack > error.log 获取错误快照。 我的问题是,如果我在 linux 中使用 > 运算符,它是关闭文件还是保持文件打开? 最佳答案 您将每秒覆盖该文件
我是一名优秀的程序员,十分优秀!