5. ìë£ êµ¬ì¡°Â¶
ì´ ì¥ììë ì¬ë¬ë¶ì´ ì´ë¯¸ ë°°ì´ ê²ë¤ì ì¢ ë ìì¸í ì¤ëª íê³ , ëª ê°ì§ ìë¡ì´ ê²ë¤ì ë§ë¶ì ëë¤.
5.1. 리ì¤í¸ ë 보기¶
The list data type has some more methods. Here are all of the methods of list objects:
- list.append(x)
리ì¤í¸ì ëì í목ì ëí©ëë¤.
a[len(a):] = [x]ì ë¹ì·í©ëë¤.
- list.extend(iterable)
리ì¤í¸ì ëì ì´í°ë¬ë¸ì 모ë í목ì ë§ë¶ì¬ì íì¥í©ëë¤.
a[len(a):] = iterableì ë¹ì·í©ëë¤.
- list.insert(i, x)
주ì´ì§ ìì¹ì í목ì ì½ì í©ëë¤. 첫 ë²ì§¸ ì¸ìë ì½ì ëë ììê° ê°ê² ë ì¸ë±ì¤ì ëë¤. ê·¸ëì
a.insert(0, x)ë 리ì¤í¸ì ì²ìì ì½ì íê³ ,a.insert(len(a), x)ëa.append(x)ì ëë±í©ëë¤.
- list.remove(x)
리ì¤í¸ìì ê°ì´ x ì ê°ì 첫 ë²ì§¸ í목ì ìì í©ëë¤. ê·¸ë° íëª©ì´ ìì¼ë©´
ValueError를 ì¼ì¼íµëë¤.
- list.pop([i])
리ì¤í¸ìì 주ì´ì§ ìì¹ì ìë í목ì ìì íê³ , ê·¸ í목ì ëë ¤ì¤ëë¤. ì¸ë±ì¤ë¥¼ ì§ì íì§ ìì¼ë©´,
a.pop()ì 리ì¤í¸ì ë§ì§ë§ í목ì ìì íê³ ëë ¤ì¤ëë¤. 리ì¤í¸ê° ë¹ì´ ìê±°ë ì¸ë±ì¤ê° 리ì¤í¸ ë²ì를 ë²ì´ëë©´IndexError를 ë°ììíµëë¤.
- list.clear()
리ì¤í¸ì 모ë í목ì ìì í©ëë¤.
del a[:]ì ë¹ì·í©ëë¤.
- list.index(x[, start[, end]])
Return zero-based index of the first occurrence of x in the list. Raises a
ValueErrorif there is no such item.ì íì ì¸ ì¸ì start ì end ë ì¬ë¼ì´ì¤ í기ë²ì²ë¼ í´ìëê³ , ê²ìì 리ì¤í¸ì í¹ë³í ìë¸ ìíì¤ë¡ ì ííë ë° ì¬ì©ë©ëë¤. ëë ¤ì£¼ë ì¸ë±ì¤ë start ì¸ìê° ìëë¼ ì ì²´ ìíì¤ì ììì 기ì¤ì¼ë¡ í©ëë¤.
- list.count(x)
리ì¤í¸ìì x ê° ë±ì¥íë íì를 ëë ¤ì¤ëë¤.
- list.sort(*, key=None, reverse=False)
리ì¤í¸ì í목ë¤ì ì ì리ìì ì ë ¬í©ëë¤ (ì¸ìë¤ì ì ë ¬ 커ì¤í°ë§ì´ì ì´ì ì ì¬ì©ë ì ììµëë¤. ì¤ëª ì
sorted()를 ë³´ì¸ì).
- list.reverse()
리ì¤í¸ì ììë¤ì ì ì리ìì ë¤ì§ìµëë¤.
- list.copy()
리ì¤í¸ì ìì ì¬ë³¸ì ëë ¤ì¤ëë¤.
a[:]ì ë¹ì·í©ëë¤.
리ì¤í¸ ë©ìë ëë¶ë¶ì ì¬ì©íë ì:
>>> fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
>>> fruits.count('apple')
2
>>> fruits.count('tangerine')
0
>>> fruits.index('banana')
3
>>> fruits.index('banana', 4) # ìì¹ 4ììë¶í° ë¤ì banana 를 ì°¾ìµëë¤
6
>>> fruits.reverse()
>>> fruits
['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange']
>>> fruits.append('grape')
>>> fruits
['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange', 'grape']
>>> fruits.sort()
>>> fruits
['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi', 'orange', 'pear']
>>> fruits.pop()
'pear'
ìë§ë ì¬ë¬ë¶ì insert, remove, sort ê°ì ë©ìëë¤ì´ 리ì¤í¸ë¥¼ ìì í ë¿ ë°í ê°ì´ ì¶ë ¥ëì§ ìë ê²ì ììì±ì ê²ì
ëë¤ â 기본 None ì ëë ¤ì£¼ê³ ììµëë¤. [1] ì´ê²ì íì´ì¬ìì 모ë ê°ë³ ìë£ êµ¬ì¡°ë¤ì ì ì©ëë ì¤ê³ ì리ì
ëë¤.
ìë§ë ì¬ë¬ë¶ì´ ììì±ì ë ë¤ë¥¸ ì¬ì¤ì 모ë ë°ì´í°ë¥¼ ì ë ¬íê±°ë ë¹êµí ìë ìë¤ë ê²ì
ëë¤. ì를 ë¤ì´, ì ì를 문ìì´ê³¼ ë¹êµí ì ìê³ Noneì ë¤ë¥¸ íê³¼ ë¹êµí ì ì기 ë문ì [None, 'hello', 10]ë ì ë ¬ëì§ ììµëë¤. ëí ì ìë ëì ê´ê³ê° ìë íì´ ììµëë¤. ì를 ë¤ì´, 3+4j < 5+7jë ì¬ë°ë¥¸ ë¹êµê° ìëëë¤.
5.1.1. 리ì¤í¸ë¥¼ ì¤íì¼ë¡ ì¬ì©í기¶
The list methods make it very easy to use a list as a stack, where the last
element added is the first element retrieved (âlast-in, first-outâ). To add an
item to the top of the stack, use append(). To retrieve an item from the
top of the stack, use pop() without an explicit index. For example:
>>> stack = [3, 4, 5]
>>> stack.append(6)
>>> stack.append(7)
>>> stack
[3, 4, 5, 6, 7]
>>> stack.pop()
7
>>> stack
[3, 4, 5, 6]
>>> stack.pop()
6
>>> stack.pop()
5
>>> stack
[3, 4]
5.1.2. 리ì¤í¸ë¥¼ íë¡ ì¬ì©í기¶
리ì¤í¸ë¥¼ íë¡ ì¬ì©íë ê²ë ê°ë¥íë°, ì²ìì¼ë¡ ë£ì ììê° ì²ìì¼ë¡ 꺼ë´ì§ë ììì ëë¤ (âfirst-in, first-outâ); íì§ë§, 리ì¤í¸ë ì´ ëª©ì ìë í¨ì¨ì ì´ì§ ììµëë¤. 리ì¤í¸ì ëì ë§ë¶ì´ê±°ë, ëìì 꺼ë´ë ê²ì ë¹ ë¥´ì§ë§, 리ì¤í¸ì 머리ì ë§ë¶ì´ê±°ë 머리ìì 꺼ë´ë ê²ì ë립ëë¤ (ë¤ë¥¸ ììë¤ì 모ë í ì¹¸ì© ì´ëìì¼ì¼ í기 ë문ì ëë¤).
í를 구ííë ¤ë©´, ì ëììì ë§ë¶ì´ê¸°ì 꺼ë´ê¸°ê° 모ë ë¹ ë¥´ëë¡ ì¤ê³ë collections.deque 를 ì¬ì©íì¸ì. ì를 ë¤ì´:
>>> from collections import deque
>>> queue = deque(["Eric", "John", "Michael"])
>>> queue.append("Terry") # Terry ëì°©
>>> queue.append("Graham") # Graham ëì°©
>>> queue.popleft() # ì²ì ëì°©í ì¬ëì´ ì´ì ë ë©ëë¤
'Eric'
>>> queue.popleft() # ëë²ì§¸ ëì°©í ì¬ëì´ ì´ì ë ë©ëë¤
'John'
>>> queue # ëì°©í ììëë¡ ë¨ììë í
deque(['Michael', 'Terry', 'Graham'])
5.1.3. 리ì¤í¸ ì»´í리í¨ì ¶
리ì¤í¸ ì»´í리í¨ì ì 리ì¤í¸ë¥¼ ë§ëë ê°ê²°í ë°©ë²ì ì ê³µí©ëë¤. íí ì©ëë, ê° ììê° ë¤ë¥¸ ìíì¤ë ì´í°ë¬ë¸ì 멤ë²ë¤ì ì´ë¤ ì°ì°ì ì ì©í ê²°ê³¼ì¸ ë¦¬ì¤í¸ë¥¼ ë§ë¤ê±°ë, ì´ë¤ ì¡°ê±´ì ë§ì¡±íë ììë¤ë¡ 구ì±ë ìë¸ ìíì¤ë¥¼ ë§ëë ê²ì ëë¤.
ì를 ë¤ì´, ì ê³±ìì 리ì¤í¸ë¥¼ ë§ë¤ê³ ì¶ë¤ê³ ê°ì íì, ì´ë° ìì ëë¤:
>>> squares = []
>>> for x in range(10):
... squares.append(x**2)
...
>>> squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
ì´ê²ì x ë¼ë ì´ë¦ì ë³ì를 ë§ë¤ê³ (ëë ë®ì´ì°ê³ ) 루íê° ì¢
ë£ë íìë ë¨ììê² ë§ë ë¤ë ê²ì ì ìíì¸ì. ì´ë¤ ë¶ìì©ë ìì´, ì ê³±ìì 리ì¤í¸ë¥¼ ì´ë° ìì¼ë¡ ê³ì°í ì ììµëë¤:
squares = list(map(lambda x: x**2, range(10)))
ëë, ì´ë ê² í ìë ììµëë¤:
squares = [x**2 for x in range(10)]
ì´ê²ì´ ë ê°ê²°íê³ ì½ê¸° ì½ìµëë¤.
리ì¤í¸ ì»´í리í¨ì
ì ííìê³¼ ê·¸ ë¤ë¥¼ ë°ë¥´ë for ì ê³¼ ìê±°ë ì¬ë¬ ê°ì for ë if ì ë¤ì ê°ì¸ë ëê´í¸ë¡ 구ì±ë©ëë¤. ê·¸ ê²°ê³¼ë ì 리ì¤í¸ì¸ë°, for ì if ì ì 문맥ìì ííìì ê°ì 구í´ì ë§ë¤ì´ì§ëë¤. ì를 ë¤ì´, ì´ ë¦¬ì¤í¸ ì»´í리í¨ì
ì ë 리ì¤í¸ì ììë¤ì ìë¡ ê°ì§ ìì ê²ë¼ë¦¬ ê²°í©í©ëë¤:
>>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
ê·¸ë¦¬ê³ , ì´ê²ì ë¤ìê³¼ ëë±í©ëë¤:
>>> combs = []
>>> for x in [1,2,3]:
... for y in [3,1,4]:
... if x != y:
... combs.append((x, y))
...
>>> combs
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
ë ì½ë ì¡°ê°ìì for ì if 문ì ììê° ê°ìì ì ìíì¸ì.
ííìì´ ííì´ë©´ (ì¦ ìì ììì (x, y)), ë°ëì ê´í¸ë¡ ëë¬ì¸ì¼ í©ëë¤.
>>> vec = [-4, -2, 0, 2, 4]
>>> # ê°ì ëë°°ë¡ íì¬ ì 리ì¤í¸ë¥¼ ë§ëëë¤
>>> [x*2 for x in vec]
[-8, -4, 0, 4, 8]
>>> # ìì를 ì ì¸íëë¡ ë¦¬ì¤í¸ë¥¼ íí°ë§í©ëë¤
>>> [x for x in vec if x >= 0]
[0, 2, 4]
>>> # 모ë ììì í¨ì를 ì ì©í©ëë¤
>>> [abs(x) for x in vec]
[4, 2, 0, 2, 4]
>>> # ê° ììì ë©ìë를 í¸ì¶í©ëë¤
>>> freshfruit = [' banana', ' loganberry ', 'passion fruit ']
>>> [weapon.strip() for weapon in freshfruit]
['banana', 'loganberry', 'passion fruit']
>>> # (ì«ì, ì ê³±) ê³¼ ê°ì 2-ííì 리ì¤í¸ë¥¼ ë§ëëë¤
>>> [(x, x**2) for x in range(6)]
[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]
>>> # ííì ê´í¸ë¡ 묶ì´ì¼í©ëë¤, ê·¸ë ì§ ìì¼ë©´ ìë¬ê° ë°ìí©ëë¤
>>> [x, x**2 for x in range(6)]
File "<stdin>", line 1
[x, x**2 for x in range(6)]
^^^^^^^
SyntaxError: did you forget parentheses around the comprehension target?
>>> # ë ê°ì 'for' 를 ê°ë 리ì¤í¸ ì»´í리í¨ì
ì¼ë¡ 리ì¤í¸ë¥¼ íííê² ë§ëëë¤
>>> vec = [[1,2,3], [4,5,6], [7,8,9]]
>>> [num for elem in vec for num in elem]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
리ì¤í¸ ì»´í리í¨ì ì ë³µì¡í ííìê³¼ ì¤ì²©ë í¨ìë¤ì í¬í¨í ì ììµëë¤:
>>> from math import pi
>>> [str(round(pi, i)) for i in range(1, 6)]
['3.1', '3.14', '3.142', '3.1416', '3.14159']
5.1.4. ì¤ì²©ë 리ì¤í¸ ì»´í리í¨ì ¶
리ì¤í¸ ì»´í리í¨ì ì 첫 ííìì¼ë¡ ììì ííìì´ ì¬ ì ìëë°, ë¤ë¥¸ 리ì¤í¸ ì»´í리í¨ì ë ê°ë¥í©ëë¤.
ë¤ìê³¼ ê°ì 길ì´ê° 4ì¸ ë¦¬ì¤í¸ 3ê°ì 리ì¤í¸ë¡ 구íë 3x4 íë ¬ì ì를 ë´ ìë¤:
>>> matrix = [
... [1, 2, 3, 4],
... [5, 6, 7, 8],
... [9, 10, 11, 12],
... ]
ë¤ì 리ì¤í¸ ì»´í리í¨ì ì íê³¼ ì´ì ì ì¹ ìíµëë¤:
>>> [[row[i] for row in matrix] for i in range(4)]
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
ìì ìì ë³´ìë¯ì´, ë´ë¶ 리ì¤í¸ ì»´í리í¨ì
ì ë¤ë°ë¥´ë for ì 문맥ìì ê°ì´ 구í´ì§ëë¤. ê·¸ëì ì´ ìë ë¤ìê³¼ ëë±í©ëë¤:
>>> transposed = []
>>> for i in range(4):
... transposed.append([row[i] for row in matrix])
...
>>> transposed
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
ì´ê²ì ë¤ì ë¤ìê³¼ ê°ìµëë¤:
>>> transposed = []
>>> for i in range(4):
... # ë¤ì 3ì¤ì ì¤ì²©ë 리ì¤í¸ ì»´í리í¨ì
ì 구íí©ëë¤
... transposed_row = []
... for row in matrix:
... transposed_row.append(row[i])
... transposed.append(transposed_row)
...
>>> transposed
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
ì¤ì ì¸ìììë, ë³µì¡í íë¦ë¬¸ë³´ë¤ ë´ì¥ í¨ìë¤ì ì í¸í´ì¼ í©ëë¤. ì´ ê²½ì°ìë zip() í¨ìê° ì ìí ì í ì ììµëë¤:
>>> list(zip(*matrix))
[(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]
ì´ ì¤ì ëì¤ë ì ì¤í°ë¦¬ì¤í¬ì ëí ìì¸í ë´ì©ì ì¸ì ëª©ë¡ ì¸ í¨í¹ ì ë³´ì¸ì.
5.2. del 문¶
There is a way to remove an item from a list given its index instead of its
value: the del statement. This differs from the pop() method
which returns a value. The del statement can also be used to remove
slices from a list or clear the entire list (which we did earlier by assignment
of an empty list to the slice). For example:
>>> a = [-1, 1, 66.25, 333, 333, 1234.5]
>>> del a[0]
>>> a
[1, 66.25, 333, 333, 1234.5]
>>> del a[2:4]
>>> a
[1, 66.25, 1234.5]
>>> del a[:]
>>> a
[]
del ë ë³ ì체를 ìì íëë°ìë ì¬ì©ë ì ììµëë¤:
>>> del a
ì´íì ì´ë¦ a 를 참조íë ê²ì ìë¬ì
ëë¤ (ì ì´ë ë¤ë¥¸ ê°ì´ ìë¡ ëì
ë기 ì ê¹ì§). ë¤ìì del ì ë¤ë¥¸ ì©ë를 ë³´ê² ë©ëë¤.
5.3. ííê³¼ ìíì¤Â¶
리ì¤í¸ì 문ìì´ì´ ì¸ë±ì±ê³¼ ì¬ë¼ì´ì± ì°ì°ê³¼ ê°ì ë§ì ì±ì§ì ê³µì í¨ì ë³´ììµëë¤. ì´ê²ë¤ì ìíì¤ ìë£ íì ë ê°ì§ ìì ëë¤ (ìíì¤ í â list, tuple, range 를 ë³´ì¸ì). íì´ì¬ì ì§ííë ì¸ì´ì´ê¸° ë문ì, ë¤ë¥¸ ìíì¤ ìë£íì´ ì¶ê°ë ìë ììµëë¤. ë¤ë¥¸ íì¤ ìíì¤ ìë£ íì´ ììµëë¤: íí ì ëë¤.
ííì ì¼íë¡ êµ¬ë¶ëë ì¬ë¬ ê°ì¼ë¡ 구ì±ë©ëë¤. ì를 ë¤ì´:
>>> t = 12345, 54321, 'hello!'
>>> t[0]
12345
>>> t
(12345, 54321, 'hello!')
>>> # ííì ì¤ì²©ë ì ììµëë¤:
>>> u = t, (1, 2, 3, 4, 5)
>>> u
((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
>>> # ííì ë¶ë³ì
ëë¤:
>>> t[0] = 88888
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> # íì§ë§ ê°ë³ ê°ì²´ë¥¼ í¬í¨í ì ììµëë¤:
>>> v = ([1, 2, 3], [3, 2, 1])
>>> v
([1, 2, 3], [3, 2, 1])
ì¬ë¬ë¶ì´ ë³´ë¯ì´, ì¶ë ¥ëë ííì íì ê´í¸ë¡ ëë¬ì¸ì ëë¤, ê·¸ëì ì¤ì²©ë ííì´ ì¬ë°ë¥´ê² í´ìë©ëë¤; ì¢ ì¢ ê´í¸ê° íìí기ë íì§ë§ (ííì´ ë í° ííìì ì¼ë¶ì¼ ë), ëë¬ì¼ ê´í¸ì í¨ê» ëë ìì´ ì ë ¥ë ì ììµëë¤. ííì ê°ë³ í목ì ëì íë ê²ì ê°ë¥íì§ ìì§ë§, 리ì¤í¸ ê°ì ê°ë³ ê°ì²´ë¥¼ í¬í¨íë ííì ë§ë¤ ìë ììµëë¤.
ííì´ ë¦¬ì¤í¸ì²ë¼ ë³´ì¸ë¤ íëë¼ë, ì´ê²ë¤ì ë¤ë¥¸ ìí©ìì ë¤ë¥¸ 목ì ì¼ë¡ ì¬ì©ë©ëë¤. ííì ë¶ë³ ì´ê³ , ë³´íµ ì´ì§ì ì¸ ììë¤ì ìíì¤ë¥¼ í¬í¨í©ëë¤. ììë¤ì ì¸ í¨í¹ (ì´ ì¹ì
ì ë¤ì ëì¨ë¤) ì´ë ì¸ë±ì± (ëë ë¤ìë íí ì ê²½ì°ë ì´í¸ë¦¬ë·°í¸ë¡ë) ì¼ë¡ ì¡ì¸ì¤í©ëë¤. 리ì¤í¸ë ê°ë³ ì´ê³ , ììë¤ì ë³´íµ ë±ì§ ì ì´ê³ 리ì¤í¸ì ëí ì´í°ë ì´ì
ì¼ë¡ ì¡ì¸ì¤ ë©ëë¤.
í¹ë³í 문ì ë ë¹ìê±°ë íëì í목ì ê°ë ííì ë§ëë ê²ì ëë¤: ì´ ê²½ì°ë¥¼ ìì©í기 ìí´ ë¬¸ë²ì ì¶ê°ì ì¸ ìì¸ ì¬íì ê°ê³ ììµëë¤. ë¹ ííì ë¹ ê´í¸ ìì¼ë¡ ë§ë¤ì´ì§ëë¤; íëì í목ì¼ë¡ 구ì±ë ííì ê° ë¤ì ì¼í를 ë¶ì¬ì ë§ëëë¤ (ê° íë를 ê´í¸ë¡ ëë¬ì¸ê¸°ë§ íë ê²ì¼ë¡ë ì¶©ë¶íì§ ììµëë¤). ì¶í©ëë¤, íì§ë§ í¨ê³¼ì ì ëë¤. ì를 ë¤ì´:
>>> empty = ()
>>> singleton = 'hello', # <-- ë§ì§ë§ ì¼íì 주ìíì¸ì
>>> len(empty)
0
>>> len(singleton)
1
>>> singleton
('hello',)
ë¬¸ì¥ t = 12345, 54321, 'hello!' ë íí í¨í¹ ì ìì
ëë¤: ê° 12345, 54321, 'hello!' ë í¨ê» ííë¡ í¨í¹ ë©ëë¤. ë°ë ì°ì° ëí ê°ë¥í©ëë¤:
>>> x, y, z = t
ì´ê²ì, ì¶©ë¶í ì ì íê²ë, ìíì¤ ì¸ í¨í¹ ì´ë¼ê³ ë¶ë¦¬ê³ ì¤ë¥¸ìª½ì ì´ë¤ ìíì¤ê° ìë ë©ëë¤. ìíì¤ ì¸ í¨í¹ì ë±í¸ì ì¢ë³ì ìíì¤ì ìë ììë¤ê³¼ ê°ì ê°ìì ë³ìë¤ì´ ì¬ ê²ì ì구í©ëë¤. ë¤ì¤ ëì ì ì¬ì¤ íí í¨í¹ê³¼ ìíì¤ ì¸ í¨í¹ì ì¡°í©ì¼ë¿ì´ë¼ë ê²ì ì ìíì¸ì.
5.4. ì§í©Â¶
Python also includes a data type for sets. A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.
ì§í©ì ë§ë¤ ëë ì¤ê´í¸ë set() í¨ì를 ì¬ì©í ì ììµëë¤. 주ìì¬í: ë¹ ì§í©ì ë§ë¤ë ¤ë©´ set() ì ì¬ì©í´ì¼ í©ëë¤. {} ê° ìëëë¤; íìë ë¹ ëì
ë리를 ë§ëëë°, ë¤ì ì¹ì
ìì ë¤ë£¹ëë¤.
Because sets are unordered, iterating over them or printing them can produce the elements in a different order than you expect.
ì¬ê¸° ê°ëµí ì¤ì°ì´ ììµëë¤:
>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
>>> print(basket) # ì¤ë³µì´ ì ê±°ëììì ë³´ì¬ì¤ëë¤
{'orange', 'banana', 'pear', 'apple'}
>>> 'orange' in basket # ë¹ ë¥¸ 멤ë²ì ê²ì¬
True
>>> 'crabgrass' in basket
False
>>> # ë ë¨ì´ì ê³ ì í ê¸ìë¤ë¡ ì§í© ì°ì° ìì°
>>>
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a # a ì ê³ ì í ê¸ìë¤
{'a', 'r', 'b', 'c', 'd'}
>>> a - b # a ì ìì¼ë b ì ìë ê¸ìë¤
{'r', 'd', 'b'}
>>> a | b # a ë b, í¹ì ì쪽 모ëì ìë ê¸ìë¤
{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
>>> a & b # a ì b 모ëì ìë ê¸ìë¤
{'a', 'c'}
>>> a ^ b # a ë b ì ìì§ë§ ì쪽 모ëì ìì§ë ìì ê¸ìë¤
{'r', 'd', 'b', 'm', 'z', 'l'}
리ì¤í¸ ì»´í리í¨ì ê³¼ ì ì¬íê², ì§í© ì»´í리í¨ì ë ì§ìë©ëë¤:
>>> a = {x for x in 'abracadabra' if x not in 'abc'}
>>> a
{'r', 'd'}
5.5. ëì ë리¶
Another useful data type built into Python is the dictionary (see
매í í â dict). Dictionaries are sometimes found in other languages as
âassociative memoriesâ or âassociative arraysâ. Unlike sequences, which are
indexed by a range of numbers, dictionaries are indexed by keys, which can be
any immutable type; strings and numbers can always be keys. Tuples can be used
as keys if they contain only strings, numbers, or tuples; if a tuple contains
any mutable object either directly or indirectly, it cannot be used as a key.
You canât use lists as keys, since lists can be modified in place using index
assignments, slice assignments, or methods like append() and
extend().
ëì
ë리를 (í ëì
ë리 ììì) í¤ê° ì¤ë³µëì§ ìëë¤ë ì ì½ ì¡°ê±´ì ê°ì§ í¤: ê° ìì ì§í©ì¼ë¡ ìê°íë ê²ì´ ìµì ì
ëë¤. ì¤ê´í¸ ìì ë¹ ëì
ë리를 ë§ëëë¤: {}. ì¤ê´í¸ ìì ì¼íë¡ ë¶ë¦¬ë í¤:ê° ìë¤ì 목ë¡ì ë£ì¼ë©´, ëì
ë리ì ì´ê¸° í¤:ê° ìë¤ì ì ê³µí©ëë¤; ì´ê²ì´ ëì
ëë¦¬ê° ì¶ë ¥ëë ë°©ìì´ê¸°ë í©ëë¤.
The main operations on a dictionary are storing a value with some key and
extracting the value given the key. It is also possible to delete a key:value
pair with del. If you store using a key that is already in use, the old
value associated with that key is forgotten.
Extracting a value for a non-existent key by subscripting (d[key]) raises a
KeyError. To avoid getting this error when trying to access a possibly
non-existent key, use the get() method instead, which returns
None (or a specified default value) if the key is not in the dictionary.
ëì
ë¬ë¦¬ì list(d) 를 ìííë©´ ëì
ë리ìì ì¬ì©ëê³ ìë 모ë í¤ì 리ì¤í¸ë¥¼ ì½ì
ììëë¡ ëë ¤ì¤ëë¤ (ì ë ¬ì ìíë©´ ëì sorted(d) 를 ì¬ì©íë©´ ë©ëë¤). íëì í¤ê° ëì
ë리ì ìëì§ ê²ì¬íë ¤ë©´, in í¤ìëë¤ ì¬ì©íì¸ì.
ì¬ê¸°ì ëì ë리를 ì¬ì©íë ì¡°ê·¸ë§í ìê° ììµëë¤:
>>> tel = {'jack': 4098, 'sape': 4139}
>>> tel['guido'] = 4127
>>> tel
{'jack': 4098, 'sape': 4139, 'guido': 4127}
>>> tel['jack']
4098
>>> tel['irv']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'irv'
>>> print(tel.get('irv'))
None
>>> del tel['sape']
>>> tel['irv'] = 4127
>>> tel
{'jack': 4098, 'guido': 4127, 'irv': 4127}
>>> list(tel)
['jack', 'guido', 'irv']
>>> sorted(tel)
['guido', 'irv', 'jack']
>>> 'guido' in tel
True
>>> 'jack' not in tel
False
dict() ìì±ìë í¤-ê° ìë¤ì ìíì¤ë¡ ë¶í° ì§ì ëì
ë리를 구ì±í©ëë¤.
>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
{'sape': 4139, 'guido': 4127, 'jack': 4098}
ì´ì ëí´, ëì ë리 ì»´í리í¨ì ì ììì í¤ì ê° ííìë¤ë¡ ë¶í° ëì ë리를 ë§ëëë° ì¬ì©ë ì ììµëë¤:
>>> {x: x**2 for x in (2, 4, 6)}
{2: 4, 4: 16, 6: 36}
í¤ê° ê°ë¨í 문ìì´ì¼ ë, ëë¡ í¤ìë ì¸ìë¤ì ì¬ì©í´ì ìì ì§ì íê¸°ê° ì½ìµëë¤:
>>> dict(sape=4139, guido=4127, jack=4098)
{'sape': 4139, 'guido': 4127, 'jack': 4098}
5.6. 루í í í¬ë¶
ëì
ëë¦¬ë¡ ë£¨íí ë, items() ë©ìë를 ì¬ì©íë©´ í¤ì 거기ì ëìíë ê°ì ëìì ì»ì ì ììµëë¤.
>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
>>> for k, v in knights.items():
... print(k, v)
...
gallahad the pure
robin the brave
ìíì¤ë¥¼ 루íí ë, enumerate() í¨ì를 ì¬ì©íë©´ ìì¹ ì¸ë±ì¤ì ëìíë ê°ì ëìì ì»ì ì ììµëë¤.
>>> for i, v in enumerate(['tic', 'tac', 'toe']):
... print(i, v)
...
0 tic
1 tac
2 toe
ëì´ë ê·¸ ì´ìì ìíì¤ë¥¼ ëìì 루ííë ¤ë©´, zip() í¨ìë¡ ìí¸ë¦¬ë¤ì ìì ë§ë¤ ì ììµëë¤.
>>> questions = ['name', 'quest', 'favorite color']
>>> answers = ['lancelot', 'the holy grail', 'blue']
>>> for q, a in zip(questions, answers):
... print('What is your {0}? It is {1}.'.format(q, a))
...
What is your name? It is lancelot.
What is your quest? It is the holy grail.
What is your favorite color? It is blue.
ìíì¤ë¥¼ ê±°ê¾¸ë¡ ë£¨ííë ¤ë©´, 먼ì ì ë°©í¥ì¼ë¡ ìíì¤ë¥¼ ì§ì í ë¤ìì reversed() í¨ì를 í¸ì¶íì¸ì.
>>> for i in reversed(range(1, 10, 2)):
... print(i)
...
9
7
5
3
1
ì ë ¬ë ììë¡ ìíì¤ë¥¼ 루ííë ¤ë©´, sorted() í¨ì를 ì¬ì©í´ì ìì¤ë¥¼ ë³ê²½íì§ ìê³ ë ì ë ¬ë ì 리ì¤í¸ë¥¼ ë°ì ì ììµëë¤.
>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
>>> for i in sorted(basket):
... print(i)
...
apple
apple
banana
orange
orange
pear
ìíì¤ì ëí´ set()ì ì¬ì©íë©´ ì¤ë³µ ìì를 ì ê±°í©ëë¤. ìíì¤ì ëí´ set()ê³¼ sorted()를 í¨ê» ì¬ì©íë ê²ì ìíì¤ì ê³ ì í ìì를 ì ë ¬ë ììë¡ ë£¨ííë ê´ì©ì ë°©ë²ì
ëë¤.
>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
>>> for f in sorted(set(basket)):
... print(f)
...
apple
banana
orange
pear
ëë¡ ë£¨í를 ëê³ ìë 리ì¤í¸ë¥¼ ë³ê²½íê³ í ì í¹ì ëëëë¤; íì§ë§, ì¢ ì¢ , ëì ì 리ì¤í¸ë¥¼ ë§ëë ê²ì´ ë ê°ë¨íê³ ë ìì í©ëë¤.
>>> import math
>>> raw_data = [56.2, float('NaN'), 51.7, 55.3, 52.5, float('NaN'), 47.8]
>>> filtered_data = []
>>> for value in raw_data:
... if not math.isnan(value):
... filtered_data.append(value)
...
>>> filtered_data
[56.2, 51.7, 55.3, 52.5, 47.8]
5.7. ì¡°ê±´ ë 보기¶
while ê³¼ if 문ìì ì¬ì©ëë ì¡°ê±´ìë ë¹êµë¿ë§ ìëë¼ ëª¨ë ì°ì°ì를 ì¬ì©í ì ììµëë¤.
ë¹êµ ì°ì°ì in ê³¼ not in ì ê°ì´ 컨í
ì´ëì ìëì§ (ìëì§) íì¸íë 멤ë²ì ê²ì¬ì
ëë¤. ì°ì°ì is ì is not ì ë ê°ì²´ê° ì§ì§ë¡ ê°ì ê°ì²´ì¸ì§ ë¹êµí©ëë¤. 모ë ë¹êµ ì°ì°ìë¤ì ê°ì ì°ì ìì를 ê°ëë°, 모ë ì°ì ì°ì°ìë¤ë³´ë¤ ë®ìµëë¤.
ë¹êµë ì°ìí ì ììµëë¤. ì를 ë¤ì´, a < b == c ë, a ê° b ë³´ë¤ ìê³ , ëìì b ê° c ì ê°ìì§ ê²ì¬í©ëë¤.
ë¹êµë ë
¼ë¦¬ ì°ì°ì and ì or 를 ì¬ì©í´ì ê²°í©í ì ìê³ , ë¹êµì ê²°ê³¼ë (ëë ê·¸ ë°ì 모ë ë
¼ë¦¬ ííìì) not ì¼ë¡ ë¶ì ë ì ììµëë¤. ì´ê²ë¤ì ë¹êµ ì°ì°ìë³´ë¤ ë®ì ì°ì ìì를 ê°ìµëë¤. ì´ê² ê°ìë not ì´ ê°ì¥ ëì ì°ì ìì를 ê°ê³ , or ê° ê°ì¥ ë®ìµëë¤. ê·¸ëì A and not B or C ë (A and (not B)) or C ì ëë±í©ëë¤. ì¬ë ëì²ë¼, ìíë ì¡°í©ì ííí기 ìí´ ê´í¸ë¥¼ ì¬ì©í ì ììµëë¤.
ë
¼ë¦¬ ì°ì°ì and ì or ë ìì ë¨ë½-íë¡(short-circuit) ì°ì°ìì
ëë¤: ì¸ìë¤ì ì¼ìª½ìì ì¤ë¥¸ìª½ì¼ë¡ ê°ì´ 구í´ì§ê³ , ê²°ê³¼ê° ê²°ì ëìë§ì ê° êµ¬í기ë ì¤ë¨ë©ëë¤. ì를 ë¤ì´, A ì C ê° ì°¸ì´ê³ B ê° ê±°ì§ì´ë©´, A and B and C ë ííì C ì ê°ì 구íì§ ììµëë¤. ë
¼ë¦¬ê°ì´ ìë ì¼ë° ê°ì¼ë¡ ì¬ì©ë ë, ë¨ë½-íë¡ ì°ì°ìì ë°í ê°ì ë§ì§ë§ì¼ë¡ ê°ì´ 구í´ì§ ì¸ìì
ëë¤.
ë¹êµì ê²°ê³¼ë ë¤ë¥¸ ë ¼ë¦¬ ííìì 결과를 ë³ìì ëì í ì ììµëë¤. ì를 ë¤ì´,
>>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance'
>>> non_null = string1 or string2 or string3
>>> non_null
'Trondheim'
íì´ì¬ìì, Cìë ë¬ë¦¬, ííì ìììì ëì
ì ë°ë¤ì½ë¼ë¦¬ ì°ì°ì :=를 ì¬ì©íì¬ ëª
ìì ì¼ë¡ ìíí´ì¼ í©ëë¤. C íë¡ê·¸ë¨ìì íí ë§ì£¼ì¹ë ë¶ë¥ì 문ì ë¤ì íí¼íëë¡ í©ëë¤: == 를 ì¬ì©í ííìì = 를 ì
ë ¥íë ì¤ì.
5.8. ìíì¤ì ë¤ë¥¸ íë¤ì ë¹êµí기¶
ìíì¤ ê°ì²´ë¤ì ë³´íµ ê°ì ìíì¤ íì ë¤ë¥¸ ê°ì²´ë¤ê³¼ ë¹êµë ì ììµëë¤. ë¹êµë ì¬ì ì ìì를 ì¬ì©í©ëë¤: 먼ì 첫 ë í목ì ë¹êµí´ì ë¤ë¥´ë©´ ì´ê²ì´ ë¹êµì 결과를 ê²°ì í©ëë¤; ê°ì¼ë©´, ë¤ì ë í목ì ë¹êµíê³ , ì´ë° ìì¼ë¡ ì´ë í ìíì¤ê° ìì§ë ëê¹ì§ ê³ìí©ëë¤. ë§ì½ ë¹êµëë ë í목 ìì²´ê° ê°ì íì ìíì¤ë©´, ì¬ì ì ë¹êµê° ì¬ê·ì ì¼ë¡ ìíë©ëë¤. ë ìíì¤ì 모ë íëª©ì´ ê°ë¤ê³ ë¹êµëë©´, ìíì¤ë¤ì ê°ì ê²ì¼ë¡ ì·¨ê¸ë©ëë¤. í ìíì¤ê° ë¤ë¥¸ íëì 머리 ë¶ë¶ ìë¸ ìíì¤ë©´, ì§§ì ìíì¤ê° ìì ê²ì ëë¤. 문ìì´ì ì¬ì ì ë°°ì´ì ê°ë³ 문ìë¤ì ìì를 ì íëë° ì ëì½ë ì½ë í¬ì¸í¸ ì«ì를 ì¬ì©í©ëë¤. ê°ì íì ìíì¤ë¤ ê°ì ë¹êµì ëª ê°ì§ ìë ì´ë ìµëë¤:
(1, 2, 3) < (1, 2, 4)
[1, 2, 3] < [1, 2, 4]
'ABC' < 'C' < 'Pascal' < 'Python'
(1, 2, 3, 4) < (1, 2, 4)
(1, 2) < (1, 2, -1)
(1, 2, 3) == (1.0, 2.0, 3.0)
(1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)
ìë¡ ë¤ë¥¸ íì ê°ì²´ë¤ì < ë > ë¡ ë¹êµíë ê²ì, ê·¸ ê°ì²´ë¤ì´ ì ì í ë¹êµ ë©ìëë¤ì ê°ê³ ìì ëë§ íë½ëë¤ë ê²ì ì ìíì¸ì. ì를 ë¤ì´, ìë¡ ë¤ë¥¸ ì«ì íë¤ì ê·¸ë¤ì ì«ì ê°ì ë°ë¼ ë¹êµë©ëë¤. ê·¸ëì 0ì 0.0ê³¼ ê°ê³ , ë±ë±. ê·¸ë ì§ ìì¼ë©´, ììì ìì를 ì ê³µíë ëì , ì¸í°í리í°ë TypeError 를 ì¼ì¼íµëë¤.
ê°ì£¼