python-List Builder

Source: Internet
Author: User
Tags square root

List generation: Use Python's built-in, very simple, but powerful list comprehensions to create a list of generated

Some examples:

#生成list原始方法
>>> l=list (range) >>> print (L) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 , 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77 , Bayi, 94,------------------------- 1,100): L.append (x*x) >>> print (L) [1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 36 1, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401, 2500, 2601, 2704, 2809, 2916, 3025, 3136, 3249, 3364, 3481, 3600, 3721, 3 844, 3969, 4096, 4225, 4356, 4489, 4624, 4761, 4900, 5041, 5184, 5329, 5476, 5625, 5776, 5929, 6084, 6241, 6400, 6561, 672 4, 6889, 7056, 7225, 7396, 7569, 7744, 7921, 8100, 8281, 8464, 8649, 8836, 9025, 9216, 9409, 9604, 9801]
#生成式生成comprehensions, but with syntax restrictions, add [], or report a syntax error
>>> x * x for x in range (1,11): Syntaxerror:invalid syntax
#x *x is a build condition, which is to take the square root >>> [x * x for x in range (1,11)][1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

#生成条件求平方根, but the list can be added if the even square root is read out >>> [x*x for x in range (1,11) if x%2 ==0][4, 16, 36, 64, 100]

#2个for不同参数取值, each match, this test is not commonly used >>> [m+n for M in ' ABC ' for n ' dzg ' [' Ad ', ' Az ', ' Ag ', ' Bd ', ' Bz ', ' Bg ', ' Cd ', ' Cz ', ' Cg ']

#求出系统中目录带. All catalog Collections >>> Import os>>> [D for D in Os.listdir ('. ')] [' DLLs ', ' Doc ', ' include ', ' Lib ', ' Libs ', ' LICENSE.txt ', ' NEWS.txt ', ' python.exe ', ' python3.dll ', ' python36.dll ', ' Pythonw.exe ', ' Scripts ', ' tcl ', ' Tools ', ' Vcruntime140.dll '

#当然也可以生成dict, this is the original dict>>> d={' x ': ' A ', ' y ': ' B ', ' z ': ' C '}>>> for K,v in D.items ():p rint (k, ' = ', v) x = Ay = Bz = C

#利用生成器来打印循环出key,value>>> [k + ' = ' +v for k,v in D.items () [' X=a ', ' y=b ', ' z=c ']

#在列表生成器中加入对字符串的处理条件 >>> l=[' hello ', ' world ', ' IBM ', ' Apple ']>>> [S.lower () for s in l][' hello ', ' world ' , ' IBM ', ' Apple ']>>> [S[0:1].upper () for S-l][' H ', ' W ', ' I ', ' A ']>>> [S[0:].upper () for S in L] #从0位开 Start with all caps [' Hello ', ' World ', ' IBM ', ' APPLE ']>>> [S.capitalize () for S ' in L] #第一个字母大写其它小写 [' Hello ', ' World ', ' IBM ', ' Ap Ple ']

 

This is needed when, if not remember can Baidu query. There is no good way. Use more, also remember familiar. But you know it has this function

Here's a final question:

#L =[' HEllo ', ' World ', ' apple ', None]
#期待输出: [' Hello ', ' world ', ' Apple ']

>>> l=[' HEllo ', ' World ', ' apple ',none]>>> l1=[]>>> [i-I in L if Isinstance (i,str)] [' HEllo ', ' World ', ' apple ']>>>-i in L:if isinstance (I,STR): L1.append (i) >>> [X.lower () for x in l1][' he Llo ', ' World ', ' Apple '

  

python-List Builder

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.