zoukankan      html  css  js  c++  java
  • 列表,字典表达式以及三元表达式

    1.三元表达式
    条件成立时的返回值 if 条件 else 条件不成立时的返回值
    三元表达式的意义就是让一些简单的if判断写成一行,减少代码量

    def max2(x,y):
    if x > y:
    return x
    else:
    return y
    x=10
    y=20
    res = x if x > y else y
    print(res)

    2.列表生成式
    l=[item**2 for item in range(1,11)]
    print(l)
    print(next(iter(l))) # 两次都是输出1
    print(next(iter(l))) # 因为重新生成了一次对象
    res = l.__iter__()
    print(next(res)) 1
    print(next(res)) 4
    print(next(res)) 9

    names=['alex','wxx','lxx']

    l=[]
    for name in names:
    l.append(name + 'SB')
    names=l

    names=[name+'SB' for name in names]
    print(names)


    names=['alex','wxx','egon','lxx','zhangmingyan']
    l=[]
    for name in names:
    if name != 'egon':
    l.append(name + 'SB')
    names=l
    names=[name+'SB' for name in names if name != 'egon']
    print(names)

    列表推导式
    l=[item**2 for item in range(1,5) if item > 2]
    print(l)

    names=['egon','alex_sb','wupeiqi','yuanhao']
    names=[name.upper() for name in names]
    print(names)
    names=['egon','alex_sb','wupeiqi','yuanhao']

    nums=[len(name) for name in names if not name.endswith('sb')]
    print(nums)


    字典生成式
    zip() 拉链函数 两个可迭代对象之间一一匹配,如果有一个对象比较长,短的匹配完了就停止匹配。
    匹配后的结果以元组形式返回。
    字典通过items后得到的值和拉链函数对两个可迭代对象进行匹配得到的值都是元组形式。
    通过变量解压可以对元组解压。
    for k,v in dict.items() == for k,v in zip(s1,l)
    s1='hellopo'
    l1=[1,2,3,4,5,6,7]

    res=zip(s1,l1) # <zip object at 0x0000019B07AFA8C8> <<---res
    # [('h', 1), ('e', 2), ('l', 3), ('l', 4), ('o', 5), ('p', 6), ('o', 7)]
    print(res)
    print(list(res))


    keys=['name','age','sex']
    values=['egon',18,'male']

    res=zip(keys,values)
    print(type(res)) # <class "zip">
    print(list(res)) # [('name', 'egon'), ('age', 18), ('sex', 'male')]
    print(list(res)) # [ ]
    d={}
    for item in zip(keys,values):
    print(item) # 通过变量解压操作 k,v = item
    for k,v in zip(keys,values):
    d[k]=v
    print(d)
    {'name': 'egon', 'age': 18, 'sex': 'male'}


    keys=['name','age','sex']
    values=['egon',18,'male']
    d={k:v for k,v in zip(keys,values)}
    print(d)

    info={'name': 'egon', 'age': 18, 'sex': 'male'}

    keys=info.keys()
    print(keys)
    iter_keys=keys.__iter__()
    values=info.values()
    print(values)

    d={k:v for k,v in zip(keys,values)}
    print(d)

    s={i for i in range(10)}
    print(s,type(s)) # set


    生成器表达式
    g=(i for i in range(10))
    # print(g)

    print(next(g))
    print(next(g))


    nums=[11,22,33,44,55]
    print(max(nums))

    with open(r'C:UsersLENOVOPycharmProjectsuntitled2a.txt',encoding='utf-8') as f:
    nums=(len(line) for line in f) # 生成器
    print(next(nums))
    print(next(nums))
    print(next(nums))
    # print(type(nums))
    # print(max(nums))
    # print(type(max))# 内置方法 max(nums)和for循环,list相似,max依次取值比较大小,返回较大的,再取值进行比较
    print(nums) # nums在外部仍是存在的
    print(max(nums)) # 会抛出异常是因为文件已经关闭了,生成器无法取值
    print(max(nums))

    nums是一个生成器,即使文件关闭,nums依然以生成器的形式存在。在文件中调用max方法,max方法会自动执行next操作


    列表表达式
    l=['egg%s' %i for i in range(100)]
    print(l)

    生成器
    g=('egg%s' %i for i in range(1000000000000))
    # print(g)
    print(next(g))
    print(next(g))
  • 相关阅读:
    在子线程中使用Toast
    时间戳与字符串的转换
    Notification小案例
    Android文件的读写操作
    SmartImageView-网络图片查看器
    JavaScript中判断对象类型的种种方法
    nodejs npm常用命令
    web前端安全机制问题全解析
    Gulp安装及配合组件构建前端开发一体化(转)
    gulp 实现 js、css,img 合并和压缩(转)
  • 原文地址:https://www.cnblogs.com/Roc-Atlantis/p/9178751.html
Copyright © 2011-2022 走看看