operator.itemgetter函数
原型:
operator. itemgetter ( item ) operator. itemgetter ( *items ) 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. For example:
注意:operator.itemgetter函数返回的是一个函数,该函数返回指定序号的值,通过该函数作用到对象上才能获取值
sorted函数
原型:sorted(iterable[, cmp[, key[, reverse]]])
参数解释:
(1)iterable指定要排序的list或者iterable;
(2)cmp specifies a custom comparison function of two arguments (iterable elements) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument: cmp=lambda x,y: cmp(x.lower(), y.lower()). The default value is None.
(3)key specifies a function of one argument that is used to extract a comparison key from each list element: key=str.lower. The default value is None (compare the elements directly)。可能使用key=operator.itemgetter(1)比较多
(4)reverse参数,是一个bool变量,表示升序还是降序排列,默认为false(升序排列),定义为True时将按降序排列。