KEMBAR78
Python Programming: Data Structure | PDF
PythonIntermediateProgramming
์ž„์ฐฌ์‹(chanshik@gmail.com)
1
PythonIntermediateProgramming
ํƒ€์ž…๊ณผ ๊ฐ์ฒด
ํ•จ์ˆ˜์™€ํ•จ์ˆ˜ํ˜•ํ”„๋กœ๊ทธ๋ž˜๋ฐ
ํด๋ž˜์Šค์™€๊ฐ์ฒด์ง€ํ–ฅํ”„๋กœ๊ทธ๋ž˜๋ฐ
๋ฐ์ดํ„ฐ๊ตฌ์กฐ
ํŠœ๋‹๊ณผ ์ตœ์ ํ™”
2
๋ฐ์ดํ„ฐ๊ตฌ์กฐ
ํ”„๋กœ๊ทธ๋žจ์„์ž‘์„ฑํ• ๋•Œ๋„์›€์ด๋˜๋Š”ํšจ์œจ์ ์ธ๋ฐ์ดํ„ฐ๊ตฌ์กฐ๋ชจ๋“ˆ์†Œ๊ฐœ
array
bisect
collections
itertools
3
abc
abc๋ชจ๋“ˆ์€์ƒˆ๋กœ์šด์ถ”์ƒ๊ธฐ๋ฐ˜ํด๋ž˜์Šค๋ฅผ์ •์˜ํ•˜๋Š”๋ฐ์‚ฌ์šฉํ•˜๋Š”
๋ฉ”ํƒ€ํด๋ž˜์Šค์™€์žฅ์‹์žํ•œ์Œ์„๊ฐ€์ง€๊ณ  ์žˆ์Œ
ABCMeta
์ถ”์ƒ๊ธฐ๋ฐ˜ํด๋ž˜์Šค๋ฅผ๋‚˜ํƒ€๋‚ด๋Š”๋ฉ”ํƒ€ํด๋ž˜์Šค
Python3
>>> import abc
>>> class Stackable(metaclass=abc.ABCMeta):
... pass
Python2
class Stackable:
__metaclass__ = abc.ABCMeta
4
abc
์ถ”์ƒ๊ธฐ๋ฐ˜ํด๋ž˜์Šค์˜ํŠน์ง•
abstracemethod, abstractproperty ์žฅ์‹์ž๋ฅผ์‚ฌ์šฉํ•˜์—ฌ
๋ฉ”์„œ๋“œ๋‚˜ํ”„๋กœํผํ‹ฐ๋ฅผ์ •์˜ํ•˜๋ฉด์‹ค์ œ๊ตฌํ˜„์„์ œ๊ณตํ• ๊ฒฝ์šฐ์—๋งŒ
์ธ์Šคํ„ด์Šค์ƒ์„ฑ๊ฐ€๋Šฅ
์–ด๋–คํƒ€์ž…์„๋…ผ๋ฆฌ์ ํ•˜์œ„ํด๋ž˜์Šค๋กœ๋“ฑ๋กํ•˜๋Š”๋ฐ์‚ฌ์šฉํ•˜๋Š”ํด๋ž˜์Šค๋ฉ”์„œ๋“œ
register(subclass) ๋ฅผ๊ฐ€์ง€๊ณ  ์žˆ์Œ
__subclasshook__(cls, subclass) ๋ฅผ์ถ”๊ฐ€๋กœ์ •์˜๊ฐ€๋Šฅ
ํƒ€์ž…subclass ๊ฐ€ ํ•˜์œ„ํด๋ž˜์Šค๋กœ๊ฐ„์ฃผ๋˜๋ฉดTrue
subclass ๊ฐ€ ํ•˜์œ„ํด๋ž˜์Šค๊ฐ€ ์•„๋‹ˆ๋ฉดFalse
์•„๋ฌด๋Ÿฐ์ •๋ณด๊ฐ€ ์—†์„๊ฒฝ์šฐNotImplemented์˜ˆ์™ธ๋ฅผ๋ฐœ์ƒ
5
abc
abstractmethod(method)
method๋ฅผ์ถ”์ƒ๋ฉ”์„œ๋“œ๋กœ์„ ์–ธํ•˜๋Š”์žฅ์‹์ž
์ง์ ‘์ƒ์†์„ํ†ตํ•ด์ •์˜ํ•œํŒŒ์ƒํด๋ž˜์Šค์—์„œ์‹ค์ œ๊ตฌํ˜„์„์ œ๊ณตํ•ด์•ผ
์ธ์Šคํ„ด์Šค์ƒ์„ฑ๊ฐ€๋Šฅ
register() ๋ฉ”์„œ๋“œ๋กœ๋“ฑ๋กํ•œํ•˜์œ„ํด๋ž˜์Šค์—๋Š”์˜ํ–ฅ์—†์Œ
abstractproperty(fget [, fset [, fdel[, doc]]])
์ถ”์ƒํ”„๋กœํผํ‹ฐ๋ฅผ์ƒ์„ฑ
๋งค๊ฐœ๋ณ€์ˆ˜๋“ค์€์ผ๋ฐ˜property() ํ•จ์ˆ˜์™€๋™์ผ
ํŒŒ์ƒํด๋ž˜์Šค์—์„œ๋Š”ํ•ด๋‹นํ”„๋กœํผํ‹ฐ๊ตฌํ˜„์„์ œ๊ณตํ•ด์•ผ์ธ์Šคํ„ด์Šค์ƒ์„ฑ๊ฐ€๋Šฅ
6
abc
์ถ”์ƒ๊ธฐ๋ฐ˜ํด๋ž˜์Šค์ •์˜
>>> from abc import ABCMeta, abstractmethod, abstractproperty
>>> class Stackable(metaclass=ABCMeta):
... @abstractmethod
... def push(self, item):
... pass
... @abstractmethod
... def pop(self):
... pass
... @abstractproperty
... def size(self):
... pass
7
abc
Stackable์„์ƒ์†ํ•˜์—ฌํด๋ž˜์Šค์ƒ์„ฑ
>>> class Stack(Stackable):
... def __init__(self):
... self.items = []
... def push(self, item):
... self.items.append(item)
... def pop(self):
... return self.items.pop()
...
>>> a = Stack()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class Stack with
abstract methods size
size() ํ”„๋กœํผํ‹ฐ๊ฐ€ ์—†์–ด์ธ์Šคํ„ด์Šค๋ฅผ์ƒ์„ฑํ• ์ˆ˜์—†์Œ
8
abc
size() ํ”„๋กœํผํ‹ฐ๋ฅผStack์—์„œ์ƒ์†๋ฐ›์€ํด๋ž˜์Šค์—์ถ”๊ฐ€
>>> class CompleteStack(Stack):
... @property
... def size(self):
... return len(self.items)
...
>>> s = CompleteStack()
>>> s.push("foo")
>>> s.push("bar")
>>> s.size
2
>>> s.pop()
'bar'
9
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
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
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
array
enumerate() ํ•จ์ˆ˜๋ฅผ์ด์šฉํ•œ์ œ์ž๋ฆฌ(inโ€‘place) ์—ฐ์‚ฐ์„ํ†ตํ•ด
๊ณต๊ฐ„์„์ ˆ์•ฝํ•˜๋Š”๋ฐฉ์‹์˜ํ”„๋กœ๊ทธ๋ž˜๋ฐํ™œ์šฉ
>>> for i, x in enumerate(a):
... a[i] = x * x
...
>>> a
array('i', [1, 4, 9, 16, 25])
13
bisect
bisect ๋ชจ๋“ˆ์€๋ฆฌ์ŠคํŠธ๋ฅผ์ •๋ ฌ๋œ์ˆœ์„œ๋กœ์œ ์ง€ํ•˜๋Š”๋ฐํ•„์š”ํ•œ๊ธฐ๋Šฅ์ œ๊ณต
bisect(list, item [, low [, high]])
insort(list, item [, low [, high]])
bisect(): list ๋ฅผ์ •๋ ฌ๋œ์ˆœ์„œ๋กœ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œitem์„
list ์–ด๋””์—์‚ฝ์ž…ํ•ด์•ผํ•˜๋Š”์ง€์œ„์น˜๋ฐ˜ํ™˜
insort(): item์„list ์—์ •๋ ฌ๋œ์ˆœ์„œ์—๋งž๊ฒŒ ์‚ฝ์ž…
item์ด์ด๋ฏธ์กด์žฌํ•˜๋ฉด๊ธฐ์กดํ•ญ๋ชฉ์˜ค๋ฅธ์ชฝ์—์‚ฝ์ž…
14
bisect
>>> from bisect import bisect
>>> grades = "FEDCBA"
>>> breakpoints = [30, 44, 66, 75, 85]
>>> grades[bisect(breakpoints, 50)]
'D'
>>> grades[bisect(breakpoints, 90)]
'A'
15
bisect
>>> from bisect import insort
>>> l = [10, 30, 50, 70]
>>> insort(l, 60)
>>> l
[10, 30, 50, 60, 70]
>>> insort(l, 40)
>>> l
[10, 30, 40, 50, 60, 70]
16
collections
์œ ์šฉํ•œ์ปจํ…Œ์ด๋„ˆํƒ€์ž…์—๋Œ€ํ•œ๊ณ ์„ฑ๋Šฅ๊ตฌํ˜„ํ•จ์ˆ˜์™€
๋‹ค์–‘ํ•œ์ปจํ…Œ์ด๋„ˆ๋ฅผ์œ„ํ•œ์ถ”์ƒ๊ธฐ๋ฐ˜ํด๋ž˜์Šค๋“ฑ์„์ œ๊ณต
deque
defaultdict
namedtuple
17
deque
์–‘๋์„๊ฐ€์ง„๊ฐ์ฒด๋ฅผ๋‚˜ํƒ€๋‚ด๋Š”์ปจํ…Œ์ด๋„ˆ
>>> from collections import deque
>>> d = deque('hello')
>>> d.pop()
'o'
>>> d.pop()
'l'
>>> d
deque(['h', 'e', 'l'])
>>> d.append('a')
>>> d.append('b')
>>> d.appendleft('c')
>>> d.appendleft('d')
>>> d
deque(['d', 'c', 'h', 'e', 'l', 'a', 'b'])
>>> d.popleft()
'd'
>>> d.popleft()
'c'
>>> d
deque(['h', 'e', 'l', 'a', 'b'])
18
deque
deque๋ฅผ์ƒ์„ฑํ• ๋•Œmaxlen์ธ์ˆ˜๋ฅผ์ง€์ •ํ•˜๋ฉด์ •ํ•ด์ง„ํฌ๊ธฐ๋ฅผ๊ฐ€์ง„
์›ํ˜•๋ฒ„ํผ๋กœ๋™์ž‘
์ƒˆ๋กœ์šดํ•ญ๋ชฉ์„์ถ”๊ฐ€ํ•˜๋Š”๋ฐ๊ณต๊ฐ„์ด๋ถ€์กฑํ•˜๋ฉด๋ฐ˜๋Œ€ํŽธ์—์žˆ๋Š”ํ•ญ๋ชฉ์‚ญ์ œ
deque([1, 2, 3], maxlen=5)
>>> d.append(4)
>>> d.append(5)
>>> d
deque([1, 2, 3, 4, 5], maxlen=5)
>>> d.append(6)
>>> d.append(7)
>>> d
deque([3, 4, 5, 6, 7], maxlen=5)
19
deque
deque๋Š”๋งค์šฐํšจ์œจ์ ์ธ์ž๋ฃŒ๊ตฌ์กฐ
๋’ค์ชฝ๋์—ํ•ญ๋ชฉ์„์ถ”๊ฐ€ํ•˜๋Š”์ž‘์—…์€list ๋ณด๋‹ค์•ฝ๊ฐ„ ๋А๋ฆผ
ํ•˜์ง€๋งŒ, ์•ž์ชฝ์—ํ•ญ๋ชฉ์„์ถ”๊ฐ€ํ•˜๋Š”์ž‘์—…์€list ๋ณด๋‹ค๋งค์šฐ๋น ๋ฆ„
deque์—์ƒˆ๋กœ์šดํ•ญ๋ชฉ์„์ถ”๊ฐ€ํ•˜๋Š”์ž‘์—…์€์Šค๋ ˆ๋“œ์—์„œ๋„์•ˆ์ „
pickle๋ชจ๋“ˆ๋กœ์ง๋ ฌํ™”๊ฐ€๋Šฅ
20
defaultdict
์—†๋Š”ํ‚ค๋ฅผ์ฒ˜๋ฆฌํ•˜๋Š”๋ถ€๋ถ„์„์ œ์™ธํ•˜๊ณ ๋Š”dict ์™€๋™์ผ
ํ‚ค๋ฅผ์ฐพ์ง€๋ชปํ•˜๋Š”๊ฒฝ์šฐ์—default_factory ๋กœ์ง€์ •ํ•œํ•จ์ˆ˜ํ˜ธ์ถœ
ํ•จ์ˆ˜๋Š”๊ธฐ๋ณธ๊ฐ’์„์ƒ์„ฑํ•˜๊ณ  ์ด๊ฐ’์„ํ•ด๋‹นํ‚ค์˜๊ฐ’์œผ๋กœ์‚ฌ์šฉ
defaultdict ๊ฐ์ฒด๋Š”๋ฐ์ดํ„ฐ๋ฅผ์ถ”์ ํ•˜๊ธฐ ์œ„ํ•ด์„œ
์‚ฌ์ „์„์ปจํ…Œ์ด๋„ˆ๋กœ์“ธ๋•Œ๋งค์šฐ์œ ์šฉ
21
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
์ด๋ฆ„์žˆ๋Š”ํŠœํ”Œ
ํŠœํ”Œ์„์‚ฌ์šฉํ• ๋•Œ๋ถˆํŽธํ•œ์ ์€๊ฐœ๋ณ„ํ•ญ๋ชฉ์„์ˆซ์ž๋กœ๋งŒ์ ‘๊ทผํ• ์ˆ˜์žˆ๋‹ค๋Š”๊ฒƒ
์ด๋Ÿฌํ•œ๋ถˆํŽธ์„์ค„์ด๊ธฐ ์œ„ํ•ด์ด๋ฆ„์žˆ๋Š”ํŠœํ”Œ(namedtuple)์„์ œ๊ณต
namedtuple(typename, fieldnames [, verbose])
์ด๋ฆ„์ดtypename์ธtupleํ•˜์œ„ํด๋ž˜์Šค์ƒ์„ฑ
fieldnames ๋Š”์†์„ฑ์ด๋ฆ„๋ชฉ๋ก์ง€์ •
์œ ํšจํ•œํŒŒ์ด์ฌ์‹๋ณ„์ž์—ฌ์•ผํ•จ
ํŠœํ”Œ์—๋‚˜ํƒ€๋‚ ํ•ญ๋ชฉ๊ณผ ๋™์ผํ•œ์ˆœ์„œ๋กœ์ง€์ •
23
์ด๋ฆ„์žˆ๋Š”ํŠœํ”Œ
>>> from collections import namedtuple
>>> NetworkAddr = namedtuple("NetworkAddr", "hostname port")
>>> a = NetworkAddr("www.python.org", 80)
>>> a.hostname
'www.python.org'
>>> a.port
80
>>> type(a)
<class '__main__.NetworkAddr'>
>>> len(a)
2
>>> isinstance(a, tuple)
True
24
์ด๋ฆ„์žˆ๋Š”ํŠœํ”Œ
์ด๋ฆ„์žˆ๋Š”ํŠœํ”Œ์€๋ฐ์ดํ„ฐ๊ตฌ์กฐ๋กœ๋งŒ์‚ฌ์šฉ๋ ๊ฐ์ฒด๋ฅผ์ •์˜ํ• ๋•Œ์œ ์šฉ
name, shares, price๋ณ€์ˆ˜๋ฅผ๊ฐ€์ง„๋ฐ์ดํ„ฐ๊ตฌ์กฐ๊ฐ€ ํ•„์š”ํ• ๊ฒฝ์šฐ
>>> Stock = namedtuple("Stock", "name shares price")
>>> a = Stock("APL", 30, 45.50)
>>> a
Stock(name='APL', shares=30, price=45.5)
25
heapq
heapq ๋ชจ๋“ˆ์€ํž™(heap)์œผ๋กœ์šฐ์„ ์ˆœ์œ„ํ๋ฅผ๊ตฌํ˜„
ํž™์กฐ๊ฑด์—๋”ฐ๋ผ์ •๋ ฌ๋œํ•ญ๋ชฉ๋“ค์˜๋ฆฌ์ŠคํŠธ
heap[0] ์€ํ•ญ์ƒ๊ฐ€์žฅ์ž‘์€ํ•ญ๋ชฉ์„๋‹ด๊ณ  ์žˆ์Œ
0 ์—์„œ์‹œ์ž‘ํ•˜๋Š”๋ชจ๋“ n์—๋Œ€ํ•ด์„œ
heap[n] <= heap[2 * n+ 1]
heap[n] <= heap[2 * n+ 2] ๋ชจ๋‘๋งŒ์กฑ
26
heapq
heapify(x)
๋ฆฌ์ŠคํŠธx ๋ฅผ์ œ์ž๋ฆฌ์—์„œํž™์œผ๋กœ๋ณ€ํ™˜
heappop(heap)
ํž™์กฐ๊ฑด์„์œ ์ง€ํ•˜๋ฉด์„œ๊ฐ€์žฅ์ž‘์€ํ•ญ๋ชฉ์„๋ฐ˜ํ™˜ํ•˜๊ณ  ์ œ๊ฑฐ
ํž™์ด๋น„์–ด์žˆ์œผ๋ฉดIndexError ๋ฐœ์ƒ
heappush(heap, item)
ํž™์กฐ๊ฑด์„์œ ์ง€ํ•˜๋ฉด์„œitem์„ํž™์—์ถ”๊ฐ€
heappushpop(heap, item)
item์„ํž™์—์ถ”๊ฐ€ํ•˜๊ณ  ํž™์—์„œ๊ฐ€์žฅ์ž‘์€ํ•ญ๋ชฉ์„์ œ๊ฑฐํ•˜๋Š”
๋™์ž‘์„ํ•œ๋ฒˆ์—์ˆ˜ํ–‰
27
heapq
heapreplace(heap, item)
ํž™์—์„œ๊ฐ€์žฅ์ž‘์€์•„์ดํ…œ์„๋ฐ˜ํ™˜ํ•˜๊ณ  ์ œ๊ฑฐํ•˜๋ฉด์„œ
์ƒˆ๋กœ์šดํ•ญ๋ชฉitem์„์ถ”๊ฐ€
ํž™์ด๋น„์–ด์žˆ์œผ๋ฉดIndexError ๋ฐœ์ƒ
merge(s1, s2, ...)
์ •๋ ฌ๋œ๋ฐ˜๋ณต๊ฐ€๋Šฅํ•œ๊ฐ์ฒดs1, s2 ๋“ฑ์„ํ•˜๋‚˜์˜์ •๋ ฌ๋œ์ˆœ์„œ์—ด๋กœ์ƒ์„ฑ
nlargest(n, iterable[, key])
iterable์—์žˆ๋Š”๊ฐ€์žฅํฐn๊ฐœ์˜ํ•ญ๋ชฉ์œผ๋กœ๊ตฌ์„ฑ๋œ๋ฆฌ์ŠคํŠธ์ƒ์„ฑ
nsmallest(n, iterable[, key])
iterable์—์žˆ๋Š”๊ฐ€์žฅ์ž‘์€n๊ฐœ์˜ํ•ญ๋ชฉ์œผ๋กœ๊ตฌ์„ฑ๋œ๋ฆฌ์ŠคํŠธ์ƒ์„ฑ
28
heapq
>>> l = [19, 9, 4, 10, 11, 8, 2]
>>> heapq.heapify(l)
>>> l
[2, 9, 4, 10, 11, 8, 19]
>>> heapq.heappop(l)
2
>>> l
[4, 9, 8, 10, 11, 19]
>>> heapq.heappop(l)
4
>>> l
[8, 9, 19, 10, 11]
>>> heapq.heappush(l, 5)
>>> l
[5, 9, 8, 10, 11, 19]
>>> heapq.heappush(l, 25)
>>> l
[5, 9, 8, 10, 11, 19, 25]
>>>
29
heapq
>>> heapq.nlargest(3, l)
[25, 19, 11]
>>> heapq.nsmallest(3, l)
[5, 8, 9]
>>> a = [3, 5, 1, 9, 7]
>>> b = [6, 4, 8, 2, 10]
>>> heapq.heapify(a)
>>> heapq.heapify(b)
>>> h = heapq.merge(a, b)
>>> h.__next__()
1
>>> h.__next__()
2
>>> h.__next__()
4
>>> h.__next__()
5
30
itertools
itertools ๋ชจ๋“ˆ์€๋ฐ์ดํ„ฐ์—๋Œ€ํ•ด์„œ๋‹ค์–‘ํ•œ๋ณธ๋ณต์„์ˆ˜ํ–‰ํ•˜๋Š”๋ฐ
์‚ฌ์šฉํ• ์ˆ˜์žˆ๋Š”ํšจ์œจ์ ์ธ๋ฐ˜๋ณต์ž๋ฅผ์ƒ์„ฑํ•˜๋Š”ํ•จ์ˆ˜๋“ค์„์ œ๊ณต
๋ชจ๋“ˆ์—์žˆ๋Š”๋ชจ๋“ ํ•จ์ˆ˜๋Š”for ๋ฌธ์ด๋‚˜๊ธฐํƒ€๋ฐ˜๋ณต์ž์™€๊ด€๋ จ์žˆ๋Š”
ํ•จ์ˆ˜์™€ํ•จ๊ป˜์‚ฌ์šฉํ• ์ˆ˜์žˆ๋Š”๋ฐ˜๋ณต์ž๋ฅผ๋ฐ˜ํ™˜
31
chain
chain(iter1, inter2, ..., interN)
๋ฐ˜๋ณต์ž๊ทธ๋ฃน์ด์ฃผ์–ด์งˆ๋•Œ๋ชจ๋“ ๋ฐ˜๋ณต์ž๋ฅผ์—ฐ๊ฒฐํ•˜๋Š”์ƒˆ๋กœ์šด๋ฐ˜๋ณต์ž์ƒ์„ฑ
>>> for x in chain([1, 2, 3], [4, 5, 6]):
... print(x)
...
1
2
3
4
5
6
32
combinations
combinations(iterable, r)
iterable์—์„œ๊ธธ์ดr ์ธ๋ชจ๋“ ํ•˜์œ„์ˆœ์„œ์—ด์„๋ฐ˜ํ™˜ํ•˜๋Š”๋ฐ˜๋ณต์ž์ƒ์„ฑ
>>> list(combinations([1, 2, 3, 4], 2))
[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
>>> list(combinations([1, 2, 3, 4], 3))
[(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
33
count
count([n])
n์—์„œ์‹œ์ž‘ํ•˜๋Š”์—ฐ์†์ ์ธ์ •์ˆ˜๋ฅผ๋งŒ๋“œ๋Š”๋ฐ˜๋ณต์ž์ƒ์„ฑ
n์„์ƒ๋žตํ•˜๋ฉด0 ์—์„œ์‹œ์ž‘
sys.maxint ๋ฅผ๋„˜์œผ๋ฉดโ€‘sys.maxint โ€‘ 1 ์—์„œ๋‹ค์‹œ์‹œ์ž‘
>>> g = count()
>>> g.__next__()
0
>>> g.__next__()
1
>>> g.__next__()
2
>>> g.__next__()
3
34
cycle
cycle(iterable)
iterable์—์žˆ๋Š”์›์†Œ๋“ค์„๊ณ„์†ํ•ด์„œ์ˆœํ™˜ํ•˜๋Š”๋ฐ˜๋ณต์ž์ƒ์„ฑ
>>> g = cycle([1, 2, 3, 4])
>>> g.__next__()
1
>>> g.__next__()
2
>>> g.__next__()
3
>>> g.__next__()
4
>>> g.__next__()
1
>>> g.__next__()
2
35
dropwhile
dropwhile(predicate, iterable)
ํ•จ์ˆ˜predicate(item) ์ดTrue๋กœํ‰๊ฐ€๋˜๋Š”๋™์•ˆ
iterable์—์„œํ•ญ๋ชฉ์„๋ฒ„๋ฆฌ๋Š”๋ฐ˜๋ณต์ž์ƒ์„ฑ
์ผ๋‹จpredicate๊ฐ€ False๋ฅผ๋ฐ˜ํ™˜ํ•˜๋ฉดํ•ด๋‹นํ•ญ๋ชฉ๊ณผ
iterable์—์žˆ๋Š”๋‚˜๋จธ์ง€ํ•ญ๋ชฉ์ด์ƒ์„ฑ
>>> s = [10, 20, -3, 5, 3, 6, -5, 7, 15]
>>> list(dropwhile(lambda x: x >= 0, s))
[-3, 5, 3, 6, -5, 7, 15]
36
groupby
groupby(iterable [, key])
iterable์—์„œ์ƒ์„ฑ๋˜๋Š”์—ฐ์†์ ์ธํ•ญ๋ชฉ์„๊ทธ๋ฃน์ง“๋Š”๋ฐ˜๋ณต์ž์ƒ์„ฑ
๊ทธ๋ฃน์„์ง“๊ธฐ ์œ„ํ•ด์ค‘๋ณต๋œํ•ญ๋ชฉ๊ฒ€์ƒ‰
iterable์—์„œ๋™์ผํ•œํ•ญ๋ชฉ์ด์—ฌ๋Ÿฌ๋ฒˆ๋‚˜์˜ค๋ฉด๊ทธ๋ฃน์ด์ƒ์„ฑ
>>> g = groupby("aaaabbccddeeefffgg")
>>> g.__next__()
('a', <itertools._grouper object at 0x10673cd30>)
>>> g.__next__()
('b', <itertools._grouper object at 0x10673ccf8>)
>>> g.__next__()
('c', <itertools._grouper object at 0x10673ce80>)
37
ifilter (Python3: filter)
ifilter(predicate, iterable)
iterable์—์„œpredicate(item) ์ดTrue์ธํ•ญ๋ชฉ๋งŒ์ถ”์ถœํ•˜๋Š”๋ฐ˜๋ณต์ž์ƒ์„ฑ
predicate๊ฐ€ None์ด๋ฉดiterable์—์žˆ๋Š”๋ชจ๋“ ํ•ญ๋ชฉ์ดTrue๋กœํ‰๊ฐ€
>>> s = [10, 15, -3, 5, -6, 7, 15]
>>> g = filter(lambda x: x < 0, s)
>>> list(g)
[-3, -6]
38
ifilterfalse(Python3: filterfalse)
ifilterfalse(predicate, iterable)
iterable์—์„œpredicate(item) ์ดFalse์ธํ•ญ๋ชฉ๋งŒ์ถ”์ถœํ•˜๋Š”๋ฐ˜๋ณต์ž์ƒ์„ฑ
predicate๊ฐ€ None์ด๋ฉดiterable์—์žˆ๋Š”๋ชจ๋“ ํ•ญ๋ชฉ์ดFalse๋กœํ‰๊ฐ€
>>> from itertools import filterfalse
>>> s = [10, 15, -3, 5, -6, 7, 15]
>>> g = filterfalse(lambda x: x < 0, s)
>>> list(g)
[10, 15, 5, 7, 15]
39
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
islice
islice(iterable, [start,] stop [, step])
iterable[start:stop:step]์ด๋ฐ˜ํ™˜ํ•œ๊ฒƒ๊ณผ ๋น„์Šทํ•œํ•ญ๋ชฉ์„์ถ”์ถœํ•˜๋Š”๋ฐ˜๋ณต์ž์ƒ์„ฑ
>>> s = [10, 15, -3, 5, -6, 7, 15]
>>> g = islice(s, 4)
>>> g.__next__()
10
>>> g.__next__()
15
>>> g.__next__()
-3
>>> g.__next__()
5
>>> g.__next__()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
41
izip (Python3: zip)
izip(iter1, iter2, ... iterN)
(iter1, iter2, ..., iterN) ๋“ฑ๋ฌถ์–ด์„œ์‚ฌ์šฉํ• ์ˆ˜์žˆ๊ฒŒ ํ•ด์ฃผ๋Š”๋ฐ˜๋ณต์ž์ƒ์„ฑ
์ฃผ์–ด์ง„๋ฐ˜๋ณต์ž์ค‘ํ•˜๋‚˜๋ผ๋„๊ฐ’์„๋”์ด์ƒ์ƒ์„ฑํ•˜์ง€์•Š์œผ๋ฉด๋ฐ˜๋ณต์„๋ฉˆ์ถค
>>> a = [1, 3, 5, 7]
>>> b = [2, 4, 6, 8]
>>> c = [3, 5, 7]
>>> g = zip(a, b, c)
>>> g.__next__()
(1, 2, 3)
>>> g.__next__()
(3, 4, 5)
>>> g.__next__()
(5, 6, 7)
>>> g.__next__()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
42
izip_longest (Python3: zip_longest)
izip_longest(iter1, iter2, ..., iterN [,fillvalue=None])
๋ฐ˜๋ณต์žiter1, iter2, iterN ๋“ฑ์„๋ชจ๋‘์†Œ์ง„ํ• ๋•Œ๊นŒ์ง€๋ฐ˜๋ณต์ด์ด์–ด์ง„๋‹ค๋Š”๊ฒƒ์„
์ œ์™ธํ•˜๊ณ ๋Š”izip() ๊ณผ ๋™์ผ
>>> g = zip_longest(a, b, c)
>>> g.__next__()
(1, 2, 3)
>>> g.__next__()
(3, 4, 5)
>>> g.__next__()
(5, 6, 7)
>>> g.__next__()
(7, 8, None)
>>> g.__next__()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
43
permutaions
permutations(iterable [, r])
iterable์—์„œ๊ธธ์ดr ์ธ๋ชจ๋“ ์ˆœ์—ด์„๋ฐ˜ํ™˜ํ•˜๋Š”๋ฐ˜๋ณต์ž์ƒ์„ฑ
r ์„์ƒ๋žตํ•˜๋ฉดiterable์—์žˆ๋Š”ํ•ญ๋ชฉ์ˆ˜์™€๋™์ผํ•œ๊ธธ์ด๋กœ๊ฐ€์ •
>>> a = [1, 3, 5, 7]
>>> permutations(a, 2)
<itertools.permutations object at 0x1066c4f10>
>>> list(permutations(a, 2))
[(1, 3), (1, 5), (1, 7), (3, 1), (3, 5), (3, 7),
(5, 1), (5, 3), (5, 7), (7, 1), (7, 3), (7, 5)]
44
product
product(iter1, iter2, ... iterN [, repeat=1])
iter1, iter2 ๋“ฑ์—์žˆ๋Š”ํ•ญ๋ชฉ๋“ค์˜๋ฐ์นด๋ฅดํŠธ๊ณฑ(Cartesianproduct)์„
๋‚˜ํƒ€๋‚ด๋Š”ํŠœํ”Œ์„๋งŒ๋“ค์–ด๋‚ด๋Š”๋ฐ˜๋ณต์ž์ƒ์„ฑ
>>> a = [1, 3, 5, 7]
>>> b = [2, 4]
>>> product(a, b)
<itertools.product object at 0x106742558>
>>> list(product(a, b))
[(1, 2), (1, 4), (3, 2), (3, 4), (5, 2), (5, 4), (7, 2), (
45
repeat
repeat(object [, times])
object ๋ฅผ๊ณ„์†ํ•ด์„œ๋งŒ๋“œ๋Š”๋ฐ˜๋ณต์ž๋ฅผ์ƒ์„ฑ
times: ๋ฐ˜๋ณตํšŸ์ˆ˜(times ๊ฐ€ ์—†์œผ๋ฉด๋์—†์ด๋ฐ˜๋ณต)
>>> g = repeat([1, 3, 5], 4)
>>> g.__next__()
[1, 3, 5]
>>> g.__next__()
[1, 3, 5]
>>> g.__next__()
[1, 3, 5]
>>> g.__next__()
[1, 3, 5]
>>> g.__next__()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
46
starmap
starmap(func [, iterable)
func(*item) ๋“ค์„๋งŒ๋“ค์–ด๋‚ด๋Š”๋ฐ˜๋ณต์ž์ƒ์„ฑ
item์€iterable์—์„œ๊ฐ€์ ธ์˜ด
func() ์„์ œ๋Œ€๋กœํ˜ธ์ถœํ• ์ˆ˜์žˆ๋Š”iterable์—๋Œ€ํ•ด์„œ๋งŒ์ œ๋Œ€๋กœ๋™์ž‘
>>> g = starmap(lambda a, b: a * b, [(1, 2), (2, 3), (3, 4
>>> g.__next__()
2
>>> g.__next__()
6
>>> g.__next__()
12
>>> g.__next__()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
47
takewhile
takewhile(predicate [, iterable])
predicate(item) ์ดTrue๋กœํ‰๊ฐ€๋˜๋Š”๋™์•ˆiterable์—์„œํ•ญ๋ชฉ์„์ถ”์ถœํ•˜๋Š”
๋ฐ˜๋ณต์ž์ƒ์„ฑ
predicate๊ฐ€ False๋กœํ‰๊ฐ€๋˜๋Š”์ฆ‰์‹œ๋ฐ˜๋ณต์„๋ฉˆ์ถค
>>> s = [10, 15, -3, 5, -6, 7, 15]
>>> g = takewhile(lambda x: x >= 0, s)
>>> g.__next__()
10
>>> g.__next__()
15
>>> g.__next__()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
48
tee
tee(iterable [, n])
iterable์—์„œn๊ฐœ์˜๋…๋ฆฝ์ ์ธ๋ฐ˜๋ณต์ž์ƒ์„ฑ
์ƒ์„ฑ๋œ๋ฐ˜๋ณต์ž๋“ค์€n๊ฐœ ํ•ญ๋ชฉํŠœํ”Œ๋กœ๋ฐ˜ํ™˜
n: ๊ธฐ๋ณธ๊ฐ’์€2
>>> s = [10, 15, -3, 5, -6, 7, 15]
>>> g = tee(s)
>>> g[0].__next__()
10
>>> g[0].__next__()
15
>>> g[1].__next__()
10
>>> g[1].__next__()
15
49

Python Programming: Data Structure