python学习——常用的内置函数汇总
文章目录
- 类型转换函数
- 数学函数
- 常用的迭代器操作函数
- 常用的其他内置函数
类型转换函数
数学函数
常用的迭代器操作函数
实操:
from cv2.gapi import descr_of
lst = [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'))