标签:保留 load png pen 换行 小数 shandong UNC col
1 #递归练习题(深度查询) 2 menus = [ 3 { 4 ‘test‘: ‘北京‘, 5 ‘children‘: [ 6 {‘test‘: ‘朝阳‘, ‘children‘: []}, 7 {‘test‘: ‘昌平‘, ‘children‘: [ 8 {‘test‘: ‘沙河‘, ‘children‘: []}, 9 {‘test‘: ‘回龙观‘, ‘children‘: []}, 10 ]}, 11 ] 12 }, 13 { 14 ‘test‘: ‘上海‘, 15 ‘children‘:[ 16 {‘test‘: ‘宝山‘, ‘children‘: []}, 17 {‘test‘: ‘金山‘, ‘children‘: []}, 18 ] 19 } 20 ] 21 #打印所有的节点 22 def calc(menu): 23 for m in menu: #循环 24 menu_test = m[‘test‘] 25 menu_children = m[‘children‘] 26 print(menu_test) #打印节点 27 calc(menu_children) #递归 28 calc(menus) #调用函数 29 30 #输入一个节点名字,沙河,要遍历找,找到了就打印它,并返回true, 31 def calc1(menu, node, layer): 32 for i in menu : 33 menu_test = i[‘test‘] 34 menu_children = i[‘children‘] 35 print(menu_test) 36 if node == menu_test: 37 print("找到%s在第%s层"%(node,layer)) #返回到外层 38 return True 39 else: 40 if calc1(menu_children,node,layer+1) == True : #如果里层返回True,继续向上返回True 41 return True 42 else: 43 calc1(menu_children,node,layer+1) 44 node_str = input("输入一个节点名字:") 45 print(calc1(menus,node_str,1))
1 # abs 绝对值 2 print(abs(-11)) #结果为:11 3 4 #dict 将一些数据转换为字典类型 5 #help 查看帮助 6 7 #min 返回最小值 max 返回最大值 8 a =[1,4,5,-1,3] 9 print(min(a)) #结果为:-1 10 print(max(a)) #结果为:5 11 12 # all 列表里面所有的值都为 True 则返回 True(0、False为 False) 13 print(all(a)) #结果为:True 14 15 print(bool(0)) #结果为:False 16 print(bool(False)) #结果为:False 17 print(bool(-1)) #结果为:True 18 19 print(bool([])) #结果为:False 20 print(all([])) #结果为:True 21 22 # any 列表里面有一个值为 True 则返回 True(0、False为 False) 23 a =[1,4,5,-1,0] 24 print(any(a)) #结果为:True 25 26 #dir 打印当前程序中存在的所有变量 27 print(dir()) #结果为:[‘__annotations__‘, ‘__builtins__‘, ‘__cached__‘, ‘__doc__‘, ‘__file__‘, ‘__loader__‘, ‘__name__‘, ‘__package__‘, ‘__spec__‘, ‘a‘] 28 29 #hex 将十进制数值转换为16进制 30 print(hex(12)) #结果为:0xc 31 32 #slice 切片 33 s = slice(1,7,2) 34 li = list(range(10)) 35 print(li) #结果为:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 36 print(s) #结果为:slice(1, 7, 2) 37 print(li[1:3]) #结果为:[1, 2] 38 print(li[s]) #结果为:[1, 3, 5] 39 40 #divmod 返回商和余数 41 print(divmod(10,3)) #结果为:(3, 1) 42 43 #sorted 排序 44 li = [0, 1, 2, 3, 99, 81, 4, 5, 6, 7, 8, 9] 45 print(li) #结果为:[0, 1, 2, 3, 99, 81, 4, 5, 6, 7, 8, 9] 46 print(sorted(li)) #结果为:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 81, 99] 47 #字典是无序的 48 d = { } 49 for i in range(20): 50 d[i] = i - 50 51 print(d) #结果为:{0: -50, 1: -49, 2: -48, 3: -47, 4: -46, 5: -45, 6: -44, 7: -43, 8: -42, 9: -41, 10: -40, 11: -39, 12: -38, 13: -37, 14: -36, 15: -35, 16: -34, 17: -33, 18: -32, 19: -31} 52 print(d.items()) #结果为:dict_items([(0, -50), (1, -49), (2, -48), (3, -47), (4, -46), (5, -45), (6, -44), (7, -43), (8, -42), (9, -41), (10, -40), (11, -39), (12, -38), (13, -37), (14, -36), (15, -35), (16, -34), (17, -33), (18, -32), (19, -31)]) 53 print(sorted(d.items())) #结果为:[(0, -50), (1, -49), (2, -48), (3, -47), (4, -46), (5, -45), (6, -44), (7, -43), (8, -42), (9, -41), (10, -40), (11, -39), (12, -38), (13, -37), (14, -36), (15, -35), (16, -34), (17, -33), (18, -32), (19, -31)] 54 print(sorted(d.items(),key = lambda x:x[1])) #结果为:[(0, -50), (1, -49), (2, -48), (3, -47), (4, -46), (5, -45), (6, -44), (7, -43), (8, -42), (9, -41), (10, -40), (11, -39), (12, -38), (13, -37), (14, -36), (15, -35), (16, -34), (17, -33), (18, -32), (19, -31)] 55 d[0] = 299 56 print(sorted(d.items(),key = lambda x:x[1])) #最大值在最后面 结果为:[(1, -49), (2, -48), (3, -47), (4, -46), (5, -45), (6, -44), (7, -43), (8, -42), (9, -41), (10, -40), (11, -39), (12, -38), (13, -37), (14, -36), (15, -35), (16, -34), (17, -33), (18, -32), (19, -31), (0, 299)] 57 print(sorted(d.items(),key = lambda x:x[1],reverse=True)) #最大值在最前面 结果为:[(0, 299), (19, -31), (18, -32), (17, -33), (16, -34), (15, -35), (14, -36), (13, -37), (12, -38), (11, -39), (10, -40), (9, -41), (8, -42), (7, -43), (6, -44), (5, -45), (4, -46), (3, -47), (2, -48), (1, -49)] 58 59 #ascii 将字符转成二进制 60 s = ‘abcd路飞‘ 61 print(s) #结果为:abcd路飞 62 print(ascii(s)) #结果为:‘abcd\u8def\u98de‘ 63 64 #enumerate 枚举,返回列表的索引 65 66 # oct 将10进制转换成为8进制 67 print(oct(12)) #结果为:0o14 68 69 # bin 将10进制转换成为2进制; 70 print(bin(12)) #结果为:0b1100
1 #eval exec 将字符串转成数字 2 f = ‘1+3/2‘ 3 print(f) #结果为:1+3/2 4 print(eval(f)) #结果为:2.5 5 code = """ 6 def foo(): 7 prinr("run foo") 8 return 1234 9 foo() 10 """ 11 res = eval("1+3+3") 12 res2 = exec("1+3+3") 13 print(‘res‘,res) #结果为:res 7 14 print(‘res‘,res2) #结果为:res None 15 # 小结; 16 # eval只能处理单行代码,而exec可以处理多行代码; 17 # 但exec没有返回值,eval有返回值; 18 19 #ord 返回在ASCII码表中的位置 chr 通过ASCII码表中的位置,返回字符 20 print(ord(‘a‘)) #结果为:97 21 print(ord(‘b‘)) #结果为:98 22 print(chr(97)) #结果为:a 23 print(chr(98)) #结果为:b 24 25 #sum 求列表中元素的和 26 a =[1,4,5,-1,3,0] 27 print(sum(a)) #结果为:12 28 29 #bytearray 30 print(a) #结果为:[1, 4, 5, -1, 3, 0] 31 s = ‘abcd路飞‘ 32 print(s) #结果为:abcd路飞 33 print(s[0]) #结果为:a 34 #s[0]=‘A‘ 35 #print(s[0]) #结果为:TypeError: ‘str‘ object does not support item assignment 36 #s2 = bytearray(s) #结果为:TypeError: string argument without an encoding 37 s = s.encode(‘utf-8‘) 38 print(s[0]) #结果为:97 39 #s[0] =98 #结果为:TypeError: ‘bytes‘ object does not support item assignment 40 s = bytearray(s) 41 print(s[0]) #结果为:97 42 s[0] = 65 43 print(s) #结果为:bytearray(b‘Abcd\xe8\xb7\xaf\xe9\xa3\x9e\xe5\xad\xa6\xe5\x9f\x8e‘) 44 print(s[4]) #结果为:232 45 s[4]=233 46 print(s) #结果为:bytearray(b‘Abcd\xe8\xb7\xaf\xe9\xa3\x9e\xe5\xad\xa6\xe5\x9f\x8e‘) 47 print(s.decode()) #结果为:Abcd鷯飞学城 48 print(id(s)) #结果为:1987262190008 49 s[0]=66 50 print(id(s)) #结果为:1987262190008 51 52 #map filter(过滤) reduce 三剑客 53 print(map(lambda x: x*x , [1,2,3,4,5])) #结果为:<map object at 0x0000022DB38384A8> 54 print(list(map(lambda x: x*x , [1,2,3,4,5]))) #结果为:[1, 4, 9, 16, 25] 55 56 print(filter(lambda x: x>3 , [1,2,3,4,5])) #结果为:<filter object at 0x000002B9B1CC84E0> 57 print(list(filter(lambda x: x>3 , [1,2,3,4,5]))) #结果为:[4, 5] 58 59 import functools 60 print(functools.reduce(lambda x,y:x+y,[1,3,4,5,6678,4,2]))#求和 结果为:6697 61 print(functools.reduce(lambda x,y:x+y,[1,3,4,5,6678,4,2],3))#从第三个开始求和 结果为:6700 62 print(functools.reduce(lambda x,y:x*y,[1,3,4,5,6678,4,2]))#求乘积 结果为:3205440 63 64 65 #pow 返回2的10次幂 66 print(pow(2,10)) #结果为:1024
1 #print 2 s = ‘hey, my name is alex\n,from shandong‘ 3 print(s) 4 #结果为: 5 # hey, my name is alex 6 #,from shandong 7 # 默认end结尾是一个\n换行符 8 9 print(s,end=‘,‘) 10 #结果为 11 #hey, my name is alex 12 #,from shandong, 13 14 print(s,end=‘|‘) 15 #结果为 16 #hey, my name is alex 17 #,from shandong| 18 19 print(s,sep="") 20 #结果为 21 #hey, my name is alex 22 #,from shandong 23 24 print(‘haifeng‘,‘gangniang‘) #结果为:haifeng gangniang 25 print(‘haifeng‘,‘gangniang‘,sep=‘---‘) #结果为:haifeng---gangniang 26 27 #print将内容直接重定向至文件中 28 msg = "又回到最初的起点" 29 f = open("print_tofile","w") 30 print(msg,"记忆中你青涩的脸",sep="|",end="",file=f) 31 32 33 #callable 判断一个对象是否可调用 34 a = [1,2,3,12,4] 35 print(callable(a)) #结果为:False 36 def f(): 37 pass 38 print(f) #结果为:<function f at 0x000002D25B1997B8> 39 print(callable(f)) #结果为:True 40 41 42 #discard 删除一个值 43 s={12,3,4,4} 44 #print(s.discard()) #结果为:TypeError: discard() takes exactly one argument (0 given) 45 s.discard(3) 46 print(s) #结果为:{12, 4} 47 48 49 #vars 打印当前所有的变量名和变量值 50 51 #locals 打印函数的局部变量 52 def f(): 53 n=3 54 print(locals()) 55 f() #结果为:{‘n‘: 3} 56 57 #globals 打印全局变量 58 59 #repr 60 s={12,3,4,4} 61 print(repr(s)) #结果为:{3, 12, 4} 62 63 #zip 64 a= [1,2,3,4,5] 65 b=[‘a‘,‘b‘,‘c‘] 66 print(zip(a)) #结果为:<zip object at 0x0000015F01C46A08> 67 print(list(zip(a,b))) #结果为:[(1, ‘a‘), (2, ‘b‘), (3, ‘c‘)] 68 69 70 #complex 变为复数 71 print(complex(3,5)) #结果为:(3+5j) 72 73 #round 四舍五入取值 74 print(round(1.553433)) #结果为:2 75 print(round(1.453433)) #结果为:1 76 #保留5位小数 77 print(round(1.343433,5)) #结果为:1.34343 78 79 80 # hash hash值 81 print(hash("武倩倩")) #结果为:6699813159710321764 82 83 #set 将列表变为集合: 84 print(set([1,3,4,5,6,7])) #结果为:{1, 3, 4, 5, 6,7}
标签:保留 load png pen 换行 小数 shandong UNC col
原文地址:https://www.cnblogs.com/wqq0723/p/9596056.html