美文网首页
三元表达式,列表推导式,生成器表达式

三元表达式,列表推导式,生成器表达式

作者: whenitsallover | 来源:发表于2018-02-10 11:08 被阅读0次

三元表达式

为真时的结果 if 判定条件 else 为假时的结果 

name=input('姓名>>: ')
res='SB' if name == 'alex' else 'NB'
print(res)

列表推导式

#1、示例
egg_list=[]
for i in range(10):
    egg_list.append('鸡蛋%s' %i)

egg_list=['鸡蛋%s' %i for i in range(10)]

#2、语法

[expression for item1 in iterable1 if condition1
for item2 in iterable2 if condition2
...
for itemN in iterableN if conditionN
]
类似于
res=[]
for item1 in iterable1:
    if condition1:
        for item2 in iterable2:
            if condition2
                ...
                for itemN in iterableN:
                    if conditionN:
                        res.append(expression)

#3、优点:方便,改变了编程习惯,可称之为声明式编程

生成器表达式

1、把列表推导式的[]换成()就是生成器表达式
2、示例:把之前生一筐鸡蛋变成你一只老母鸡,用的时候就下蛋,这就是生成器特性。
3、优点:省内存,一次只产生一个值在内存中、

匿名函数

匿名就是没有名字
def func(x,y,z=1):
    return x+y+z

匿名
lambda x,y,z=1:x+y+z #与函数有相同的作用域,但是匿名意味着引用计数为0,使用一次就释放,除非让其有名字
func=lambda x,y,z=1:x+y+z 
func(1,2,3)
#让其有名字就没有意义

有名字的函数和匿名函数的对比

有名函数:循环使用,保存了名字,通过名字就可以重复引用函数功能
匿名函数:一次性使用,随时随地定义

##内置函数
注意:内置函数id()可以返回一个对象的身份,返回值为整数。这个整数通常对应与该对象在内存中的位置。
######is 运算符用于比较两个对象的身份,等号比较两个对象的值,内置函数type()则返回一个对象的类型。
参考文献:
http://www.cnblogs.com/linhaifeng/articles/7580830.html

##给匿名函数加buff

#相关官网链接:
https://docs.python.org/3/library/functions.html#filter

######map函数
map函数会对列表中的所有元素进行操作,map有两个参数(函数,列表),他会在内部遍历列表中的每一个元素,执行传递过来的函数参数。再输入到新列表中。

1 li = [11, 22, 33]
2 new_list = map(lambda a: a + 100, li)
输出:[111, 122, 133]

######reduce函数
reduce函数会对于序列内元素进行累计操作
Apply function of two arguments cumulatively to the items of sequence, from left to right, so as to reduce the sequence to a single value.

For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). The left argument, x, is the accumulated value and the right argument, y, is the update value from the sequence. If the optional initializer is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty. If initializer is not given and sequence contains only one item, the first item is returned.

reduce用法链接:https://docs.python.org/3/library/functools.html#functools.reduce

######filter函数
filter函数,可以根据条件对数据进行过滤

li = [1,2,3,4,5,6]
new_li = filter(lambda i:i>2,li)
print(list(new_li))


相关文章

  • 三元表达式,列表推导式,生成器表达式

    三元表达式 列表推导式 生成器表达式 匿名函数 有名字的函数和匿名函数的对比 1 li = [11, 22, 33...

  • 三元表达式 / 生成器

    一,三元表达式 示例 二,列表推导式: 三, 生成器表达式 示例 四,递归 五,二分法 六,匿名函数 1)匿名函数...

  • Python 各种推导式

    python 列表推导式 执行结果 生成器表达式 1 把列表解析的[]换成()得到的就是生成器表达式 2 列表解析...

  • 迭代器&生成器

    列表推导式: 列表生成式: 相当于: 以上就是用for语句的迭代器。 生成器表达式: 生成器函数: 调用: 生成器...

  • python 基础 05 生成器,列表推导式,次要的内置函数

    python 基础 05 1.生成器 2.列表推导式 3.生成器表达式 4.字典推导式与集合推导式 5.次要的内置函数

  • 5.Python-推导式和函数

    推导式 推导式也叫三元表达式可以用于生成列表,集合和字典 也可以生成列表元素和: sum([x**2 for x ...

  • 推导式,生成器,迭代器

    列表推导式 1.举例 字典推导式 1.举例 生成器 创建生成器的方式1.生成器表达式2.yield 关键字 生成器...

  • python笔记(

    常用 提高python编码效率 赋值 列表推导式 列表解析表达式,类似[i*2 for i in list] 三元...

  • python-12 小技巧

    条件表达式 列表推导式 生成器表达式 any 和 all 集合 计数器 defaultdict 命名元组 汇集关键字实参

  • Python : 生成器表达式Generator expres

    生成器表达式 Generator expressions 是列表推导式和生成器的高性能,内存使用效率高的推广. 对...

网友评论

      本文标题:三元表达式,列表推导式,生成器表达式

      本文链接:https://www.haomeiwen.com/subject/qhlctftx.html