zoukankan      html  css  js  c++  java
  • 自己不熟悉的内置函数总结

    all()
    Return True if bool(x) is True for all values x in the iterable.

    If the iterable is empty, return True.
    '''

    '''
    any()
    Return True if bool(x) is True for any x in the iterable.

    If the iterable is empty, return False.

    # dir() 打印当前程序的所有变量

    # divmod(x, y, /) Return the tuple (x//y, x%y).
    # print(divmod(10, 3))  # (3,1)

    # sorted(iterable, /, *, key=None, reverse=False)。自定义排序。
    d = {}
    for i in range(20):
        d[i] = i - 50
    d = sorted(d.items(), key=lambda x: x[1], reverse=True)  # 按字典的value排序后反转
    eval(source, globals=None, locals=None, /)  字符串转代码,只能执行单行代码
    f = "1+3/2"
    print(eval(f))  # 2.5
    
    f2 = "lambda x, y: print(x - y) if x > y else print(x + y)"
    f2 = eval(f2)
    f2(5, 10)  # 15
    exec() 功能和eval差不多。但有两个区别:1.可以执行多行代码 2.拿不到返回值
    f3 = '''
    def foo():
        print('run foo')
        return 1234
    foo()
    '''
    
    res = exec(f3)  # run foo
    print('res', res)  # res None
    
    res2 = eval('1+3+3')
    res3 = exec('1+3+3')
    print(res2, res3)  # 7 None
    ord 返回在ASCII码的位置

    chr 返回ASCII数字对应的字符

    bytearray 返回一个新字节数组。这个数组里的元素是可变的,并且每个元素的值范围: 0 <= x < 256,其实就是ASCII表。
    s = 'Hello world'
    
    # s[0] = 'H' 报错
    s = s.encode('utf-8')
    s = bytearray(s)
    s[0] = 65  # 65是ASCII码表中的A)
    print(s)  # bytearray(b'Aello world')
    s = s.decode()
    print(s)  # Aello world

    map

    print(list(map(lambda x: x * x, [1, 2, 3, 4, 5])))  # [1, 4, 9, 16, 25]
    filter
    print(list(filter(lambda x: x > 3, [1, 2, 3, 4, 5])))  # [4, 5]
    reduce
    import functools
    
    print(functools.reduce(lambda x, y: x + y, [1, 2, 3, 4, 5, ]))  # 15
    print(functools.reduce(lambda x, y: x * y, [1, 2, 3, 4, 5, ]))  # 120
    print(functools.reduce(lambda x, y: x + y, [1, 2, 3, 4, 5], 10))  # 25 第三个参数会放在列表的第一个位置
    print(functools.reduce(lambda x, y: x * y, [1, 2, 3, 4, 5, ], 10))  # 1200 第三个参数会放在列表的第一个位置
    # pow 返回多少次幂
    print(pow(2, 4))  # 16


    print
    print('a', 'b')  # a b
    print('a', 'b', sep='--->')  # a--->b
    
    msg = '又回到最初的起点'
    
    with open('print_test.txt', 'w', encoding='utf-8') as f:
        print(msg, '记忆中你青涩的脸', sep='|', end="", file=f)
    # 又回到最初的起点|记忆中你青涩的脸    写入了文件
    callable()  # 是否可调用,可以用来判断一个变量是否是函数

    frozenset 把一个集合变成不可变的

    vars() 打印出当前所有的变量名加上变量对应的值,dir()只会打印变量名

    locals() 打印局部变量

    globals() 打印全局变量

    repr 把显示形式变成字符串

    zip 让两个列表一一对应
    a = [1, 2, 3, 4, 5]
    b = ['a', 'b', 'c', 'm']
    print(list(zip(a, b)))  # [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'm')]

    round 保留几位小数

    print(round(1.12312312, 2))  #  1.12

  • 相关阅读:
    Hive初识(一)
    图解HTTP总结(8)——确认访问用户身份的认证
    Android 7.0 照相 FileUriExposedException
    activity跳转的一些坑
    gopath配置
    android项目中记录
    一些趣味性总结(JAVA)
    http的response遇到illegalstateexception解决办法
    django demo
    Error:Execution failed for task ':app:transformClassesWithDexForDebug'解决方法
  • 原文地址:https://www.cnblogs.com/lshedward/p/9964594.html
Copyright © 2011-2022 走看看