文章目錄
- 類型轉換函數
- 數學函數
- 常用的迭代器操作函數
- 常用的其他內置函數
類型轉換函數
數學函數
常用的迭代器操作函數
實操:
from cv2.gapi import descr_oflst = [55, 42, 37, 2, 66, 23, 18, 99]# (1) 排序操作
asc_lst = sorted(lst) # 升序
desc_lst = sorted(lst, reverse=True) # 降序
print(asc_lst)
print(desc_lst)# (2) reversed 倒序
new_lst = reversed(lst)
print(new_lst)
print(list(new_lst))# (3) zip
x = ['a', 'b', 'c', 'd', 'e']
y = [1, 2, 3, 4, 5]
zipobj = zip(x, y)
print(zipobj)
print(list(zipobj))# (4) enumerate
enum = enumerate(y, start=1)
print(list(enum))# (5) all
lst2 = [10, 20, '', 30]
print(all(lst2))
lst2 = [10, 20, '有空即為false', 30]
print(all(lst2))# (6) any
lst2 = ['', '', '', '']
print(any(lst2))
lst2 = [10, 20, '全空即為false', 30]
print(any(lst2))# (7) next
zipped = zip(x, y)
print(next(zipped))
print(next(zipped))
print(next(zipped))
print(next(zipped))# (8) filter
def fun(n):return n % 2 == 1 # 奇數為True, 偶數為False
obj = filter(fun, [1, 2, 3, 4, 5]) # 函數作參數時不用參數
print(list(obj))# (9) map
def upper(s):return s.upper()
new_lst2 = ['hello', 'world']
obj2 = map(upper, new_lst2) # 函數作參數時不用參數
print(list(obj2))
常用的其他內置函數
# (1) format()
print(format(3.14, '20')) # 數值默認右對齊
print(format('3.14', '20')) # 字符串默認左對齊
print(format('3.14', ' <20')) # 左對齊
print(format('3.14', ' >20')) # 右對齊
print(format('3.14', ' ^20')) # 居中對齊
print(format(3.1415926, '.2f')) # 保留兩位小數
print(format(20, 'b')) # 二進制
print(format(20, 'o')) # 八進制
print(format(20, 'x')) # 十六進制# (2) len...# (3) id...(查看內存地址)# (4) type...# (5) eval (字符串內數學運算)
print(eval('10+30'))
print(eval('10>30'))