python常用内置函数

不报错循环

while True:
    try:
      
    except:
      break

输入函数 input

>>> input()
hello
'hello'

打印函数 print

>>> print(1,2,3)
1 2 3

序列映射 map

>>> lis = map(int,['1','22','333'])
>>> list(lis)
[1, 22, 333]

排序函数 sorted

# 指定排序指标
>>> students = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
>>> sorted(students,key=lambda x:x[2],reverse=False)
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
# 默认字典序
>>> a=[[3,2,1],[2,3,1],[2,1,3],[1,1,2],[1,2,3]]
>>> sorted(a)
[[1, 1, 2], [1, 2, 3], [2, 1, 3], [2, 3, 1], [3, 2, 1]]

保留小数位数 round

>>> round(1.456,2)
1.46

字符串转为表达式 eval

>>> eval('1+2')
3
>>> eval('[1,2,3]')
[1, 2, 3]

二进制 bin

>>> bin(1025)
'0b10000000001'

十进制 int

>>> int(0b10000000001)
1025

添加索引 enumerate

>>> letters = ['a','b','c','e']
>>> list(enumerate(letters,start=1))
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'e')]

压缩 zip

>>> a = [1,2,3]
>>> b = [4,5,6]
>>> list(zip(a,b))
[(1, 4), (2, 5), (3, 6)]
posted @ 2021-01-20 09:50  Bill_H  阅读(71)  评论(0编辑  收藏  举报