python 入坑路-内置函数

python 内置函数跟随pyton 启动而启动

1.abs()  绝对值

 abs(10)
 10

 abs(-10)
 10

2.all()   如果这个可迭代对象所有元素为真,返回True

 

all([1,2,3,-1,3,-2])
True

all([0])
False

3.any()  与all 相反,有一个元素为真,即为真

any([1,0])
True

any([0])
False

 4.ascii()  变成字符串,据说没有卵用

In [33]: a=[1,2,3]

In [34]: b=ascii(a)

In [35]: print(b)
[1, 2, 3]

In [36]: type(b)
Out[36]: str

 5.bin(x) 十进制转二进制

 bin(2)
 '0b10'

 bin(4)
 '0b100'

 bin(255)
 '0b11111111'

 6.bool()  判断真假,0为假,空位假,非0即真

bool(1)
True

bool(2)
True

bool(0)
False

bool([])
False

 7.bytearray()

In [44]: a=bytes("abc",encoding="utf-8")

In [45]: print(a.capitalize(),a)
b'Abc' b'abc'

#by the way  字符串不能修改,会生成一个新的,覆盖原来

#bytesarray 可以修改二进制字节格式

In [46]: b=bytearray("abc",encoding="utf-8")

In [47]: print(b[0])
97

In [48]: b[1]=100  #必须是ascii 吗 

In [49]: b
Out[49]: bytearray(b'adc')# b 变成d

 8.callable()  判断是否可以调用,by the way: 可以加() 表示可以调用,如函数,类?

In [50]: print(callable([]))
False

In [51]: def test():
    ...:     pass
    ...:

In [52]: print(callable(test))
True

 9.chr()  返回ascii 码的对应表

 chr(9)
 '\t'

 chr(100)  #必须是数字
 'd'

#ord()  把 ascii 字符返回 ascii 码

ord("a") #只能一个一个字符来,
97

 10.compile()  底层编译,把字符串变成可执行代码,从远程传代码,然后执行,黑客?

直接 使用exec()  执行

 code="for i in range(10):print(i)"

 code
 'for i in range(10):print(i)'

 compile(code," ","exec")
 <code object <module> at 0x03837D88, file " ", line 1>

 11.complex 复数

12.dict()  生成一个空字典

a=dict()  

#by the way  查看可用方法 dir(a) ,前面带__一般是内置方法,__next__() 例外

13.divmod(a,b)   a/b 返回商和余数。

 divmod(5,2)
 (2, 1)

 divmod(7,2)
 (3, 1)

 14.eval   把字符串变成字典, 把简单的字符串,变成可以执行的代码, 复杂带语句例如for 等就需要用exec

 1 s="{'a':1,'b':2}"
 2 
 3 s
 4 "{'a':1,'b':2}"
 5 
 6 d=eval(s)
 7 
 8 d
 9 {'a': 1, 'b': 2}
10 
11 d.keys()
12 dict_keys(['a', 'b'])
13 
14 d.values
15 <function dict.values>

15,exec() 同上

16filter() 一组数据过滤出你想要的,

In [16]: filter(lambda n:n>5,range(10))
Out[16]: <filter at 0x34ce350>

In [17]: res=filter(lambda n:n>5,range(10))

In [18]: for i  in res:
    ...:     print(i)
    ...:
6
7
8
9

 by the way: map()   把传进去的值,做lambda 函数处理

res=map(lambda n:n*2,range(10))
for i in res:
    print(i)

#相当于 [i*2 for i in range(10)]

In [21]: import functools

In [22]: res=functools.reduce(lambda x,y:x*y,range(1,10))

In [23]: print(res)
362880

 17.frozenset 不可变集合

 set([1,2,3,4,2,3,1])
 {1, 2, 3, 4}

 a=set([1,2,3,4,2,3,1])

 a
 {1, 2, 3, 4}

 b=frozenset([1,2,3,1])

 b
 frozenset({1, 2, 3})

 b.

 18 globals 返回一个字典,返回当前程序所有变量名:key  ,值:values

print(globals())   locals 对应,

#判断变量是否存在,也可以做修改。

 19.hash  将没有规则的 数据,映射到一个规则的序列,方便查找。例如用对折猜数法,或更高级的算法。字典key 查找也是通过这个原理

 

by the way: 对折,猜数算法。例如:给一个100内的数据。98

   1,猜 50, 如果小了,

  2,再猜 75,小了

  3,再猜 88,小了

  4,再猜 94, 小了

  5,再猜97,小了

  6,再猜99,大了

  7, 猜中 98  只需要7次判断,比循环100 次快。  数据量越大,效果越明显。

hash("b")
337795230

hash("a")
1862767850

 20.locals  ,相对globals  ,

In [35]: def test():
    ...:     local_var=333
    ...:     print(locals())
    ...:

In [36]: test()
{'local_var': 333}

 by the way: python 一切皆对象,通过 "."  调用方法。

21.oct()  10进制,转8进制

 

oct(9)
'0o11'

oct(8)
'0o10'

oct(1)
'0o1'

 22.pow(a,b)  放回 a 的 b 次方  a**b

23.repr()  用字符串表示这个对象

 a
 {1, 2, 3, 4}

 repr(a)
 '{1, 2, 3, 4}'

 24. reversed()  反转,跟列表里的reverse 一样

25.round()  看下面例子,发现问题了吗,4舍6入5取偶数

 round(1.335,2)
 1.33

 round(1.345,2)
 1.34

 round(1.345,2)
 1.34

 round(1.346,2)
 1.35

 round(1.355,2)
 1.35

 round(1.355,2)
 1.35

 26.sorted()  字典排序,默认按key 排序,也可以指定按 value 排序。

 dict1
 {-6: 0, -5: 1, -4: 90, -1: 20, 0: -4, 1: 10, 6: 2, 8: 0}

#默认按照key  排序,输出类表
 sorted(dict1.items())
 [(-6, 0), (-5, 1), (-4, 90), (-1, 20), (0, -4), (1, 10), (6, 2), (8, 0)]

#按照vula
sorted(dict1.items(),key=lambda x:x[1])
[(0, -4), (8, 0), (-6, 0), (-5, 1), (6, 2), (1, 10), (-1, 20), (-4, 90)]

 27.zip()  把两个列表一一对应组合在一起。如果一个类表比较多元素,按照少的来。

 

In [76]: a=[1,2,3,4]

In [77]: b=['a','b','c','d']

In [78]: zip(a,b)
Out[78]: <zip at 0x356fd78>

In [79]: for i in zip(a,b):
    ...:     print(i)
    ...:
(1, 'a')
(2, 'b')
(3, 'c')
(4, 'd')

In [80]: c=['a','b','c','d','e']

In [81]: zip(a,c)
Out[81]: <zip at 0x3a8f580>

In [82]: for i in zip(a,c):
    ...:     print(i)
    ...:
(1, 'a')
(2, 'b')
(3, 'c')
(4, 'd')

 by the way :map() 有一个语法,可以功能一一,但是,当一个列表少元素时,按多的来,赋默认值。

28.import    __import__("m_name")  知道大概的名字,

 

posted @ 2018-01-03 14:15  东郭仔  阅读(171)  评论(0)    收藏  举报