operator

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

a = [1,2,3] 
>>> b=operator.itemgetter(1)      //定义函数b,获取对象的第1个域的值
>>> b(a) 

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

要注意,operator.itemgetter函数获取的不是值,而是定义了一个函数,通过该函数作用到对象上才能获取值。

sorted(ngrams.items(),key = operator.itemgetter(1),reverse = True)

ngrams 是一个字典,对字典排序

猜你喜欢

转载自blog.csdn.net/zjkpy_5/article/details/88617803