L=[‘Jack‘,‘Mick‘,‘Leon‘,‘Jane‘,‘Aiden‘]
N=[0,1,2,3,4,5,6,7,8,9]
map()
函数接收两个参数,一个是函数,一个是Iterable
,map
将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator
返回。
>>> def f(x): ... return x*x ... >>> r = map(f,[0,1,2,3,4,5,6,7,8,9]) >>> list(r) [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
reduce
把一个函数作用在一个序列[x1, x2, x3, ...]
上,这个函数必须接收两个参数,reduce
把结果继续和序列的下一个元素做累积计算>>> from functools import reduce >>> def add(x,y): ... return x+y ... >>> reduce(add,[1,3,5,7,9]) 25
filter()
把传入的函数依次作用于每个元素,然后根据返回值是True
还是False
决定保留还是丢弃该元素。>>> def is_odd(n): ... return n % 2 == 1 ... >>> list(filter(is_odd,[1,2,4,5,6,9,10,15])) [1, 5, 9, 15]
key
函数来实现自定义的排序,例如按绝对值大小排序>>> sorted([36,5,-12,9,-21],key=abs) #按abs的结果升序排列 [5, 9, -12, -21, 36]
>>> sorted([‘BOB‘,‘aBOUT‘,‘zoo‘,‘Credit‘]) #按首字母的ASICC码 [‘BOB‘, ‘Credit‘, ‘aBOUT‘, ‘zoo‘]
lambda
表示匿名函数,冒号前面的x
表示函数参数return
,返回值就是该表达式的结果>>> f = lambda x:x*x >>> f(5) 25
1 #定义一个Student的对象 2 class Student(Object): 3 #初始化对象的属性 4 def __init__(self,name,score): 5 self.name = name 6 self.score = score 7 8 #定义一个对象的方法print_score 9 def print_score(self): 10 print(‘%s: %s‘%(self.name,self.score))
1 #定义一个Student的对象 2 class Student(Object): 3 #初始化对象的属性 4 def __init__(self,name,score): 5 #私有属性 6 self.__name = name 7 self.__score = score 8 9 #定义一个对象的函数print_score 10 def print_score(self): 11 print(‘%s: %s‘%(self.__name,self.__score))
>>> a=‘AAA‘ >>> type(a) <class ‘str‘>
>>> isinstance(‘a‘,str) True >>> isinstance(123,int) True >>> isinstance(123,str) False
>>> dir(‘a‘) [‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__getnewargs__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__mod__‘, ‘__mul__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__rmod__‘, ‘__rmul__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘capitalize‘, ‘casefold‘, ‘center‘, ‘count‘, ‘encode‘, ‘endswith‘, ‘expandtabs‘, ‘find‘, ‘format‘, ‘format_map‘, ‘index‘, ‘isalnum‘, ‘isalpha‘, ‘isdecimal‘, ‘isdigit‘, ‘isidentifier‘, ‘islower‘, ‘isnumeric‘, ‘isprintable‘, ‘isspace‘, ‘istitle‘, ‘isupper‘, ‘join‘, ‘ljust‘, ‘lower‘, ‘lstrip‘, ‘maketrans‘, ‘partition‘, ‘replace‘, ‘rfind‘, ‘rindex‘, ‘rjust‘, ‘rpartition‘, ‘rsplit‘, ‘rstrip‘, ‘split‘, ‘splitlines‘, ‘startswith‘, ‘strip‘, ‘swapcase‘, ‘title‘, ‘translate‘, ‘upper‘, ‘zfill‘]
>>> class Student(object): ... name = ‘Student‘ ... >>> s = Student() >>> print(s.name) Student >>> print(Student.name) Student >>> s.name = ‘Jack‘ >>> print(s.name) Jack >>> print(Student.name) Student
1 class Dict(dict): 2 3 def __init__(self,**kw): 4 super().__init__(**kw) 5 6 7 def __getattr__(self,key): 8 try: 9 return self[key] 10 except KeyError: 11 raise AttributeError(r"‘Dict‘ object has no attribute %s" %key) 12 13 def __setattr__(self,key,value): 14 self[key] = value
1 import unittest 2 3 from mydict import Dict 4 5 class TestDict(unittest.TestCase): 6 7 def test_init(self): 8 #完成数据初始化 9 d = Dict(a=1,b=‘test‘) 10 #判断d.a是否为1,为1则PASS,否则FAIL 11 self.assertEqual(d.a,1) 12 #判断d.b是否为‘test‘ 13 self.assertEqual(d.b,‘test‘) 14 self.assertTrue(isinstance(d,dict)) 15 16 17 18 if __name__ == ‘__main__‘: 19 unittest.main()
? Python python3 TestDict.py . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK
原文:http://www.cnblogs.com/s380774061/p/4690916.html