推导式

  1. 生成指定序列列表(程序生成)

    lst = []
    for i in range(1,6):
     lst.append('python_%d' % i)
    print(lst)

    结果:
    F:\myPy\venv\Scripts\python.exe F:/myPy/test.py
    ['python_1', 'python_2', 'python_3', 'python_4', 'python_5']

  2. [列表|其他可迭代的对象]推导式(最终结果是[列表|其他可迭代的对象])
    #语法:
    # [变量a for 变量a in 可迭代对象]
    # return 可迭代对象
    lst = ['python_%d' % i for i in range(1,6)]
    print(lst)

    结果:
    F:\myPy\venv\Scripts\python.exe F:/myPy/test.py
    ['python_1', 'python_2', 'python_3', 'python_4', 'python_5']

上一篇:【python】python27安装jupyter编码UnicodeDecodeError_byte_0xe5_in_position_4ordinal


下一篇:创建flask