operator module在使用内置函数如map,itertools.groupby,sorted排序使用DSU技巧时,经常用到;这里面最经常用到的两个函数是:
operator.
attrgetter
(
attr
[
, args...
]
)
这个函数返回一个可调用对象(Callable Objects),取此对象的attr属性值;如果参数是多个属性,则返回的是属性值tuple.
例如:f
=
attrgetter('name')
,则 f(b)
返回的是 b.name
. f
=
attrgetter('name',
'date')
,则 f(b)
返回的是
(b.name,
b.date)
. Equivalent to:
resolve_attr是用来处理属性名有.情 况;f
=
attrgetter('date.month')
,调用 f(b)
返回的是 b.date.month
.
应用举例DSU:
operator.
itemgetter
(
item
[
, args...
]
)
Return a callable object that fetches item
from its operand using the operand’s __getitem__()
method. If multiple items are specified, returns a tuple of lookup values. Equivalent to:
The items can be any type accepted by the operand’s __getitem__()
method. Dictionaries accept any hashable value. Lists, tuples, and strings accept an index or a slice:
>>>
itemgetter
(
1
)(
'ABCDEFG'
)
'B'
>>>
itemgetter
(
1
,
3
,
5
)(
'ABCDEFG'
)
('B', 'D', 'F')
>>>
itemgetter
(
slice
(
2
,
None
))(
'ABCDEFG'
)
'CDEFG'
分享到:
相关推荐
资源分类:Python库 所属语言:Python 资源全名:operator-manifest-0.0.1.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
Python 3 Operator Precedence.pdf
Python functions can either be exposed as Excel user defined functions that can be called from worksheets, as custom menu items, or as macros.
Python 3 Operator Precedence
资源分类:Python库 所属语言:Python 资源全名:operator_csv_libs-1.8.0-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
python学习笔记,包含修饰符的使用-operator模块-作用域-动态编译
python中的operator模块提供了一系列的函数操作。下面这篇文章主要给大家介绍了在Python中利用operator模块实现对象的多级排序的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。
python_operator_airflow 运行文件setting_up.sh创建虚拟环境并安装python3 pip: $ sh setting_up.sh 安装气流并设置本地主机 $ pip install -r required.txt $气流initdb $ airflow网络服务器-p 8080 或运行...
python库,解压后可用。 资源全名:python_chi_operator-0.2.0-py3-none-any.whl
operator模块中包含了Python的各种内置操作符,诸如逻辑、比较、计算等,这里我们针对一些常用的操作符来作一个Python中operator模块的操作符使用示例总结:
perator模块输出一系列对应Python内部操作符的函数。例如:operator.add(x, y)等价于表达式x+y。许多函数的名称都被一些特定的方法使用,没有下划线加持。为了向下兼容,它们中的许多都保留着由双下划线的变体。那些...
python 3.4.3 的版本中已经没有cmp函数,被operator模块代替,在交互模式下使用时,需要导入模块。 在没有导入模块情况下,会出现 提示找不到cmp函数了,那么在python3中该如何使用这个函数呢? 所以要导入模块 ...
资源来自pypi官网。 资源全名:python_chi_operator-0.2.0-py3-none-any.whl
uafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in LIBCMTD.lib(new.obj) uafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl ...
资源分类:Python库 所属语言:Python 资源全名:atomic_operator-0.7.0-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
Python编程规范PEP8( Style Guide for Python),包括命名规范,注释规则,编程推荐等等,欢迎大家下载。