2018-08-23

operator.itemgetter函数:

import operator

>>> help(operator.itemgetter)

Help on class itemgetter in module operator:

class itemgetter(builtins.object)

|  itemgetter(item, ...) --> itemgetter object

|

|  Return a callable object that fetches the given item(s) from its operand.

|  After f = itemgetter(2), the call f(r) returns r[2].

|  After g = itemgetter(2, 5, 3), the call g(r) returns (r[2], r[5], r[3])

|

|  Methods defined here:

|

|  __call__(self, /, *args, **kwargs)

|      Call self as a function.

|

|  __getattribute__(self, name, /)

|      Return getattr(self, name).

|

|  __reduce__(...)

|      Return state information for pickling

|

|  __repr__(self, /)

|      Return repr(self).

|

|  ----------------------------------------------------------------------

|  Static methods defined here:

|

|  __new__(*args, **kwargs) from builtins.type

|      Create and return a new object.  See help(type) for accurate signature.


operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号)

a=[1,2,3]

b=operator.itemgetter(1)#定义函数b,获取对象的第一个域的值

b(a)

b=operator.itemgetter(1,0)#定义函数b,获取对象的第1个域和第0个域的值

b(a)


运行结果:

a = [1,2,3]

>>> b = operator.itemgetter(1)

>>> b(a)

2

>>> b = operator.itemgetter(1,0)

>>> b(a)

(2, 1)

你可能感兴趣的:(2018-08-23)