10. array
한종류의타입만담는객체. 공간 효율적인자료구조
array(typecode, [, initializer])
타입코드목록
코드 설명 C 타입 크기
'b' 8비트정수 signedchar 1
'B' 8비트부호없는정수 unsignedchar 1
'u' 유니코드문자 PY_UNICODE 2 / 4
'h' 16비트정수 short 2
'H' 16비트부호없는정수 unsignedshort 2
정수형은머신아키텍처에의해결정
10
11. array
코드 설명 C 타입 크기
'i' 정수 int 4 / 8
'I' 부호없는정수 unsignedint 4 / 8
'l' 긴 정수 long 4 / 8
'L' 부호없는긴 정수 unsignedlong 4 / 8
'q' 더욱긴 정수 longlong 8 (Python3.3)
'Q' 부호없는더욱긴 정수 unsignedlonglong 8 (Python3.3)
'f' 단일정밀도실수 float 4
'd' 배정밀도실수 double 8
11
12. array
array 객체와생성기 표현식을이용한데이터생성
>>> import array
>>> a = array.array("i", [1, 2, 3, 4, 5])
>>> b = array.array(a.typecode, (x * x for x in a))
>>> a
array('i', [1, 2, 3, 4, 5])
>>> b
array('i', [1, 4, 9, 16, 25])
12
22. defaultdict
>>> from collections import defaultdict
>>> s = "yeah but no but yeah but no but yeah"
>>> words = s.split()
>>> word_locations = defaultdict(list)
>>> for n, w in enumerate(words):
... word_locations[w].append(n)
...
>>> word_locations
defaultdict(<class 'list'>, {'yeah': [0, 4, 8],
'but': [1, 3, 5, 7], 'no': [2, 6]})
22
40. imap (Python3: map)
imap(function, iter1, iter2, ..., iterN)
function(i1, i2, ..., iN) 들을수행하는반복자생성
>>> a = [1, 2, 4, 10]
>>> b = [5, 10, 15, 20]
>>> m = map(lambda x, y: x + y, a, b)
>>> list(m)
[6, 12, 19, 30]
>>> m = map(lambda x, y: x + y, a, b)
>>> m.__next__()
6
40