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)

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