python基础:operator.itemgetter函数

# -*- coding: utf-8 -*-
"""
Created on Fri Apr 20 09:59:42 2018

@author: Lelouch_C.C
"""
#operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号
#operator.itemgetter函数不仅是用于获取对象的哪些维的数据
#而是类似于一个函数,通过该函数作用到对象上才能获取值。
import operator

a=[1,2,3]  
b=operator.itemgetter(1) #定义函数b,获取对象的第一个维度的值  
print(b(a))  
#输出:2
b=operator.itemgetter(1,0)#定义函数b,获取对象的第1个维度和第0个维度的值  
print(b(a))  
#输出:(2, 1)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章