python高級(jí)特性-列表生成式

生成[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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容