List parsing
The way you create new lists efficiently, based on an existing list.
List parsing is an application of the Python iteration mechanism, which is commonly used to create new lists and is therefore used in [].
Grammar:
[Expression for iter_val in iterable]
[Expression for Iter_val in iterable if COND_EXPR]
Examples show:
1Requirements: List 1~10 Square of all numbers2 ####################################################31, common method:4>>> L = []5>>> forIinchRange (1,11):6... L.append (i**2)7 ... 8>>>PrintL9[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]Ten #################################################### One2, List parsing A>>>l = [i**2 forIinchRange (1,11)] ->>>PrintL -[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
1Requirements: List 1~The square of a number greater than or equal to 4 in 102 ####################################################31, common method:4>>> L = []5>>> forIinchRange (1,11):6...ifI >= 4:7... L.append (i**2)8 ... 9>>>PrintLTen[16, 25, 36, 49, 64, 81, 100] One #################################################### A2, List parsing ->>>l = [i**2 forIinchRange (1,11)ifI >= 4 ] ->>>PrintL the[16, 25, 36, 49, 64, 81, 100]
1Requirements: List 1~10 The square of all numbers divided by the value of 22 ####################################################31, Common methods4>>> L = []5>>> forIinchRange (1,11):6... L.append (I**2/2)7 ... 8>>>PrintL9[0, 2, 4, 8, 12, 18, 24, 32, 40, 50]Ten #################################################### One2, List parsing A>>> L = [I**2/2 forIinchRange (1,11) ] ->>>PrintL -[0, 2, 4, 8, 12, 18, 24, 32, 40, 50]
1Requirements: List"/var/log"All the'. Log'End of File2 ##################################################31, Common methods4>>>ImportOS5>>>file = []6>>> forFileinchOs.listdir ('/var/log'):7...ifFile.endswith ('. Log'):8 ... file.append (file)9 ... Ten>>>Printfile One['Anaconda.ifcfg.log','Xorg.0.log','Anaconda.storage.log','Xorg.9.log','Yum.log','Anaconda.log','Dracut.log','Pm-powersave.log','Anaconda.yum.log','Wpa_supplicant.log','Boot.log','Spice-vdagent.log','Anaconda.program.log'] A ################################################## -2. List parsing ->>>ImportOS the>>> file = [file forFileinchOs.listdir ('/var/log')ifFile.endswith ('. Log') ] ->>>Printfile -['Anaconda.ifcfg.log','Xorg.0.log','Anaconda.storage.log','Xorg.9.log','Yum.log','Anaconda.log','Dracut.log','Pm-powersave.log','Anaconda.yum.log','Wpa_supplicant.log','Boot.log','Spice-vdagent.log','Anaconda.program.log']
1 Requirement: Implement the elements in both lists note pairing. 21, common method:3>>> L1 = ['x','y','Z']4>>> L2 = [a] 5>>> L3 = []6>>> forAinchL1:7... forBinchL2:8 ... L3.append ((b))9 ... Ten>>>PrintL3 One[('x', 1), ('x', 2), ('x', 3), ('y', 1), ('y', 2), ('y', 3), ('Z', 1), ('Z', 2), ('Z', 3)] A #################################################### -2, List parsing: ->>> L1 = ['x','y','Z'] the>>> L2 = [a] -L3 = [(A, B) forAinchL1 forBinchL2] ->>>PrintL3 -[('x', 1), ('x', 2), ('x', 3), ('y', 1), ('y', 2), ('y', 3), ('Z', 1), ('Z', 2), ('Z', 3)]
Description
In the above example, using list parsing can be almost 1 time times faster than using the normal method. It is therefore recommended to use list parsing.
Python list parsing