python中內(nèi)置了很多有用的函數(shù),調(diào)用前來賊雞兒爽。
調(diào)用函數(shù)需要知道函數(shù)的名稱和參數(shù)。
這是Python的官方文檔介紹內(nèi)置函數(shù)。
http://docs.python.org/3/library/functions.html#abs
數(shù)學(xué)相關(guān)
abs(x) 求絕對(duì)值
abs()函數(shù)返回參數(shù)的絕對(duì)值

fabs()與abs()的區(qū)別
abs()是一個(gè)內(nèi)置函數(shù),fabs()在math模塊中定義
fabs()函數(shù)只適用于float和integer類型, abs()也適用于復(fù)數(shù)類型。

min()求最小值
返回給參數(shù)最小值
-
初級(jí)用法
image.png - 中級(jí)用法
key屬性的運(yùn)用
當(dāng)key參數(shù)不為空時(shí),就以key的函數(shù)對(duì)象為判斷的標(biāo)準(zhǔn)。
如果我們想找出value中絕對(duì)值最小的數(shù),就可以配合lamda先進(jìn)行處理,再找出最小值
price = {"HPQ": 37.2, 'FB': 10.75, 'AAPL': 612.78}
min(price, key=lambda k: price[k])
結(jié)果如下:

- 高級(jí)用法
找出字典中最小的那組數(shù)據(jù)
price = {"HPQ": 37.2, 'FB': 10.75, 'AAPL': 612.78}
min_price = min(zip(price.value(), price.key()))
print(min_price)
結(jié)果如下:

max()求最大值
用法min()相似
中級(jí)用法
當(dāng)key參數(shù)不為空時(shí),就以key的函數(shù)對(duì)象為判斷的標(biāo)準(zhǔn)。
如果我們想找出一組數(shù)中絕對(duì)值最大的數(shù),就可以配合lamda先進(jìn)行處理,再找出最大值
a = [-9, -8, 1, 2, -1, 6]
max(a, key=lambda x:abs(x))
結(jié)果如下:

sum(): 求和
語法:sum(iterable[, start])
參數(shù):
iterable -- 可迭代的
start -- 指定相加的參數(shù),如果沒有設(shè)置這個(gè)值,默認(rèn)為0。

sorted() 排序
sort與sorted的區(qū)別
| sort | sorted |
|---|---|
| 在原有的列表上排序 | 排序之后返回一個(gè)新列表 |
- 初級(jí)用法
g = [1, 4, 6, 8, 9, 3, 5]
print(sorted(g))
print("--------------------")
print(sorted((1, 4, 6, 8, 9, 3, 5)))
print("--------------------")
print(sorted("gafrtp"))
# 反序
print("--------------------")
print(sorted(g, reverse=True))
結(jié)果如下:

- 高級(jí)用法
有時(shí)候,我們要處理的數(shù)據(jù)內(nèi)的元素不是一維的,而是二維的甚至是多維的,那要怎么進(jìn)行排序呢?這時(shí)候,sorted()函數(shù)內(nèi)的key參數(shù)就派上用場了!從幫助信息上可以了解到,key參數(shù)可傳入一個(gè)自定義函數(shù)。
L = [('a', 1), ('b', 2), ('c', 6), ('d', 4), ('e', 3)]
sorted(L, key=lambda x:x[0])
sorted(L, key=lambda x:x[1])
sorted(L, key=lambda x:x[0], reverse=True)
sorted(L, key=lambda x:x[1], reverse=True)
這里,列表里面的每一個(gè)元素都為二維元組,key參數(shù)傳入了一個(gè)lambda函數(shù)表達(dá)式,其x就代表列表里的每一個(gè)元素,然后分別利用索引返回元素內(nèi)的第一個(gè)和第二個(gè)元素,這就代表了sorted()函數(shù)利用哪一個(gè)元素進(jìn)行排列。而reverse參數(shù)就如同上面講的一樣,起到逆排的作用。默認(rèn)情況下,reverse參數(shù)為False

