必会的内置函数方法: len type id print input open round min max filter map zip exec eval
# list dict tuple str int float set # print input type id len print(all([1, 2, 3,4])) # 判断可迭代的对象里面的值是否都为真,都为真,结果才是真 print(all([1,2,3,4,False]))#判断可迭代的对象里面的值是否都为真 print(any([0,False,'',[],1])) # 判断可迭代的对象里面的值是否有一个为真,只要有一个为真,就是真 print(any([0, 1, 2, 3, 4])) print(max(111, 12)) # 取最大值 print(min([1,2,3,4])) print(round(11.1111, 2)) # 取几位小数 print(filter(lambda x: x > 5, [12, 3, 12, 2, 1, 2, 35])) #filter自动循环你传给它的list,然后把list里的每一个元素传给指定的函数,如果函数结果返回True,那么保留这个元素,需要传两个参数,func_name,list print(map(lambda x: x > 5, [1, 2, 3, 4, 5, 6])) # map自动循环你传给它的list,把list里的每一个元素传给指定的函数,把这个函数的返回结果保存下来 print(bin(10)) # 十进制转二进制 print(chr(10)) # 打印数字对应的ascii print(ord('b')) # 打印字符串对应的ascii码 print(dir(1)) # 打印传入对象的可调用方法 print(eval('[]')) # 执行python代码,只能执行简单的,定义数据类型和运算 print(exec('def a():pass')) # 执行python代码 print(globals()) # 返回程序内所有的变量,返回的是一个字典 print(locals()) # 返回局部变量 print(hex(111)) # 数字转成16进制 print(oct(111)) # 把数字转换成8进制 locals() #获取当前函数里面的局部变量,以字典方式输出 globals() #获取当前文件里面的全局变量,以字典方式输出 zip() #把多个list压缩到一起 sorted()#排序filter和map 没有使用filter方法
def oushu(number): if number%2==0: return True l = range(1,11) l2 = [] for i in l: if oushu(i): l2.append(i) print(l2) 输出结果: [2, 4, 6, 8, 10]使用filter方法
def oushu(number): if number%2==0: return True l = range(1,11) result = list(filter(oushu,l))#filter自动循环你传给它的list,然后把list里的每一个元素传给指定的函数,如果函数结果返回True,那么保留这个元素 print(result) 输出结果: [2, 4, 6, 8, 10]使用map方法
def oushu(number): if number%2==0: return True l = range(1,11) result2 = list(map(oushu,l)) # map自动循环你传给它的list,把list里的每一个元素传给指定的函数,把这个函数的返回结果保存下来 print(result2) 输出结果: [None, True, None, True, None, True, None, True, None, True] result3 = list(map(str,range(1,101)))# 把1--100的数字转化为字符串,并用list输出 print(result3) 输出结果: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99', '100']动态执行代码:exec()方法
s=''' for i in range(10): print(i) ''' exec(s)#用来动态执行python代码的 输出结果: 0 1 2 3 4 5 6 7 8 9locals()和globals()
result3 = list(map(str,range(1,101))) def test(): a = 1 b = 2 print('locals()方法输出结果:',locals())#获取当前函数里面的局部变量,以字典方式输出 print('globals()方法输出结果:',globals())#获取当前文件里面的全局变量,以字典方式输出 test() 输出结果: locals()方法输出结果: {'a': 1, 'b': 2} globals()方法输出结果: {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x00000267A4CF08B0>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'D:/Python/project/tmz-code/day5/内置函数.py', '__cached__': None, 'result3': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99', '100'], 'test': <function test at 0x00000267A4E42790>} a = ['fd','hzy','ds'] b = ['1224','456','789'] c = ['1','2','3'] for username,password,c in zip(a,b,c): print(c,username,password) #以字符串形式输出,把三个list压缩到一起 输出结果: 1 fd 1224 2 hzy 456 3 ds 789把多个list压缩在一起:zip()方法
a = ['fd','hzy','ds'] b = ['1224','456','789'] c = ['1','2','3'] for u in zip(c,a,b): print(u) #以元组方式输出,把三个list压缩到一起 输出结果: ('1', 'fd', '1224') ('2', 'hzy', '456') ('3', 'ds', '789')排序方法:sorted()
s='235423236236' result = sorted(s,reverse=True)#reverse=True是降序 result2 = sorted(s)#默认升序 print(result) print(result2) 输出结果: ['6', '6', '5', '4', '3', '3', '3', '3', '2', '2', '2', '2'] ['2', '2', '2', '2', '3', '3', '3', '3', '4', '5', '6', '6']枚举方法:enumerate()
l = ['a','b','c','d'] for s in enumerate(l): print(s) 输出结果: (1, 'b') (2, 'c') (3, 'd')练习:按分数降序排列
d = { 'fd':100, 'ds':93, 'lhy':88, 'hzy':35 }#按分数降序排列 result = sorted(d.items(),key=lambda x:x[0],reverse=True) # result = sorted(d.items(),key=get_value,reverse=True) print(result) 输出结果: [('lhy', 88), ('hzy', 35), ('fd', 100), ('ds', 93)]