• 逻辑运算&map函数&filter函数&reduce函数


    Python语言支持逻辑运算符,以下假设变量 a 为 10, b为 20:

    运算符逻辑表达式描述实例
    and x and y 布尔"与" - 如果 x 为 False,x and y 返回 False,否则它返回 y 的计算值。 (a and b) 返回 20。
    or x or y 布尔"或" - 如果 x 是 True,它返回 x 的值,否则它返回 y 的计算值。 (a or b) 返回 10。
    not not x 布尔"非" - 如果 x 为 True,返回 False 。如果 x 为 False,它返回 True。 not(a and b) 返回 False
     1 print(1>1 or 3<4 or 4>5 and 2>1 and 9>8 or 7<6)  #or 一个为Ture 结果Ture
     2 print(not 2 >1 and 3<4 or 4>5 and 2>1 and 9>8 or 7<6) # f
     3 print(1>2 and 3<4 or 4>5 and 2>1 or 9<8 and 4>6 or 3<2 ) #f
     4 
     5 print(8 or 3 and 4 or 2 and 0 or 9 and 7) #8
     6 print(0 or 2 and 3 and 4 or 6 and 0 or 3) #4
     7 print( 4  or 3)  #4  #or x y    x or y   x 值为Ture不为0的话, 结果为x
     8 print(5 and 9 or 10 and 2 or 3 and 5 or 4 or 5)  #9
     9 print(6 or 2>1) #6
    10 print(0 or 5< 4) #f
    11 print(5<4 or 3) #3
    12 print(2>1 or 6) #t
    13 print(3 and 2>1) #t
    14 print(2>1 and 0) #0
    15 print(2>1 and 3) #3
    16 print(3>1 and 2 or 2 <3 and 4 or 3>2) #2

    lambda函数:     #匿名函数    处理多个值时,需要自己做元祖处理,也就是自己加括号

    1 func = lambda x:x+1
    2 func(10)
    3 print(func(10))    #输出11
    1 name = ["alex","tom"]
    2 for i in name:
    3     fun=lambda x:x+"_sb"
    4     print(fun(i))   #输出alex_sb   tom_sb
    1 f =lambda x,y,z:(x+1,y+1)   #处理多个值时,要手动变成元祖格式
    2 print(f(1,2,3))     

    函数式编程:

    高阶函数:1、函数接收的参数是一个函数名    2、返回值中包含函数

    map() 会根据提供的函数对指定序列做映射。

    第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表。

    map() 函数语法:

    map(function, iterable, ...)
    1 # map函数
    2 nul = [1,2,3,4,5,6,7]
    3 def map_test(func,array):   #func =lambda x:x+1  #array=[1,2,3,4,5,6,7]
    4     ret=[]
    5     for i in array:
    6         res=func(i)      #调取函数
    7         ret.append(res)
    8     return ret
    9 print(map_test(lambda x:x+1,nul))
    1 ##############map使用################
    2 nl = [1,2,3,4,5,6]
    3 print(list(map(lambda x:x+1,nl)))
    4 ll = "alex"
    5 print(list(map(lambda x:x.upper(),ll)))

    filter() 函数用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表。

    该接收两个参数,第一个为函数,第二个为序列,序列的每个元素作为参数传递给函数进行判,然后返回 True 或 False,最后将返回 True 的元素放到新列表中。

    filter() 方法的语法:

    filter(function, iterable)
    1 ln = ["alex_sb","tom","Jim"]
    2 print(list(filter(lambda x:x.endswith("sb"),ln)))
    3 print(list(filter(lambda x:not x.endswith("sb"),ln)))  #取结尾不含sb的人
    1 #######过滤#########
    2 people =3     {“name”:"alex1","age":100} ,
    4     {“name”:"alex2","age":1000} ,
    5     {“name”:"alex3","age":10000} ,
    6     {“name”:"alex4","age":18} ,
    7 ]
    8 
    9 print(list(filter(lambda p:p["age"]<=18,people)))

    reduce() 函数会对参数序列中元素进行累积。

    函数将一个数据集合(链表,元组等)中的所有数据进行下列操作:用传给 reduce 中的函数 function(有两个参数)先对集合中的第 1、2 个元素进行操作,得到的结果再与第三个数据用 function 函数运算,最后得到一个结果。

    reduce() 函数语法:

     reduce(function, iterable[, initializer])
     
    1 # reduce函数
    2 from functools import reduce   #导入reduce函数模块
    3 ln = [1,2,3,100]
    4 print(reduce(lambda x,y:x*y,ln,100))     #100是指点一个基数(初始值)然后相乘

    小结:

    处理序列中的每个元素,得到的结果是一个“列表”,该“列表”元素个数及位置与原来一样

    map()      

    便利序列中的每个元素,判断每个元素得到的布尔值,如果是True则留下来

    filter()     过滤

    处理一个序列,然后把序列进行合并操作

    reduce()

    内置函数:

    http://www.runoob.com/python/python-built-in-functions.html

    abs()取绝对值

    all() 布尔值判断,如果一个为false。则false

    any()如果有一个为ture则为ture

    bin()十进制转为二进制   0b二进制

    hex()十进制转成十六进制

    oct()十进制转成八进制

    bool()计算布尔值,假的:none ,“ ”,0 为falase

    bytes()把字符串转换成字节encoding = “utf-8”编码     decode解码,用什么编码就用什么解码

    dict()字典

    dir()打印某一个对象下面都有那些方法

    divmod()取商得余数,做分页使用

    enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。

    eval() 1、把字符串中的数据结构提取出来  2、把字符串中的表达式进行运算

    hash()  可hash的数据类型即不可变数据类型,不可hash的数据类型即可变数据类型

    globals()打印当前的全局变量

    zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。

    如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作符,可以将元组解压为列表。

    max()方法返回给定参数的最大值,参数可以为序列。

    1 l = ["a","b","c","11"]
    2 print(list(max(l)))
    3 
    4 age_dic = {"alex_age":18,"tom_age":20,"zhangsan_age":22}
    5 for i in zip(age_dic.values(),age_dic.keys()):
    6     print(i)    #查看zip函数列出的元祖
    7 print(list(max(zip(age_dic.values(),age_dic.keys()))))    

    注意:

    1、max函数处理的是可迭代对象,相当于一个for循环取出每个元素进行比较,注意,不同类型之间不能进行比较

    2、每个元素间进行比较,是从每个元素的第一位依次比较,如果这一个位置分出大小,后面的都不需要比较了,直接得出这俩元素的大小

     1 people = [
     2     {"name":"alex","age":20},
     3     {"name":"tom","age":28},
     4     {"name":"zhangsan","age":30},
     5     {"name":"lisi","age":40}
     6 ]
     7 print(max(people,key=lambda dic:dic["age"]))
     8 
     9 #max(people,key=lambda dic:dic["age"]) 这一行分解理解为以下for循环
    10 ret =[]
    11 for i in people:
    12     ret.append(i["age"])
    13 print(ret)
    14 max(ret)

     min()方法返回给定参数的最小值,参数可以为序列。与max相反

    pow()方法返回 xy(x的y次方) 的值,如果是三个参数xyz,方法是xy%z(x的y次方除以z取余)

    reversed()反转

    chr()查询数字是ASCII表中代表什么字符

    ord()查询字符是ASCII表中代表的什么数字

    round()四舍五入

    set()将列表变成集合形式

    slice()切片和l【3:5】和步长

    sorted()排序

    1 people = [
    2     {"name":"alex","age":20},
    3     {"name":"tom","age":28},
    4     {"name":"zhangsan","age":30},
    5     {"name":"lisi","age":40}
    6 ]
    7 print(sorted(people,key=lambda dic:dic["age"]))

     vars() 相当于locals()打印函数

  • 相关阅读:
    IE下全局对象报 脚本错误提示“对象不支持此属性或方法”解决方案
    IE6、7下inline-block不起作用
    IE下图片切换的时候,图片总是切换不成功---根本问题是IE缓存图片
    Fiddler 跟踪 手机页面数据包
    Regular Expression Matching——没理解的动态规划
    常见排序算法分析
    Flyweight模式(亨元模式)
    组合模式(Composite Pattern)
    装饰者模式(不太理解的设计模式)
    适配器模式
  • 原文地址:https://www.cnblogs.com/Tang-Yuan/p/9468286.html
Copyright © 2020-2023  润新知