An Array of Sequences(1)

1.  Listcomps (List comprehensions) do everything the map and filter functions do. (map 함수 와 filter 함수 가 할 수 있 는 것 은 목록 생 성 식 이 모두 가능 합 니 다)
목록 생 성식 과 map, filter 함수 운행 시간의 비교 예시:
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 :', '[ord(s) for s in symbols if non_ascii(ord(s))]')
clock('filter + lambda :', 'list(filter(lambda c: c > 127, map(ord, symbols)))')
clock('filter + func   :', 'list(filter(non_ascii, map(ord, symbols)))')


#            ,    timeit   
"""
timeit           :
1. timeit(stmt='pass', setup='pass', timer=, number=1000000)
      :
                stmt    number      ,    ,float 

         :
            stmt:        

            setup:         ,     ,   import   

            timer:   win32  time.clock(),linux  time.time(),   ,   

            number:   stmt   
            
2. repeat(stmt='pass', setup='pass', timer=, repeat=3, number=1000000)
         timeit      repeat    ,      timeit       ,      ,         ;repeat   3
"""

timeit 참조 링크: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
"""

문자열 포맷 참조 링크: https://www.cnblogs.com/songdanlee/p/11105807.html 
 
3. Named Tuples
from collections import namedtuple

# Build a namedtuple example 1:
# Card = namedtuple('Card', ['rank', 'suit'])

# 【Two parameters】 are required to create a named tuple: 【a class name】 and a 【list of field names】, which can be given by
# an 【iterable of strings】 or as 【a single space-delimited string】

In [57]: from collections import namedtuple

In [58]: City = namedtuple('City', 'name country population coordinates')

# Data must be passed as positional arguments to the constructor.
In [59]: tokyo = City('Tokyo', 'JP', 36.933, (35.689722, 139.691667))

In [60]: tokyo
Out[60]: City(name='Tokyo', country='JP', population=36.933, coordinates=(35.689722, 139.691667))

In [61]: tokyo.population
Out[61]: 36.933

In [62]: tokyo.coordinates
Out[62]: (35.689722, 139.691667)

# You can access the fields by name or position
In [63]: tokyo[1]
Out[63]: 'JP'

In [64]:

In [64]:

# named tuple attributes: _fields -- class attribute; _make(iterable) -- class method; _asdict() -- instance method
In [64]: City._fields
Out[64]: ('name', 'country', 'population', 'coordinates')

In [65]: LatLong = namedtuple('LatLong', 'lat long')

In [66]: delhi_data = ('Delhi NCR', 'IN', 21.935, LatLong(28.613889, 77.208889))

In [67]: delhi = City._make(delhi_data)

In [68]: delhi
Out[68]: City(name='Delhi NCR', country='IN', population=21.935, coordinates=LatLong(lat=28.613889, long=77.208889))

In [69]: delhi._asdict()
Out[69]:
OrderedDict([('name', 'Delhi NCR'),
             ('country', 'IN'),
             ('population', 21.935),
             ('coordinates', LatLong(lat=28.613889, long=77.208889))])

In [70]: for key, val in delhi._asdict().items():
    ...:     print(key + ':', val)
    ...:
name: Delhi NCR
country: IN
population: 21.935
coordinates: LatLong(lat=28.613889, long=77.208889)

"""
_fields is a tuple with the field names of the class.
_make() allow you to instantiate a named tuple from an iterable; City(*delhi_data) would do the same.
_asdict() returns a collections.OrderDict built from the named tuple instance.
"""

 
4. 슬라이스 (슬라이스)
4.1 Why Slices and Range Exclude the Last Item ?
"""
The Pythonic convention of excluding the last item in slices and ranges works well with the zero-based indexing used in Python,C and many other languages.
Some convenient features of the convention are :
1. It's easy to see the length of a slice or range when only the stop position is given: range(3) and my_list[:3] both produce three items.
2. It's easy to compute the length of a slice or range when start and stop are given: just subtract stop - start.
3. It's easy to split a sequence in two parts at any index x, without overlapping: simply get my_list[:x] and my_list[x:].
"""

4.2 Slice Object
예시: 플랫 파일 인보이스 의 라인 아 이 템
invoice = """
0.....6.................................40...........52...55........
1909  Pimoroni PiBrella                      $17.50      3    $52.50
1489  6mm Tactile Switch x20                  $4.95      2     $9.90
1510  Panavise Jr. -PV -201                  $28.00      1    $28.00
1601  PiTFT Mini Kit 320x240                 $34.95      1    $34.95
"""

# assign name to slices
SKU = slice(0, 6)
DESCRIPTION = slice(6, 40)
UNIT_PRICE = slice(40, 52)
QUANTITY = slice(52, 55)
ITEM_TOTAL = slice(55, None)

line_items = invoice.split('
') print(line_items) """ Output: ['', '0.....6.................................40...........52...55........', '1909 Pimoroni PiBrella $17.50 3 $52.50', '1489 6mm Tactile Switch x20 $4.95 2 $9.90', '1510 Panavise Jr. -PV -201 $28.00 1 $28.00', '1601 PiTFT Mini Kit 320x240 $34.95 1 $34.95', ''] """ line_items = invoice.split('
')[2:] for item in line_items: print(item[UNIT_PRICE], item[DESCRIPTION]) # sequence[slice] """ Output: $17.50 Pimoroni PiBrella $4.95 6mm Tactile Switch x20 $28.00 Panavise Jr. -PV -201 $34.95 PiTFT Mini Kit 320x240 """ """ : The notation a:b:c is only valid within [] when used in the indexing or subscript operator, and it produces a slice object:slice(a, b, c). To evaluate the expression seq[start:stop:step], Python calls seq.__getitem__(slice(start, stop, step)) """

4.3 Building Lists of Lists (Using + and * with Sequences)
"""
To initialize a list of lists as my_list = [[]] * 3 will result in a list with three references to the same inner list.
e.g.
In [47]: my_list = [[]] * 3

In [48]: my_list
Out[48]: [[], [], []]
# Out[48]      []          

In [49]: my_list[0] is my_list[1] and my_list[1] is my_list[2]
Out[49]: True
"""

"""
The best way of initializing a list with a certain number of nested lists is with a list comprehension.
                      
"""

# Right example:
# Example 2-12
board = [[] for i in range(3)]
print(board)
#
# [[], [], []]

print(board[0] is board[1] and board[1] is board[2])
#
# False
# False                       (        )

board[1].append('a')
print(board)
#
# [[], ['a'], []]


# Wrong example:
# Example 2-13
weird_board = [[]] * 3
print(weird_board)
#
# [[], [], []]

print(weird_board[0] is weird_board[1] and weird_board[1] is weird_board[2])
#
# True
#     True                   

weird_board[1].append('a')
print(weird_board)
#
# [['a'], ['a'], ['a']]


#         Example 2-13         :
row = []
board_wrong = []
for i in range(3):
    board_wrong.append(row)     # The same row is appended three times to board_wrong.
print(board_wrong)
#
# [[], [], []]


#         Example 2-12         :
board_right = []
for i in range(3):
    row = []    #    for       row     
    board_right.append(row)     # Each iteration builds a new row and appends it to borad_right.
board_right[1].append('b')
print(board_right)
#
# [[], ['b'], []]

 
end

좋은 웹페이지 즐겨찾기