python学习 内建属性/内建函数/集合/functools
1 '''内建属性''' 2 class Idcast(): 3 def __init__(self,subject1): 4 self.subject1=subject1 5 self.subject2='hello' 6 7 #属性访问时拦截器,打印log 8 def __getattribute__(self, item): #item---->属性 9 print('====1>{}'.format(item)) 10 if item=='hh': 11 print('嘿嘿') 12 return 'hello subject1' 13 else: 14 temp=object.__getattribute__(self,item) 15 print('====2>{}'.format(temp)) 16 return temp 17 18 def show(self): 19 print('this is Idcast') 20 21 s=Idcast('xxxxxx') 22 print(s.hh) 23 print(s.subject1) 24 print(s.subject2) 25 s.show() #1、先获取show属性对应的结果,应该是一个方法; 2、方法() 26 27 #打印结果 28 # ====1>hh 29 # 嘿嘿 30 # hello subject1 31 # ====1>subject1 32 # ====2>xxxxxx 33 # xxxxxx 34 # ====1>subject2 35 # ====2>hello 36 # hello 37 # ====1>show 38 # ====2><bound method Idcast.show of <__main__.Idcast object at 0x000001A865911240>> 39 # this is Idcast
1 '''内建函数''' 2 #####map可根据可迭代对象,生成一个新的迭代对象##### 3 a=map(lambda x:x+x,[1,2,3]) 4 for i in a: 5 print(i) #打印结果为2,4,6 6 7 b=map(lambda x,y:x+y,[1,2,3],[4,5,6]) 8 for i in b: 9 print(i) #打印结果为5,7,9 10 11 def f1(x,y): 12 return (x,y) 13 l1=[0,1,2,3,4,5,6] 14 l2=['sun','m','w','s','f','t','a'] 15 c=map(f1,l1,l2) 16 for i in c: 17 print(i) 18 # 打印结果 19 # (0, 'sun') 20 # (1, 'm') 21 # (2, 'w') 22 # (3, 's') 23 # (4, 'f') 24 # (5, 't') 25 # (6, 'a') 26 27 28 #####filter过滤筛选功能,最终返回的结果包含调用结果为True的元素##### 29 a=filter(lambda x:x%2,[1,2,3,4,5]) #0是False,1是True 30 for i in a: 31 print(i) #打印结果为1,3,5 32 33 b=filter(None,'she') #不过滤,所有的东西全取 34 for i in b: 35 print(i) #打印结果为s,h,e 36 37 38 #####reduce,可运用于累计和及阶乘#### 39 from functools import reduce 40 a=reduce(lambda x,y:x+y,[1,2,3,4]) 41 print(a) #打印结果为10 42 43 b=reduce(lambda x,y:x+y,[1,2,3,4],5) 44 print(b) #打印结果为15 45 46 c=reduce(lambda x,y:x+y,['aa','bb','cc'],'dd') 47 print(c) #打印结果为ddaabbcc 48 49 50 #####sort/sorted#### 51 a=[12,34,53,3,22,1,4,456,44,66,690] 52 53 a.sort() 54 print(a) #打印结果为[1, 3, 4, 12, 22, 34, 44, 53, 66, 456, 690] 55 a.sort(reverse=True) 56 print(a) #打印结果为[690, 456, 66, 53, 44, 34, 22, 12, 4, 3, 1] 57 58 print(sorted(a)) #打印结果为[1, 3, 4, 12, 22, 34, 44, 53, 66, 456, 690] 59 b=sorted(a) 60 print(b) #打印结果为[1, 3, 4, 12, 22, 34, 44, 53, 66, 456, 690] 61 print(sorted(a,reverse=True)) #打印结果为[690, 456, 66, 53, 44, 34, 22, 12, 4, 3, 1] 62 c=sorted(a,reverse=True) 63 print(c) #打印结果为[690, 456, 66, 53, 44, 34, 22, 12, 4, 3, 1]
1 '''集合''' 2 a='abcdef' 3 A=set(a) 4 print(A) #{'f', 'a', 'c', 'd', 'e', 'b'} 5 6 b='defghijk' 7 B=set(b) 8 print(B) #{'d', 'i', 'h', 'g', 'e', 'k', 'f', 'j'} 9 10 #交集 11 c=A&B 12 print(c) #{'e', 'd', 'f'} 13 14 #并集 15 d=A|B 16 print(d) #{'b', 'f', 'k', 'j', 'c', 'h', 'g', 'i', 'd', 'a', 'e'} 17 18 #差集 19 e=A-B 20 print(e) #{'a', 'c', 'b'} 21 22 #对称差集 23 f=A^B 24 print(f) #{'k', 'b', 'c', 'g', 'i', 'a', 'j', 'h'} 25 26 C=[1,2,3,3,5,3,4,5,6] 27 print(set(C)) #{1, 2, 3, 4, 5, 6} 28 print(list(set(C))) #[1, 2, 3, 4, 5, 6]
1 '''functools''' 2 ####偏函数functools.partial-省略参数的传递 3 import functools 4 def showarg(*args,**kwargs): 5 print(args) 6 print(kwargs) 7 8 p1=functools.partial(showarg,1,2,3) 9 p1() 10 p1(4,5,6) 11 p1(a='python',b='idcast') 12 print('=================') 13 14 p2=functools.partial(showarg,a=3,b='linux') 15 p2() 16 p2(1,2) 17 p2(a='python',b='idcast') 18 19 #打印结果 20 # (1, 2, 3) 21 # {} 22 # (1, 2, 3, 4, 5, 6) 23 # {} 24 # (1, 2, 3) 25 # {'a': 'python', 'b': 'idcast'} 26 # ================= 27 # () 28 # {'a': 3, 'b': 'linux'} 29 # (1, 2) 30 # {'a': 3, 'b': 'linux'} 31 # () 32 # {'a': 'python', 'b': 'idcast'}
posted on 2019-08-12 22:09 cherry_ning 阅读(148) 评论(0) 收藏 举报
浙公网安备 33010602011771号