美文网首页
python高级特性-列表生成式

python高级特性-列表生成式

作者: CaesarsTesla | 来源:发表于2016-04-23 09:38 被阅读24次

    生成[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

    >>> list(range(12))

    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

    >>> list(range(2,12))

    [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

    >>> list(range(1,12))

    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

    >>> list(range(0,12))

    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

    生成[1x1,2x2,3x3,...........,10x10]

    >>> [x*x for x in list(range(1,11))]

    [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

    >>> [m+n for m in 'abc' for n in 'opq']

    ['ao', 'ap', 'aq', 'bo', 'bp', 'bq', 'co', 'cp', 'cq']

    >>> [x*x for x in list(range(0,12)) if x % 2 == 0]

    [0, 4, 16, 36, 64, 100]

    >>> [d for d in os.listdir('.')]

    ['.adobe', '.android', '.babel.json', '.bash_history', '.bash_profile', '.bash_profile.pysave', '.bashrc', '.cache', '.CF89AA64', '.CFUserTextEncoding', '.cocoapods', '.config', '.DS_Store', '.fontconfig', '.gem', '.gemrc', '.gitconfig', '.idlerc', '.itmstransporter', '.lldb', '.local', '.mkshrc', '.node-gyp', '.npm', '.npmrc', '.nvm', '.profile', '.putty', '.python_history', '.rnd', '.rvm', '.ssh', '.subversion', '.swt', '.Trash', '.viminfo', '.wireshark', '.wireshark-etc', '.Xauthority', '.zlogin', '.zshrc', 'Applications', 'Desktop', 'Documents', 'Downloads', 'haoshu', 'HBuilder', 'HBuilderProjects', 'IOS_project', 'Library', 'Movies', 'Music', 'mynote', 'Pictures', 'Public', 'svn', 'SwiftWeather']

    >>> x = 'abc'

    >>> y = 123

    >>> isinstance(x,str)

    True

    >>> isinstance(y,str)

    False

    相关文章

      网友评论

          本文标题:python高级特性-列表生成式

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