An Array of Sequences

Listcomps 1. (List comprehensions) do Everything The Map and filter Functions do. (Map function and filter function can do, the list can be done formula)

And formula list map, comparative example filter function of time:

import timeit

TIMES = 10000

SETUP = """
symbols = '$¢£¥€¤'
def non_ascii(c):
    return c > 127
"""
timeit.timeit()

def clock(label, cmd):
    res = timeit.repeat(cmd, setup=SETUP, number=TIMES)
    print(label, *('{:.3f}'.format(x) for x in res))


clock('listcomp        :', '[ord(s) for s in symbols if ord(s) > 127]')
clock('listcomp + func :' , ' [The ord (S) for S in Symbols IF non_ascii (the ord (S))] ' ) 
Clock ( ' filter + the lambda: ' , ' List (filter (the lambda C: C> 127, Map (the ord, Symbols) )) ' ) 
Clock ( ' filter + FUNC: ' , ' List (filter (non_ascii, Map (the ord, Symbols))) ' ) 


# run time of the test piece of code can be used timeit module 
"" " 
timeit module main function there are two: 
1. the timeit (stmt = 'Pass', = Setup 'Pass', Timer = <defaulttimer>,number = 1000000) 
    Returns: 
            Returns stmt execution time code number used times, in seconds, float type 

       parameter: 
            stmt: that code to be executed

            setup: the implementation of the code of preparation, do not count the time, normally import like 

            timer: This is time.clock under win32 (), under linux is time.time (), the default, do not control 

            number: To perform how many times stmt 
            
2. repeat (stmt = 'pass' , setup = 'pass', timer = <defaulttimer>, repeat = 3, number = 1000000) 
    this function is more than timeit a function only of repeat, this indicates repeatedly performed timeit process many times, it returns a list of the execution time of each pass; repeat default is 3 
"" "

timeit reference links: https://www.cnblogs.com/itcomputer/articles/4578769.html

 

2. Tuple

In [44]: traveler_ids = [('USA','31195855'), ('BRA','CE342567'), ('ESP', 'XDA205856')]

In [45]: for passport in sorted(traveler_ids):
    ...:     print('%s/%s' % passport)
    ...:
BRA/CE342567
ESP/XDA205856
USA/31195855

# The % formatting operator understands tuples and treats each item as a separate field.


# Tuple Unpacking
In [46]: t = (20, 8)

In [47]: divmod(*t)
Out[47]: (2, 4)

In [48]: quotient,remainder = divmod(*t)

In [49]: quotient, remainder
Out[49]: (2, 4)

# Using * to grab excess items
In [50]: a, b, *rest = range(5)

In [51]: a, b, rest
Out[51]: (0, 1, [2, 3, 4])

In [52]: a, b, *rest = range(2)

In [53]: a, b, rest
Out[53]: (0, 1, [])

In [54]: a, *body, c, d = range(5)

In [55]: a, body, c, d
Out[55]: (0, [1, 2], 3, 4)

# In the context of parallel assignment, the * prefix can be applied to exactly one variable, but it can appear in any position


# Nested Tuple Unpacking
metro_areas = [
    ('Tokyo', 'JP', 36.933, (35.689722, 139.691667)),
    ('Mexico City', 'MX', 20.142, (19.433333, -99.133333)),
    ('New York-Newark', 'US', 20.104, (40.808611, -74.020386)),
]

print('{:15} | {:^9} | {:^9}'.format('', 'lat.', 'long.'))
fmt = '{:15} | {:9.4f} | {:9.4f}'

# By assigning the last field to a tuple, we unpack the coordinates.
for name, cc, pop, (latitude, longitude) in metro_areas:
    if longitude <= 0:
        print(fmt.format(name, latitude, longitude))


# Output:
"""
                |   lat.    |   long.  
Mexico City     |   19.4333 |  -99.1333
New York-Newark |   40.8086 |  -74.0204
"""

Reference link string formatting:  https://www.cnblogs.com/songdanlee/p/11105807.html 

 

 

 

 

 

 

 

 

 

 

 

end

Guess you like

Origin www.cnblogs.com/neozheng/p/12128625.html