标签:最大 打印 个数 super 复杂 fine osi and bool
由于python内置函数较多,在此总结一下部分常见的内置函数的用法:
1 >>> abs(0) 2 0 3 >>> abs(-5) 4 5 5 >>>
1 >>> all([1,2,3]) 2 True 3 >>> all([0,1,2,3]) 4 False 5 >>>
1 >>> any([‘a‘,‘b‘,‘c‘]) 2 True 3 >>> any([]) #空列表为假 4 False 5 >>> any([0]) 6 False 7 >>> any([0,1]) 8 True 9 >>>
1 >>> bin(100) 2 ‘0b1100100‘ 3 >>> bin(1) 4 ‘0b1‘ 5 >>>
1 >>> bool(‘x‘) 2 True 3 >>> bool(‘‘) 4 False 5 >>> bool(0) 6 False 7 >>>
1 >>> list1=bytearray(‘001‘,encoding=‘utf-8‘) 2 >>> list1 3 bytearray(b‘001‘) 4 >>> list1[0] #以列表方式访问元素,返回的是对应的ASCII码 5 48 6 >>> list1[0]=99 #更新时也必须以ASCII码的形式来更新 7 >>> list1 8 bytearray(b‘c01‘) #更新后原列表变了 9 >>>
1 >>> b = bytes("abcd",encoding="utf-8") 2 >>> b 3 b‘abcd‘ 4 >>> b[1] 5 98 6 >>> b[1]=99 7 Traceback (most recent call last): 8 File "<stdin>", line 1, in <module> 9 TypeError: ‘bytes‘ object does not support item assignment
1 >>> def func1(): 2 ... pass 3 ... 4 >>> callable(func1) 5 True 6 >>> a=‘test‘ 7 >>> callable(a) 8 False
1 >>> chr(98) 2 ‘b‘
1 >>> ord(‘c‘) 2 99
1 >>> dict 2 <class ‘dict‘> 3 >>> dict({(‘address‘,‘Chengdu‘),(‘Num‘,‘028‘)}) #传入一个set 4 {‘address‘: ‘Chengdu‘, ‘Num‘: ‘028‘} 5 >>> dict([(‘address‘,‘Chengdu‘),(‘Num‘,‘028‘)]) #传入list 6 {‘address‘: ‘Chengdu‘, ‘Num‘: ‘028‘} 7 >>> dict([[‘address‘,‘Chengdu‘],[‘Num‘,‘028‘]]) 8 {‘address‘: ‘Chengdu‘, ‘Num‘: ‘028‘} 9 >>> dict(((‘address‘,‘Chengdu‘),(‘Num‘,‘028‘))) #传入元组 10 {‘address‘: ‘Chengdu‘, ‘Num‘: ‘028‘} 11 >>>
1 >>> a=dict(((‘address‘,‘Chengdu‘),(‘Num‘,‘028‘))) 2 >>> dir(a) 3 [‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__delitem__‘, ‘__dir__‘, ‘__doc__‘ 4 , ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__gt__‘, 5 ‘__hash__‘, ‘__init__‘, ‘__init_subclass__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘ 6 __lt__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__seta 7 ttr__‘, ‘__setitem__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘clear‘, ‘co 8 py‘, ‘fromkeys‘, ‘get‘, ‘items‘, ‘keys‘, ‘pop‘, ‘popitem‘, ‘setdefault‘, ‘update 9 ‘, ‘values‘]
1 >>> divmod(9,2) 2 (4, 1)
1 >>> test=‘abcd‘ 2 >>> for index,item in enumerate(test): 3 ... print(index,item) 4 ... 5 0 a 6 1 b 7 2 c 8 3 d 9 >>> print(list(enumerate(test))) 10 [(0, ‘a‘), (1, ‘b‘), (2, ‘c‘), (3, ‘d‘)] 11 >>> print(list(enumerate(test,start=2))) #start参数人为设定起始索引值,默认从0开始 12 [(2, ‘a‘), (3, ‘b‘), (4, ‘c‘), (5, ‘d‘)] 13 >>> print(dict(enumerate(test))) 14 {0: ‘a‘, 1: ‘b‘, 2: ‘c‘, 3: ‘d‘}
1 >>> eval(‘100‘) 2 100 3 >>> type(eval(‘100‘)) 4 <class ‘int‘> 5 >>> eval("[1,2,3]") 6 [1, 2, 3] 7 >>> eval("(1,2,3)") 8 (1, 2, 3) 9 >>> eval("{1:‘name‘,2:‘male‘,3:‘age‘}") 10 {1: ‘name‘, 2: ‘male‘, 3: ‘age‘}
1 >>> a=filter(lambda x:x %2 == 0, [0,1,2,3,4]) 2 >>> print(a) 3 <filter object at 0x0000000001E9BE48> #经filter处理后返回的是一个filter对象 4 >>> for i in a: 5 ... print(i) 6 ... 7 0 8 2 9 4
1 >>> def func1(x): 2 ... return x * 2 3 ... 4 >>> rs=map(func1,range(5)) 5 >>> for i in rs: 6 ... print(i) 7 ... 8 0 9 2 10 4 11 6 12 8 13 >>> rs 14 <map object at 0x00000000021DBE10>
1 >>> def f(x,y): 2 ... return x+y 3 ... 4 >>> reduce(f,[1,3,5,7,9]) 5 Traceback (most recent call last): 6 File "<stdin>", line 1, in <module> 7 NameError: name ‘reduce‘ is not defined 8 >>> from functools import reduce #注意这里需要先导入后才能使用 9 >>> reduce(f,[1,3,5,7,9]) 10 25 11 >>> reduce(f,[1,3,5,7,9],100) #给出初始值100 12 125
先计算头两个元素:f(1, 3),结果为4;
再把结果和第3个元素计算:f(4, 5),结果为9;
再把结果和第4个元素计算:f(9, 7),结果为16;
再把结果和第5个元素计算:f(16, 9),结果为25;
由于没有更多的元素了,计算结束,返回结果25。
典型的应用场景是:凡是要对一个可迭代独享进行操作的,并且要有一个统计结果的,能够用循环或者递归方式解决的问题,一般情况下都可以用reduce方式实现(比如统计一个集合中key重复的次数)。
1 >>> float(‘a‘) 2 Traceback (most recent call last): 3 File "<stdin>", line 1, in <module> 4 ValueError: could not convert string to float: ‘a‘ #a不是数字形式,无法转换 5 >>> 6 >>> float(‘1‘) 7 1.0 8 >>> float(‘1.23‘) 9 1.23
1 >>> res=frozenset([1,1,2,2,3]) 2 >>> res 3 frozenset({1, 2, 3}) #集合中的元素不可重复,符合集合特征 4 >>> res.add(4) #不能针对该集合增加元素,不可变 5 Traceback (most recent call last): 6 File "<stdin>", line 1, in <module> 7 AttributeError: ‘frozenset‘ object has no attribute ‘add‘ 8 >>> dir(res) #没有可变方法 9 [‘__and__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__dir__‘, ‘__doc__‘, ‘_ 10 _eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__gt__‘, ‘__hash__‘, ‘__ini 11 t__‘, ‘__init_subclass__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__ne__‘, 12 ‘__new__‘, ‘__or__‘, ‘__rand__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__r 13 or__‘, ‘__rsub__‘, ‘__rxor__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__sub__‘ 14 , ‘__subclasshook__‘, ‘__xor__‘, ‘copy‘, ‘difference‘, ‘intersection‘, ‘isdisjoi 15 nt‘, ‘issubset‘, ‘issuperset‘, ‘symmetric_difference‘, ‘union‘] 16 >>>
1 >>> def foo(x): 2 ... global a 3 ... a=10 4 ... b=20 5 ... print(globals()) 6 ... 7 >>> foo() 8 Traceback (most recent call last): 9 File "<stdin>", line 1, in <module> 10 TypeError: foo() missing 1 required positional argument: ‘x‘ 11 >>> foo(1) 12 {‘__name__‘: ‘__main__‘, ‘__doc__‘: None, ‘__package__‘: None, ‘__loader__‘: <cl 13 ass ‘_frozen_importlib.BuiltinImporter‘>, ‘__spec__‘: None, ‘__annotations__‘: { 14 }, ‘__builtins__‘: <module ‘builtins‘ (built-in)>, ‘f‘: <function f at 0x0000000 15 001D23E18>, ‘reduce‘: <built-in function reduce>, ‘res‘: frozenset({1, 2, 3}), ‘ 16 test‘: <function test at 0x00000000023DAEA0>, ‘foo‘: <function foo at 0x00000000 17 02543840>, ‘a‘: 10} #定义的全局变量已经获取打印到 18 19 >>> def foo2(): 20 ... global a 21 ... a=100 22 ... print(globals().get(‘a‘)) #直接获取全局变量的值 23 ... 24 >>> foo2() 25 100
1 >>> def foo(x): 2 ... a=‘hehe‘ 3 ... print(locals()) 4 ... 5 >>> foo(1) 6 {‘a‘: ‘hehe‘, ‘x‘: 1} 7 >>>
1 >>> hash(‘xy‘) 2 6325275833896794579 3 >>> hash(‘xy‘) 4 6325275833896794579 5 >>> hash(‘100‘) 6 -93280025000303673 7 >>> hash(100) 8 100 9 >>>
1 >>> max((1,2,3)) 2 3 3 >>> max({1,2,6}) 4 6
1 >>> max((1,2,3)) 2 3 3 >>> max({1,2,6}) 4 6
1 >>> input(‘:‘) 2 :hehe 3 ‘hehe‘
1 >>> list({1,2,3}) 2 [1, 2, 3]
1 >>> set({1:‘a‘,2:‘b‘}) 2 {1, 2} 3 >>> set(range(5)) 4 {0, 1, 2, 3, 4}
1 >>> hex(16) 2 ‘0x10‘ 3 >>>
1 >>> oct(8) 2 ‘0o10‘ 3 >>> oct(7) 4 ‘0o7‘
1 >>> a={1:‘a‘,2:‘c‘,3:‘b‘} 2 >>> sorted(a) #默认只是排序key 3 [1, 2, 3] 4 >>> sorted(a.items()) #a.items返回的是包含键值对元组的列表,这里还是按key排序 5 [(1, ‘a‘), (2, ‘c‘), (3, ‘b‘)] 6 >>> sorted(a.items(),key=lambda x:x[1]) #指定键值对中的value作为排序的key,因此按value来排序 7 [(1, ‘a‘), (3, ‘b‘), (2, ‘c‘)] 8 >>> print(a.items()) 9 dict_items([(1, ‘a‘), (2, ‘c‘), (3, ‘b‘)])
1 >>> a=[1,2,3] 2 >>> b={‘a‘,‘b‘} 3 >>> for i in zip(a,b): 4 ... print(i) 5 ... 6 (1, ‘a‘) 7 (2, ‘b‘) 8 >>>
1 >>> import os 2 >>> __import__(‘os‘) 3 <module ‘os‘ from ‘C:\\Program Files (x86)\\python3.6.1\\lib\\os.py‘>
标签:最大 打印 个数 super 复杂 fine osi and bool
原文地址:http://www.cnblogs.com/linupython/p/7679771.html