1.isinstance()函數(shù)
描述:isinstance()函數(shù)判斷一個對象是否是一個已知的類型,類似type()
isinstance() 與type()區(qū)別:
type()不會認(rèn)為子類是一種父類類型,不考慮繼承關(guān)系。
isinstance()會認(rèn)為子類是一種父類類型,考慮繼承關(guān)系
如果要判斷兩個類型是否相同推薦使用isinstance()
語法:
isinstance(object,classinfo)
參數(shù):
- object -- 實例對象。
- classinfo -- 可以是直接或間接類名、基本類型或者由它們組成的元組。
返回值:
如果對象的類型與參數(shù)二的類型(classinfo)相同則返回 True,否則返回 False。。
實例:
>>>a = 2
>>> isinstance (a,int)
True
>>> isinstance (a,str)
False
>>> isinstance (a,(str,int,list)) # 是元組中的一個返回 True
True
type() 與isinstance()區(qū)別
class A:
pass
class B(A):
pass
isinstance(A(), A) # returns True
type(A()) == A # returns True
isinstance(B(), A) # returns True
type(B()) == A # returns False
2.issubclass()函數(shù)
描述:issubclass() 方法用于判斷參數(shù) class 是否是類型參數(shù) classinfo 的子類。
語法:
issubclass(class, classinfo)
參數(shù):
- class -- 類。
- classinfo -- 類。
返回值:
如果 class 是 classinfo 的子類返回 True,否則返回 False。
實例:
class A:
pass
class B(A):
pass
print(issubclass(B,A)) # 返回 True
3.iter()函數(shù)
描述:iter() 函數(shù)用來生成迭代器。
語法:iter(object[, sentinel])
參數(shù):
- object -- 支持迭代的集合對象。
- sentinel -- 如果傳遞了第二個參數(shù),則參數(shù) object 必須是一個可調(diào)用的對象(如,函數(shù)),此時,iter 創(chuàng)建了一個迭代器對象,每次調(diào)用這個迭代器對象的next()方法時,都會調(diào)用 object。
返回值:迭代器對象
實例:
>>>lst = [1, 2, 3]
>>> for i in iter(lst):
... print(i)
...
1
2
3
4.map()函數(shù)
描述:
map() 會根據(jù)提供的函數(shù)對指定序列做映射。
第一個參數(shù) function 以參數(shù)序列中的每一個元素調(diào)用 function 函數(shù),返回包含每次 function 函數(shù)返回值的新列表。
語法:
map(function, iterable, ...)
參數(shù):
- function -- 函數(shù),有兩個參數(shù)
- iterable -- 一個或多個序列
返回值:
- Python 2.x 返回列表。
- Python 3.x 返回迭代器。
實例:
>>>def square(x) : # 計算平方數(shù)
... return x ** 2
...
>>> map(square, [1,2,3,4,5]) # 計算列表各個元素的平方
[1, 4, 9, 16, 25]
>>> map(lambda x: x ** 2, [1, 2, 3, 4, 5]) # 使用lambda 匿名函數(shù)
[1, 4, 9, 16, 25]
# 提供了兩個列表,對相同位置的列表數(shù)據(jù)進(jìn)行相加
>>> map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])
[3, 7, 11, 15, 19]
5.property()函數(shù)
描述:
property()函數(shù)的作用在新式類中返回屬性值
語法:
class property([fget[, fset[, fdel[, doc]]]])
參數(shù):
fegt--獲取屬性值的函數(shù)
fset--設(shè)置屬性值的函數(shù)
fdel--刪除屬性值的函數(shù)
doc--屬性描述信息
返回值:
返回新式類屬性
實例:http://www.runoob.com/python/python-func-property.html
6.repr()函數(shù)
描述:repr()函數(shù)將對象轉(zhuǎn)化為供解釋器讀取的形式
語法:
repr(object)
返回值:返回一個對象的string格式
實例:
>>>s = 'RUNOOB'
>>> repr(s)
"'RUNOOB'"
>>> dict = {'runoob': 'runoob.com', 'google': 'google.com'};
>>> repr(dict)
"{'google': 'google.com', 'runoob': 'runoob.com'}"
>>>
7.reversed函數(shù)
描述:
reversed函數(shù)返回一個反轉(zhuǎn)迭代器。
語法
reversed(seq)
參數(shù):
seq -- 要轉(zhuǎn)換的序列,可以是 tuple, string, list 或 range。
返回值:
返回一個反轉(zhuǎn)的迭代器
實例:
# 字符串
seqString = 'Runoob'
print(list(reversed(seqString)))
# 元組
seqTuple = ('R', 'u', 'n', 'o', 'o', 'b')
print(list(reversed(seqTuple)))
# range
seqRange = range(5, 9)
print(list(reversed(seqRange)))
# 列表
seqList = [1, 2, 4, 3, 5]
print(list(reversed(seqList)))
['b', 'o', 'o', 'n', 'u', 'R']
['b', 'o', 'o', 'n', 'u', 'R']
[8, 7, 6, 5]
[5, 3, 4, 2, 1]
8.sorted()函數(shù)
描述:
sorted()函數(shù)對所有課迭代的對象進(jìn)行排序操作
sort與sorted區(qū)別:
sort是應(yīng)用在list上的方法,sorted可以對所有可迭代對象進(jìn)行排序操作,list的sort方法返回的是對已經(jīng)存在的列表進(jìn)行操作,而內(nèi)建函數(shù)sorted方法返回的是一個新的list,而不是在原來的基礎(chǔ)上進(jìn)行操作
語法:sorted(iterable, key=None, reverse=False)
參數(shù)說明:
- iterable -- 可迭代對象。
- key -- 主要是用來進(jìn)行比較的元素,只有一個參數(shù),具體的函數(shù)的參數(shù)就是取自于可迭代對象中,指定可迭代對象中的一個元素來進(jìn)行排序。
- reverse -- 排序規(guī)則,reverse = True 降序 , reverse = False 升序(默認(rèn))。
返回值 :返回重新排序的列表。
實例:
>>>sorted([5, 2, 3, 1, 4])
[1, 2, 3, 4, 5] # 默認(rèn)為升序
你也可以使用 list 的 list.sort() 方法。這個方法會修改原始 的 list(返回值為None)。
通常這個方法不如sorted()方便-如果你不需要原始的 list,list.sort()方法效率會稍微高一些。
>>>a=[5,2,3,1,4]
>>> a.sort()
>>> a
[1,2,3,4,5]
另一個區(qū)別在于list.sort() 方法只為 list 定義。而 sorted() 函數(shù)可以接收任何的 iterable。
>>>sorted({1: 'D', 2: 'B', 3: 'B', 4: 'E', 5: 'A'})
[1, 2, 3, 4, 5]
利用key進(jìn)行倒序排序
>>>example_list = [5, 0, 6, 1, 2, 7, 3, 4]
>>> result_list = sorted(example_list, key=lambda x: x*-1)
>>> print(result_list)
[7, 6, 5, 4, 3, 2, 1, 0]
>>>