參考博客:
https://www.cnblogs.com/brad1994/p/6697196.html
divmod() 獲取上和余數(shù)
divmod(a,b)方法返回的是a//b(商)以及a%b(余數(shù)),返回結(jié)果類型為tuple
>>> divmod(9, 2)
(4, 1)
>>> divmod(9, 2)[0]
4
>>> divmod(9, 2)[1]
1
pow() 獲取乘方數(shù)
與math.pow的區(qū)別
| pow | math.pow() |
|---|---|
| 可以在冪乘之后除數(shù)求余 | 只能冪乘 |
1、pow(x,y):這個(gè)是表示x的y次冪。
2、pow(x,y,z):這個(gè)是表示x的y次冪后除以z的余數(shù)
>>> pow(2, 4)
16 這個(gè)是2**4
>>> pow(2, 4, 5)
1 這個(gè)是2**4/5的余數(shù)
round() 方法返回浮點(diǎn)數(shù)x的四舍五入值.
語法:
round( x [, n] )
參數(shù):
x -- 數(shù)字表達(dá)式
n -- 表示從小數(shù)點(diǎn)位數(shù), 其中x需要四舍五入, 默認(rèn)值為0
>>> print ("round(70.23456) : ", round(70.23456))
round(70.23456) : 70
>>> print ("round(56.659,1) : ", round(56.659,1))
round(56.659,1) : 56.7
>>> print ("round(80.264, 2) : ", round(80.264, 2))
round(80.264, 2) : 80.26
>>> print ("round(100.000056, 3) : ", round(100.000056, 3))
round(100.000056, 3) : 100.0
>>> print ("round(-100.000056, 3) : ", round(-100.000056, 3))
round(-100.000056, 3) : -100.0
range() 生成一個(gè)a到b的數(shù)組, 左閉右開
語法:
range(start, stop[, step])
參數(shù)說明:
- start: 計(jì)數(shù)從 start 開始。默認(rèn)是從 0 開始。例如range(5)等價(jià)于range(0, 5);
- stop: 計(jì)數(shù)到 stop 結(jié)束,但不包括 stop。例如:range(0, 5) 是[0, 1, 2, 3, 4]沒有5
- step:步長,默認(rèn)為1。例如:range(0, 5) 等價(jià)于 range(0, 5, 1)
Python3.x 中 range() 函數(shù)返回的結(jié)果是一個(gè)整數(shù)序列的對(duì)象,而不是列表。
>>> list(range(10)) # 從0到10
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(1, 11)) # 從1到11
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> list(range(0, 30, 5)) # 步長為5
[0, 5, 10, 15, 20, 25]
>>> list(range(0, 10, 3)) # 步長為3
[0, 3, 6, 9]
>>> list(range(0, -10, -1)) # 負(fù)數(shù)
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
>>> list(range(0))
[]
>>> list(range(1, 0))
[]
萌萌噠的分割線
類型轉(zhuǎn)換
int() 轉(zhuǎn)為int形
語法:int(x base)
x -- 字符串或數(shù)字
base -- 進(jìn)制數(shù), 默認(rèn)是十進(jìn)制
>>> int() # 不傳入?yún)?shù)
0
>>> int(3)
3
>>> int(3.6) # 浮點(diǎn)數(shù)將向下取整
3
>>> int("12", 16) #如果是帶參數(shù)base的話,12要以字符串的形式進(jìn)行輸入,12 為 16進(jìn)制
18
>>> int("0xa", 16)
10
>>> int('10', 16)
16
>>> int(100, 2) # 出錯(cuò),base 被賦值后函數(shù)只接收字符串
參考博客:https://www.cnblogs.com/guyuyuan/p/6827987.html?utm_source=itdadao&utm_medium=referral
float() 轉(zhuǎn)為浮點(diǎn)型
語法:float(x)
x -- 整數(shù)或字符串
>>> float(1)
1.0
>>> float(-123.6)
-123.6
>>> float("123")
123.0
str() 轉(zhuǎn)為字符型
語法:str(object="")
>>> str(s)
'asfg'
>>> dict = {"a": 1, "b": 2}
>>> str(dict)
"{'a': 1, 'b': 2}"
bool() 轉(zhuǎn)為布爾型
語法:bool([x])
x -- 要進(jìn)行轉(zhuǎn)換的參數(shù)
>>> bool()
False
>>> bool(0)
False
>>> bool(1)
True
>>> issubclass(bool, int) #bool 是int子類
True
-------------------------------------------------------
>>> bool("") # 空字串 False
False
>>> bool("a")
True
-------------------------------------------------------
>>> bool(()) # 空元組
False
>>> bool((0,)) # 非空
True
>>> bool([]) # 空列表
False
>>> bool([1])
True
>>> bool({}) # 空字典
False
>>> bool({"a": 1})
True
bytes()
desc: bytes 函數(shù)返回一個(gè)新的 bytes 對(duì)象,該對(duì)象是一個(gè) 0 <= x < 256 區(qū)間內(nèi)的整數(shù)不可變序列。
bytes([source[, encoding[, error]]])
- 如果 source 為整數(shù),則返回一個(gè)長度為 source 的初始化數(shù)組。
- 如果 source 為字符串,則按照指定的 encoding 將字符串轉(zhuǎn)換為字節(jié)序列。
- 如果 source 為可迭代類型,則元素必須為[0 ,255] 中的整數(shù)。
- 如果 source 為與 buffer 接口一致的對(duì)象,則此對(duì)象也可以被用于初始化 bytearray。如果沒有輸入任何參數(shù),默認(rèn)就是初始化數(shù)組為0個(gè)元素。
>>> a = bytes([1, 2, 3, 4])
>>> a
b'\x01\x02\x03\x04'
>>> type(a)
<class 'bytes'>
>>> a = bytes("helloi", 'ascii')
>>> a
b'helloi'
>>> type(a)
<class 'bytes'>
list() 將元組轉(zhuǎn)化為列表
元組與列表類似, 區(qū)別在于元組值不能修改,元組放在()中, 列表放在[]
語法: list(iterable)
>>> t = (123, "Goole", "asdas")
>>> list(t)
[123, 'Goole', 'asdas']
>>> str = "sadas"
>>> list(str)
['s', 'a', 'd', 'a', 's']
>>> dict_date = {"a": 1, "b": 2, "c": 3} # 字典好像不能直接轉(zhuǎn)
>>> list(dict_date)
['a', 'b', 'c']
iter() 返回一個(gè)可迭代的對(duì)象
用來生成迭代器
語法: iter(object[, sentine])
object -- 支持迭代的集合
sentinel -- 如果傳遞了第二個(gè)參數(shù), 則參數(shù) object 必須是一個(gè)可調(diào)用的對(duì)象(如,函數(shù)),此時(shí),iter 創(chuàng)建了一個(gè)迭代器對(duì)象,每次調(diào)用這個(gè)迭代器對(duì)象的next()方法時(shí),都會(huì)調(diào)用 object。
>>> l = [1, 3, 4]
>>> ite = iter(l)
>>> ite
<list_iterator object at 0x00C211D0>
>>> ite.__next__()
1
>>> ite.__next__()
3
>>> ite.__next__()
4
>>> ite.__next__()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
dict() 轉(zhuǎn)化為字典
創(chuàng)建一個(gè)字典
語法:dict(**kwarg)
dict(mapping, **kwarg)
dict(iterable, **kwarg)
**kwargs ---關(guān)鍵字
mapping --- 元素的容器
iterable --- 可迭代對(duì)象
>>> dict() # 創(chuàng)建字典
{}
>>> dict(a="a", b="b", c="c") # 傳入關(guān)鍵字
{'a': 'a', 'b': 'b', 'c': 'c'}
>>> dict(zip(["one", "two", "three"], [1,2,3])) # 映射函數(shù)方式來構(gòu)造字典
{'one': 1, 'two': 2, 'three': 3}
>>> dict([('one', 1), ('two', 2), ("three", 3)]) # 可迭代對(duì)象方式來構(gòu)造字典
{'one': 1, 'two': 2, 'three': 3}
enumerate() 返回一個(gè)枚舉對(duì)象
函數(shù)用于將一個(gè)可遍歷的數(shù)據(jù)對(duì)象(如列表、元組或字符串)組合為一個(gè)索引序列,同時(shí)列出數(shù)據(jù)和數(shù)據(jù)下標(biāo),一般用在 for 循環(huán)當(dāng)中。
語法: enumerate(iterable, [start=0])
seasons = ["Spring", "Summer", "Fall", "Winter"]
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
__________________ 分割線________________
普通for 循環(huán)
>>> i = 0
>>> seq = ["one", "two", "three"]
>>> for element in seq:
... print(i, seq[i])
... i += 1
...
0 one
1 two
2 three
enumerate 循環(huán)
>>> seq = ["one", "two", "three"]
>>> for i, element in enumerate(seq):
... print(i, seq[i])
...
0 one
1 two
2 three
————————————————————補(bǔ)充———————————————————
# 如果要統(tǒng)計(jì)文件的行數(shù),可以這樣寫
>>>count = len(open(filepath, 'r').readlines())
>>>1
# 這種方法簡單,但是可能比較慢,當(dāng)文件比較# 大時(shí)甚至不能工作。
可以利用enumerate():
>>>count = 0
>>>for index, line in enumerate(open(filepath,'r')):
>>> count += 1
>>>1
>>>2
>>>3
參考博客:https://www.cnblogs.com/quietwalk/p/7997850.html
tuple() 函數(shù)將列表轉(zhuǎn)換為元組。
語法:tuple(seq)
seq --- 要轉(zhuǎn)換為元組的序列
>>> list = ["Google", "tabao", "baidu"]
>>> tuple1 = tuple(list)
>>> tuple1
('Google', 'tabao', 'baidu')
set() 轉(zhuǎn)化為set
desc: 函數(shù)創(chuàng)建一個(gè)無序不重復(fù)元素集,可進(jìn)行關(guān)系測(cè)試,刪除重復(fù)數(shù)據(jù),還可以計(jì)算交集、差集、并集等。
語法: set([iterable])
返回新的集合
>>> x = set("runoob")
>>> y = set("google")
>>> x,y
({'u', 'o', 'r', 'b', 'n'}, {'o', 'g', 'e', 'l'})
>>> x&y # 交集
{'o'}
>>> x | y # 并集
{'l', 'u', 'o', 'r', 'b', 'g', 'n', 'e'}
>>> x - y # 差集
{'b', 'r', 'n', 'u'}
hex() 轉(zhuǎn)化為16進(jìn)制
語法:hex(x)
>>> hex(12233)
'0x2fc9'
>>> hex(-12)
'-0xc'
oct() 轉(zhuǎn)化為8進(jìn)制
語法:oct(x)
>>> oct(12)
'0o14'
>>> oct(-12)
'-0o14'
bin() 轉(zhuǎn)化為2進(jìn)制
語法:bin(x)
>>> bin(1)
'0b1'
>>> bin(-1)
'-0b1'
chr() 轉(zhuǎn)化為數(shù)字為相應(yīng)的ASCII碼
描述: 用一個(gè)范圍在 range(256)內(nèi)的(就是0~255)整數(shù)作參數(shù),返回一個(gè)對(duì)應(yīng)的字符。
>>> chr(0x31) # 十六進(jìn)制
'1'
>>> chr(0x61)
'a'
>>> chr(49) # 十進(jìn)制
'1'
>>> chr(97)
'a'
ord() 轉(zhuǎn)化ASCII字符為相應(yīng)的數(shù)字
語法:ord(c)
>>> ord("a")
97
>>> ord("b")
98
>>> ord("c")
99
相關(guān)操作
eval(): 執(zhí)行一個(gè)表達(dá)式, 或字符串作為運(yùn)算。
描述:eval() 函數(shù)用來執(zhí)行一個(gè)字符串表達(dá)式,并返回表達(dá)式的值。
語法:eval(expression[, globals[, locals]])
- expression -- 表達(dá)式
- globals -- 變量作用域,全局命名空間,如果被提供,則必須是一個(gè)字典對(duì)象。
- locals -- 變量作用域,局部命名空間,如果被提供,可以是任何映射對(duì)象。
>>> x = 7
>>> eval('3 * x')
21
>>> eval('pow(2, 2)')
4
>>> eval('2+2')
4
>>> n = 81
>>> eval("n+4")
85
可以把list,tuple,dict和string相互轉(zhuǎn)化。
參考博客:https://www.cnblogs.com/liu-shuai/p/6098246.html
詳解:
https://www.cnblogs.com/dadadechengzi/p/6149930.html
exec() 執(zhí)行Python語句
?沒用過
map() 根據(jù)提供的函數(shù)對(duì)指定序列做出映射
描述:
第一個(gè)參數(shù) function 以參數(shù)序列中的每一個(gè)元素調(diào)用 function 函數(shù),返回包含每次 function 函數(shù)返回值的新列表。
ensp; 用法靈活,結(jié)合lambda 使用。
語法:
map()函數(shù)語法:
map(function, iterable, ...)
- function 函數(shù), 有兩個(gè)參數(shù)
- iterable 一個(gè)或多個(gè)序列
返回值:
?Python 2 列表
?Python3 迭代器
>>> def square(x):
... return pow(x, 2)
...
>>> map(square, [1,2,3,4,5])
<map object at 0x0173B610>
>>> l = map(square, [1,2,3,4,5])
>>> l.__next__()
1
>>> l.__next__()
4
>>> l.__next__()
9
>>> l.__next__()
16
>>> l.__next__()
25
>>> l.__next__()
————————————————————————————————
>>> lst = map(lambda x:x ** 2, [1, 2, 3, 4, 5]) # 使用lambda 函數(shù)
>>> for i in lst:
... print(i)
...
1
4
9
16
25
filter() 對(duì)指定序列執(zhí)行過濾操作
描述: 用于過濾序列,過濾掉不符合條件的元素,返回由符合條件元素組成的新列表。
該接收兩個(gè)參數(shù),第一個(gè)為函數(shù),第二個(gè)為序列,序列的每個(gè)元素作為參數(shù)傳遞給函數(shù)進(jìn)行判,然后返回 True 或 False,最后將返回 True 的元素放到新列表中。
語法: filter(function, iterable)
function -- 判斷函數(shù)
iterable -- 可迭代對(duì)象
返回值
返回一個(gè)迭代對(duì)象
>>> def is_odd(n):
... return n % 2 == 1
...
>>> tmplist = filter(is_odd, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
>>> newlist = list(tmplist)
>>> print(newlist)
[1, 3, 5, 7, 9]
過濾1-100中平方根是整數(shù)的數(shù):
>>> import math
>>> def is_sqr(x):
... return math.sqrt(x) % 1 == 0
...
>>> tmplist = filter(is_sqr, range(1, 101))
>>> newlist = list(tmplist)
>>> print(newlist)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
reduce() 對(duì)參數(shù)序列中元素進(jìn)行累積
描述:函數(shù)將一個(gè)數(shù)據(jù)集合(鏈表,元組等)中的所有數(shù)據(jù)進(jìn)行下列操作:用傳給 reduce 中的函數(shù) function(有兩個(gè)參數(shù))先對(duì)集合中的第 1、2 個(gè)元素進(jìn)行操作,得到的結(jié)果再與第三個(gè)數(shù)據(jù)用 function 函數(shù)運(yùn)算,最后得到一個(gè)結(jié)果。
注意:在python3.0以后, reduce不在內(nèi)建函數(shù)中了, 要用它就要from functools import reduce
語法:
reduce(function, iterable[, initializer])
- function 函數(shù), 有兩個(gè)參數(shù)
- iterable 可迭代對(duì)象
- initializer 可選, 初始參數(shù)
>>> from functools import reduce
>>> reduce(lambda x, y: x+y, [1, 2, 3])
6
>>> reduce(lambda x, y: x+y, [1, 2, 3], 9)
15
-----------------------------------------------------
from functools import reduce
def add(x,y):
return x + y
print (reduce(add, range(1, 101)))
result=5050
————————————————————————————————
統(tǒng)計(jì)某字符串重復(fù)的次數(shù)
>>> from functools import reduce
>>> sentences = ['The Deep Learning textbook is a resource intended to help students and practitioners enter the field of machine learning in general and deep learning in particular. ']
>>> word_count =reduce(lambda a,x:a+x.count("learning"),sentences,0)
>>> print(word_count)
>>> 2
zip() 將iterable分組合并。返回一個(gè)zip對(duì)象
函數(shù)用于將可迭代的對(duì)象作為參數(shù),將對(duì)象中對(duì)應(yīng)的元素打包成一個(gè)個(gè)元組,然后返回由這些元組組成的列表。
如果各個(gè)迭代器的元素個(gè)數(shù)不一致,則返回列表長度與最短的對(duì)象相同,利用 * 號(hào)操作符,可以將元組解壓為列表。
zip 方法在 Python 2 和 Python 3 中的不同:在 Python 3.x 中為了減少內(nèi)存,zip() 返回的是一個(gè)對(duì)象。如需展示列表,需手動(dòng) list() 轉(zhuǎn)換。
語法:
zip([iterable, ...])
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> c = [4, 5, 6, 7, 8]
>>> zipped = zip(a, b) # 返回一個(gè)對(duì)象
>>> zipped
<zip object at 0x00B58440>
>>> list(zipped)
[(1, 4), (2, 5), (3, 6)]
>>> list(zip(a, c)) # 元素個(gè)數(shù)與最短的列表一致
[(1, 4), (2, 5), (3, 6)]
>>>
>>> a1, a2 = zip(*zip(a, b)) # 與zip相反, *zip可以理解為解壓, 返回二維矩陣。
>>> list(a1)
[1, 2, 3]
>>> list(a2)
[4, 5, 6]
————————————————————————————————
>>> v1 = {1:11, 2:22}
>>> v2 = {3:33, 4:44}
>>> v3 = {5:55, 6:66}
>>> v = list(zip(v1, v2, v3)) # 壓縮
>>> print(v)
[(1, 3, 5), (2, 4, 6)]
>>> w = zip(*zip(v1, v2, v3)) # 解壓
>>> print(list(w))
[(1, 2), (3, 4), (5, 6)]
參考博客:https://www.cnblogs.com/wushuaishuai/p/7766470.html
hash() 返回一個(gè)對(duì)象的hash值,
用于獲取取一個(gè)對(duì)象(字符串或者數(shù)值等)的哈希值
語法:
?hash(obj)
>>> hash('test') # 字符串
740618988
>>> hash(1) # 數(shù)字
1
>>> hash(str([1, 2, 3])) # 集合
2097662348
>>> hash(str(sorted({'1':1}))) # 字典
1162454008
isinstance() 函數(shù)來判斷一個(gè)對(duì)象是否是一個(gè)已知的類型,類似 type()。
- isinstance() 與 type()的區(qū)別
>**type() 不會(huì)認(rèn)為子類是一種父類類型,不考慮繼承關(guān)系。
isinstance() 會(huì)認(rèn)為子類是一種父類類型,考慮繼承關(guān)系。
如果要判斷兩個(gè)類型是否相同推薦使用 isinstance()。**
>>> class A:
... pass
>>> class B(A):
... pass
>>> isinstance(A(), A)
True
>>> type(A()) == A
True
>>> isinstance(B(), A)
True
>>> type(B()) == A
False
語法:
? isinstance(obj, classinfo)
- object -- 實(shí)例對(duì)象。
- classinfo -- 可以是直接或間接類名、基本類型或者由它們組成的元組。
>>> a = 2
>>> isinstance(a, int)
True
>>> isinstance(a, str)
False
>>> isinstance(a, (str, int, list)) # 是元組中的一個(gè)返回True
True
issubclass() 方法用于判斷參數(shù) class 是否是類型參數(shù) classinfo 的子類。
語法:
? issubclass(class, classinfo)
>>> class A:
... pass
>>> class B(A):
... pass
>>> print(issubclass(B,A))
True
reversed() 生成一個(gè)反序列的迭代器
語法: reversed(seq)
seq -- 要轉(zhuǎn)換的序列, 可以是tuple, string, list或者range
>>> seqstring = 'Runoob' # 字符串
>>> print(list(reversed(seqstring)))
['b', 'o', 'o', 'n', 'u', 'R']
>>> seqtuple = ('R', 'u', 'n', 'o', 'o', 'b') # 元組
>>> print(list(reversed(seqtuple)))
['b', 'o', 'o', 'n', 'u', 'R']
>>> seqrange = range(5, 9)
>>> print(list(reversed(seqrange)))
[8, 7, 6, 5]
>>> seqlist = [1, 2, 4, 3, 5] # 列表
>>> print(list(reversed(seqlist)))
[5, 3, 4, 2, 1]
詳解博客:
https://blog.csdn.net/sxingming/article/details/51353379
globals() 返回當(dāng)前全局變量的字典
函數(shù)會(huì)以字典類型返回當(dāng)前位置的全部全局變量。
其他
hasattr(obj, name)
?判斷一個(gè)對(duì)象里面是否有name屬性或者name方法,返回BOOL值,有name特性返回True, 否則返回False。
需要注意的是name要用括號(hào)括起來
getattr(obj, name)
?獲取對(duì)象object的屬性或者方法,如果存在打印出來,如果不存在,打印出默認(rèn)值,默認(rèn)值可選。
需要注意的是,如果是返回的對(duì)象的方法,返回的是方法的內(nèi)存地址,如果需要運(yùn)行這個(gè)方法,
可以在后面添加一對(duì)括號(hào)。
setattr(obj, name, values)
?給對(duì)象的屬性賦值,若屬性不存在,先創(chuàng)建再賦值。
join()
用于將序列中的元素以指定的字符連接生成一個(gè)新的字符串。
語法:
str.join(seq)
seq --- 要連接的元素序列
返回值:
返回通過指定字符連接序列中元素后生成的新字符串
>>> str = "-"
>>> seq = ("a", "b", "c")
>>> str = str.join(seq)
>>> print(str)
a-b-c
>>> str = "".join(seq)
>>> str
'abc'
?參考博客:
?https://www.cnblogs.com/cenyu/p/5713686.html
??本文主要參考菜鳥教程, 覺得不錯(cuò)點(diǎn)個(gè)贊唄。
