zoukankan      html  css  js  c++  java
  • Python之路——内置函数

    内置函数一

    
    
    # ************************************************************1
    # sorted
    # 对list、dict进行排序,有两个方法
    # 1.用list成员函数sort进行排序,在本地进行排序,不返回副本,默认按升序排序
    # 2.用built-in函数sorted进行排序(从2.4开始),返回副本,原始输入不变
    # sorted(iterable, key=None, reverse=False)
    # 参数说明:
    # iterable:是可迭代类型;
    # key:传入一个函数名,函数的参数是可迭代类型中的每一项,根据函数的返回值大小排序;
    # reverse:排序规则. reverse = True 降序 或者 reverse = False 升序,有默认值。
    # 返回值:有序列表
    # 例一
    # l1 = [1,2,3,-1,-2,-4,-6]
    # l2 = sorted(l1,key=abs)
    # print(l2)
    # # 例二
    # l = [[1,2],[3,4,5,6],(7,),'123']
    # print(sorted(l,key=len))
    # ************************************************************2
    # repr:Return the canonical string representation of the object.
    # For many object types, including most builtins, eval(repr(obj)) == obj.
    # a = [1,2,3,4,5,[23,3,5,'sdgsdf']]
    # b = repr(a) # b = '[1, 2, 3, 4, 5, [23, 3, 5, 'sdgsdf']]'
    # c = eval(b) # c = [1, 2, 3, 4, 5, [23, 3, 5, 'sdgsdf']]
    # print(type(a)) # <class 'list'>
    # print(type(b)) # <class 'str'>
    # print(type(c)) # <class 'list'>
    # ************************************************************3
    # round: 取小数位数,默认取0为小数,会自动四舍五入
    # 被舍掉的第一位大于5时才会进1,round(0.50) == 0.
    # print(round(0.9)) # 1:int
    # print(round(1.9,1)) # 1.9
    # print(round(1.944,2)) # 1.9
    # print(round(0.50)) # 0
    # ************************************************************4
    # # sum(iterable[,start = 0])
    # print(sum([1,2,3,4,5,6])) # 21: 默认start为0
    # print(sum([1,2,3,4,5,6],10)) # 31 start = 10
    # ************************************************************5
    # min
    # print(min(1,2,3,4,5,6))# 可以接收多个参数
    # print(min([1,2,3,44,5,6,787]))# 也可以接收一个iterable
    # print(min((23,4,5,6,76,676543)))
    # print(min([[1,2,3],[12],[23,4213,45,3]],key=len)) # [12]
    # ************************************************************6
    # max
    # print(max(1,2,3,4,5,6))# 可以接收多个参数
    # print(max(1,2,-8,3,4,5,6,key=lambda x:x*x)) # -8
    # print(max([1,2,3,44,5,6,787]))# 也可以接收一个iterable
    # print(max((23,4,5,6,76,676543)))
    # print(max([[1,2,3],[12],[23,4213,45,3]],key=len)) # [23, 4213, 45, 3]
    # ************************************************************7
    # filter: filter(function or None, iterable) --> filter object
    # Return an iterator yielding those items of iterable for which function(item)
    # is true. If function is None, return the items that are true.
    # def is_odd(x):
    # return x%2 ==1
    # # print(is_odd(1))
    # a = filter(is_odd,[1,2,3,4,6,7,34,63,23]) #
    # print(type(a))
    # for i in a:
    # print(i)
    # print(list(filter(lambda x:x%2==1,[1,2,3,4,6,7,32,453,23])))
    # ************************************************************8
    # map: map(func, *iterables) --> map object
    # Make an iterator that computes the function using arguments from
    # each of the iterables. Stops when the shortest iterable is exhausted.
    # print(list(map(abs,[1,-3,2,3,-6,2.5]))) # [1, 3, 2, 3, 6, 2.5]
    # print(list(map(lambda x,y:x+y,[1,2,3,4,5],[1,2,3,4,5]))) # [2, 4, 6, 8, 10]
    # ************************************************************9
    # abs 求绝对值
    # print(abs(-5))
    # ************************************************************10
    # divmod : Return the tuple ((x-x%y)/y, x%y)
    # print(divmod(7,2)) # (3, 1)
    # # print(divmod(9,5)) # (1, 4)
    # ************************************************************11
    # pow: Equivalent to x**y (with two arguments) or x**y % z (with three arguments)
    # print(pow(2,3)) # 2**3
    # print(pow(3,4,2)) # (3**4)%2
    # ************************************************************12+2
    # ord: Return the Unicode code point for a one-character string.
    # print(ord('1')) # 49
    # print(ord('a')) # 97
    # print(ord('中')) # 20013
    # chr:
    # print(chr(97)) # a
    # print(chr(20013)) # 中
    # ascii(o) # 返回一个对象的字符串表示,属于ascii的,原样表示,不属于ascii的,用'u'打标记
    # print(ascii(' ')) # ' '
    # print(ascii('wjlwr我爱你')) # 'wjlwru6211u7231u4f60'
    # lst = [1,23,32,'asdkf我爱你''aslkdf']
    # l2 = ascii(lst)
    # print(l2,type(l2)) # [1, 23, 32, 'asdkfu6211u7231u4f60aslkdf'] <class 'str'>


    # ************************************************************13
    # format
    # print(format(3.1415936))
    # print(str(3.1415936))

    # 字符串可以提供的参数,指定对齐方式,<是左对齐, >是右对齐,^是居中对齐
    # print(format('test','<20'))
    # print(format('test','>20'))
    # print(format('test','^20'))

    # 整形数值可以提供的参数有 'b' 'c' 'd' 'o' 'x' 'X' 'n' None
    # print(format(3,'b')) # 转换成二进制
    # print(format(97,'c')) # 转换成uinicode字符
    # print(format(11,'d')) # 转换成十进制
    # print(format(11,'o')) # 转换成八进制
    # print(format(11,'x')) # 转换成16进制,字母用小写表示
    # print(format(11,'X')) # 转换成16进制,字母用大写表示
    # print(format(11,'n')) # 和d一样
    # print(format(11)) # 默认和d一样

    # #浮点数可以提供的参数有 'e' 'E' 'f' 'F' 'g' 'G' 'n' '%' None
    # print(format(314149267,'e')) #科学计数法,默认保留6位小数
    # print(format(314149267,'0.8e')) #科学计数法,指定保留8位小数
    # print(format(314159267,'0.2E')) # 科学计数法,指定保留2位小数,采用大写E表示
    # print(format(314159267,'f')) # 小数点表示法,默认保留6位小数
    # print(format(3.14159267000,'f')) # 小数点表示法,默认保留6位小数
    # print(format(3.14159267000,'0.8f')) # 小数点表示法,指定保留8位小数
    # print(format(3.14159267000,'0.10f')) # 小数点表示法,指定保留10位小数
    # print(format(3.14e+10000,'F')) # 小数点表示法,无穷大换成大写字母

    #g的格式化比较特殊,假设p为格式中指定的保留小数位数,先尝试采用科学计数法格式化,得到幂指数exp,
    # 如果-4<= exp <p,则采用小数计数法,并保留p-1-exp位小数,否则按小数计数法计数,并按p-1保留小数位数
    # print(format(314156.9267,'0.3g')) # 3.14e+05
    # print(format(3.141569267,'0.3g'))
    # ************************************************************14
    # zip: 返回一个可迭代的zip对象,如果迭代完后继续取数据,会产生StopIteration
    # zip(iter1 [,iter2 [...]]) --> zip object
    # l = [1,2,3,4]
    # l2 = ['a','b','c']
    # l3 = ['q','e']
    # print(list(zip(l))) # [(1,), (2,), (3,), (4,)]
    # print(list(zip(l,l2,l3))) # [(1, 'a', 'q'), (2, 'b', 'e')]
    # ************************************************************15,16,17
    # eval、exec和compile
    # exec('print(123)')
    # eval('print(123)')
    # print(eval('1+2+3+4'))
    # print(exec('1+2+3+4')) # None
    # exec 和eval都可以执行字符串类型的代码
    # eval有返回值 -- 有结果的简单计算
    # exec没有返回值 -- 简单流程控制
    # eval只能用在你明确知道你要执行的代码是什么

    # code = '''for i in range(10):
    # print(i*'*')'''
    # exec(code)
    # # eval(code) # SyntaxError: invalid syntax

    # code1 = 'for i in range(0,10):print(i)'
    # compile1 = compile(code1,'','exec')
    # exec(compile1)
    # eval(compile1) # 可以正确执行

    # code2 = '1+2+3+4'
    # compile2 = compile(code2,'','eval')
    # print(eval(compile2))

    # code3 = "name = input('please input your name:')"
    # compile3 = compile(code3,'','single')
    # exec(compile3)
    # print(name)
    # ************************************************************18
    # all(iterable):判断iterable里面有没有bool值为False的元素,
    # 有则返回False,如果参数为[],()等,则返回True
    # print(all([1,2,3,4,0,''])) # False
    # print(all([])) # True
    # ************************************************************19
    # any
    # print(any([])) # False
    # print(any([(),[],'',1]))
    # ************************************************************20
    # enumerate(iterable,start = 0),每次__next__返回一个元组(x_index,x)
    # a = enumerate([12,3,34,5,4])
    # print(list(a)) # [(0, 12), (1, 3), (2, 34), (3, 5), (4, 4)]
    # b = enumerate([12,3,34,5,4],start=10)
    # print(list(b)) # [(10, 12), (11, 3), (12, 34), (13, 5), (14, 4)]
    # c = {'k1':'v1','k2':'v2'}
    # print(list(enumerate(c))) # [(0, 'k2'), (1, 'k1')]
    # print(list(enumerate(c.items()))) # [(0, ('k1', 'v1')), (1, ('k2', 'v2'))]
    # print(list(c.items())) # [('k2', 'v2'), ('k1', 'v1')]
    # print(list(enumerate(c.values()))) # [(0, 'v1'), (1, 'v2')]
    # ************************************************************21
    # set:可以用于列表去重
    # 对list和tuple,如果要求去重后顺序不变,则用下面的代码:
    # lst = [1,2,3,4,54,1,22,3,32,45,23,13,1]
    # new_list = []
    # for i in lst:
    # if i not in new_list:
    # new_list.append(i)
    # print(new_list)
    # ************************************************************22
    # frozenset(iterable),将iterable对象转会为不可变的集合,可以作为字典的key
    # print(frozenset([1,2,3])) # frozenset({1, 2, 3})
    # print(frozenset('adsab')) # frozenset({'d', 'a', 'b', 's'})
    # print(frozenset({1,23,4})) # frozenset({1, 4, 23})
    # ************************************************************23
    # slice
    # a = slice(10) # a = slice(None, 10, None)
    # a1 = slice(1,5) # a1 = slice(1, 5, None)
    # a2 = slice(2,10,2) # a2 = slice(2, 10, 2)
    # b = list(range(20))
    # print(b[a]) # b[0:10]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    # print(b[a1]) # b[1:5]: [1, 2, 3, 4]
    # print(b[a2]) # b[2:10:2]: [2, 4, 6, 8]
    # ************************************************************24
    # reversed
    # a = [1,2,3,4523,23,42,34]
    # b = reversed(a) # 返回一个迭代器
    # print(list(b)) # [34, 42, 23, 4523, 3, 2, 1]
    # print(a) # a 不变
    # ************************************************************25
    # memoryview:需要接收一个bytes object
    # v= memoryview(bytes('hello,eva',encoding='utf-8'))
    # print(v) # <memory at 0x0000025130B52F48>
    # print(v[1]) # 101
    # print(v[1:4])# <memory at 0x0000013B5A35E048>
    # print(v[1:4].tobytes()) # b'ell'
     
  • 相关阅读:
    处理某客户p570硬盘故障所思
    Android手机使用WIFI及USB建立FTP服务器总结
    Metro界面的真正意义
    找工作之面试血泪史
    vim7.4官方源码在vs2013的编译方法及问题总结
    关于vs2012/2013的C编译器生成的exe的向后兼容xp的问题
    【转】一篇关于32位Linux内核使用大内存的文章——Hugemem Kernel Explained  &nb
    直接修改Android软件数据库来改变软件设置实例一则
    解决MyEclipse中安装或升级ADT之后SDK Target无法显示的问题
    国行Android手机使用google全套GMS服务小结
  • 原文地址:https://www.cnblogs.com/liuyankui163/p/8229005.html
Copyright © 2011-2022 走看看