zoukankan      html  css  js  c++  java
  • python_推导式

    • 列表推导式

      目的:方便的生成一个列表

      格式:

        v1 = [i for i in 可迭代对象 ]

        v2 = [i for i in 可迭代对象 if条件]#条件为True菜进行append

    v1 = [i for i in 'alex']
    # 变量 = [for循环的变量 for循环一个可迭代的对象]
    v2 = [i+100 for i in range(10)]
    v3 = [100 for i in range(10)]
    v4 = [99 if i > 5 else 66 for i in range(10)]
    def func():
    print(123)
    v5 = [func for i in range(10)]
    v6 = [lambda :i for i in range(10)]
    v7 = [lambda x:x*i for i in range(10)]
    print(v1)
    print(v2)
    print(v3)
    print(v4)
    print(v5)
    print(v6[9]())
    print(v7[0](2))

    # 筛选功能
    v8 = [i for i in range(10) if i >5]
    print(v8)
    • 集合推导式

      格式:v1 = {i for i in 可迭代对象} 

    v8 = {i for i in range(10) if i >5}
    print(v8)
    • 字典推导式

      格式:v1 = {'k'+str(i):i for i in range(10)} 

    v1 = {'k'+str(i):i for i in range(10)}
    print(v1)
  • 相关阅读:
    bzoj1415 NOI2005聪聪和可可
    Tyvj1952 Easy
    poj2096 Collecting Bugs
    COGS 1489玩纸牌
    COGS1487 麻球繁衍
    cf 261B.Maxim and Restaurant
    cf 223B.Two Strings
    cf 609E.Minimum spanning tree for each edge
    cf 187B.AlgoRace
    cf 760B.Frodo and pillows
  • 原文地址:https://www.cnblogs.com/wangdianchao/p/11519675.html
Copyright © 2011-2022 走看看