zoukankan      html  css  js  c++  java
  • 内置函数

    reverse()

    l = [1,2,3,4,5] l.reverse() print(l) l = [1,2,3,4,5] l2 = reversed(l) print(l2) # 保留原列表,返回一个反向的迭代器

    bytes()

    print(bytes('你好',encoding='GBK'))     # unicode转换成GBK的bytes
    print(bytes('你好',encoding='utf-8'))   # unicode转换成utf-8的bytes
    map,
    filter
    def is_odd(x):
        return x % 2 == 1
    
    def is_str(s):
        return s and str(s).strip()
    ret = filter(is_odd, [1,  6, 7, 12, 17])
    ret = filter(is_str, [1, 'hello','','  ',None,[], 6, 7, 'world', 12, 17])
    print(ret)
    for i in ret:
        print(i)
    [i for i in [1, 4, 6, 7, 9, 12, 17] if i % 2 == 1]
    
    from math import sqrt
    def func(num):
        res = sqrt(num)
        return res % 1 == 0
    ret = filter(func,range(1,101))
    for i in ret:
        print(i)
    ret = map(abs,[1,-4,6,-8])
    print(ret)
    for i in ret:
    print(i)


    # filter 执行了filter之后的结果集合 <= 执行之前的个数
    #filter只管筛选,不会改变原来的值
    # map 执行前后元素个数不变
    # 值可能发生改变
    sorted()
    l = [1,-4,6,5,-10]
    # l.sort(key = abs)   # 在原列表的基础上进行排序
    # print(l)
    
    print(sorted(l,key=abs,reverse=True))      # 生成了一个新列表 不改变原列表 占内存
    print(l)
    
    l = ['   ',[1,2],'hello world']
    new_l = sorted(l,key=len)
    print(new_l)

     匿名函数

    ret = zip((('a'),('b')),(('c'),('d')))
    res = map(lambda tup:{tup[0]:tup[1]},ret)
    print(list(res))
    
    def multipliers():
        return [lambda x:i*x for i in range(4)]
    print([m(2) for m in multipliers()])
  • 相关阅读:
    图像修补
    图像的矩
    使用多边形将轮廓包围
    寻找物体的凸包
    查找并绘制轮廓
    重映射
    霍夫变换
    边缘检测
    第二周神经网络基础
    第一周:深度学习引言(Introduction to Deep Learning)
  • 原文地址:https://www.cnblogs.com/Celebrator/p/9846065.html
Copyright © 2011-2022 走看看