Generator expressions and various derivations

1. The generator expression

# Generator expression (accounting for almost no memory) 
G = (I for I in Range (10 ))
 Print (G)
 # <Object Generator <genexpr> AT 0x0000019DFFFAC408> 
for I in G:
     Print (I)

2. List comprehensions

# List comprehension 
ex_list = [ ' % s of ' % (I +. 1) for I in Range (. 5 )]
 Print (ex_list)
 # [ 'on a', 'second', 'third' , 'fourth', 'fifth'] 
# corresponds 
ex_list = []
 for I in Range (. 5 ): 
    ex_list.append ( " % s of " % (. 1 + I ))
 Print (ex_list)

3. The difference between the derived list generator expressions and formulas

# List Generator comprehension and expression distinction 
RET1 = (I * 2 for I in Range (. 5 ))
 Print (RET1)    # At this time to get a generator, small footprint 
# <Object Generator <genexpr> AT 0x0000019DFFFAC408 > 
RET2 = [I 2 * for I in Range (. 5 )]
 Print (RET2)
 # [0, 2,. 4,. 6,. 8]

4. derivations Dictionary

# The value of the key value and a dictionary swap 
DIC1 = { " . 1 " :. 11, " 2 " : 12 is } 
dic1_reverse = {DIC1 [K]: K for K in DIC1}
 Print (dic1_reverse)
 # . 11: '. 1' , 12: '2'} 

# combined sensitive value corresponding to the k value ,, uniform lowercase 
DIC2 = { " a " : 10, " B " : 21 is, " a " : 20 is, " K " : 44 is } 
dic2_f = {k.lower (): dic2.get ( k.lower (), 0) + dic2.get (k.upper (),0) for k in} DIC2
 Print (dic2_f)
 # dic2.get ( 'a', 0) Gets the value of a, if not returns 0 (0 is not written return None) 
# { 'a': 30, 'B': 21 is, ' k ': 44}

The derivation of the formula set (comes to weight)

# Set of derivation 
# calculates the square of each value in the list of (to own weight) 
squaredx ** 2 = {X for X in [. 1,-1,2, -2,3,4,7 ]}
 Print (squaredx )
 # {1, 4, 9, 16, 49}

6. No inferential ancestral

# No ancestral derivation, since, after all, with parentheses, but preceded tuple, casts 
RETX = ((I * 2 for I in Range (. 5) IF I% 2 == 0))
 Print (RETX)
 # <Object Generator <genexpr> AT 0x000001ADF3FCC5E8> 
# is (0, 4, 8) after the cast

7. Other examples

# Nested list has the value 'e' (the complex is not recommended) 
names = [[ ' Google ' , ' Facebook ' , ' YouTube ' , ' Amazon ' ], [ ' baidu ' , ' alibaba ' , ' Tencent ' , ' bilibili ' ]] 
NEME = [name for LST in names for name in LST IF name.count ( ' E ' )>=1]
print(neme)
#['google', 'facebook', 'youtube', 'tencent']
# To python3 can Chinese as a variable name 
example = (I for I in Range (10 ))
 Print (G)
 # <Generator Object <genexpr> AT 0x0000019DFFFAC408> 
for elements in Example:
     Print (element, End = " * " )
 # 0 * 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 *

 

to sum up:

Syntax:
[ each element or elements and for operating expressions related elements in may iterate type] # in turn processed after traversing the
[ element satisfies the condition related to operation for the element in may iterate if the data type of operating elements associated] # filtering capabilities
# Some notes: 
generating and iterators are inert operation (not find it does not take the value of the work), the generator, there is no time value is taken, but a plurality of generators
iterables has __iter__ method

Guess you like

Origin www.cnblogs.com/lowislucifer/p/10994296.html