파이썬3 바이블 - 제8장 연습문제

프리렉 - FREELEC

http://freelec.co.kr/book/catalogue_view.asp?UID=134

이강성저

1.

In [1]:
menu = {'odeng':300, 'sundae':400, 'mandu':500}

menuselection = 'sundae'
menu[menuselection]
Out[1]:
400
In [2]:
menuselection = 'mandu'
menu.get(menuselection, 0)
Out[2]:
500
In [3]:
menuselection = 'steak'
menu.get(menuselection, 0)
Out[3]:
0

2.

사전의 키는 해쉬 가능해야(hashable) 한다. 해쉬 가능하다는 뜻은 객체를 해쉬 메모리 공간에 매핑 시킬 수 있다는 뜻이다. 만일 객체가 변경 가능하다면 (mutable), 그 객체는 해쉬 공간 매핑 주소가 변경될 것이므로 변경 가능한 객체는 기본적으로 해쉬가능하지 않은 것으로 취급한다. 따라서, 변경가능하지 않은 (immutable) 객체이면서 해쉬 가능한 객체들이 사전의 키로 사용될 수 있다. 수치자료형, 문자열, 바이트, 튜플 등은 사전의 키로 사용 가능하며, 리스트, 사전등은 키로 사용가능하지 않다.

3.

In [4]:
L1 = [1,2,3]
L2 = [4,5,6]
d = {'low':L1, 'high':L2}
e = d
f = d.copy()
d['low'] = [10, 20, 30]
d['high'][1] = 500
In [5]:
e
Out[5]:
{'high': [4, 500, 6], 'low': [10, 20, 30]}
In [6]:
f  # d를 복사했음에도 불구하고 d의 변경이 f에 영향을 미치는 점에 유의
Out[6]:
{'high': [4, 500, 6], 'low': [1, 2, 3]}

4.

In [7]:
d = {'one':1, 'two':2, 'three':3, 'four':4, 'five':5}
In [8]:
sorted(d.items())
Out[8]:
[('five', 5), ('four', 4), ('one', 1), ('three', 3), ('two', 2)]
In [9]:
sorted(d.items(), key=lambda a:a[1])
Out[9]:
[('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5)]

5.

In [10]:
keys = ['one', 'two', 'three', 'four']
values = [1, 2, 3, 4]
dict(zip(keys, values))
Out[10]:
{'four': 4, 'one': 1, 'three': 3, 'two': 2}

6.

In [11]:
s = 'one two one two three four'
In [12]:
d = {}
for w in s.split():
    d[w] = None
list(d.keys())
Out[12]:
['three', 'one', 'two', 'four']
In [13]:
# 집합을 이용하면 간단히 해결된다
list(set(s.split()))
Out[13]:
['three', 'one', 'two', 'four']

7.

In [14]:
s = 'We propose to start by making it possible to teach programming in Python, an existing scripting language, and to focus on creating a new development environment and teaching materials for it.'
In [15]:
import re

s2 = re.sub('[,.]', '', s.lower())
s2
Out[15]:
'we propose to start by making it possible to teach programming in python an existing scripting language and to focus on creating a new development environment and teaching materials for it'
In [16]:
ws = s2.split()

import collections
collections.Counter(ws)
Out[16]:
Counter({'to': 3, 'and': 2, 'it': 2, 'development': 1, 'by': 1, 'in': 1, 'materials': 1, 'programming': 1, 'for': 1, 'existing': 1, 'possible': 1, 'we': 1, 'start': 1, 'teach': 1, 'teaching': 1, 'an': 1, 'python': 1, 'creating': 1, 'language': 1, 'environment': 1, 'propose': 1, 'scripting': 1, 'new': 1, 'a': 1, 'focus': 1, 'making': 1, 'on': 1})
In [17]:
sorted(list(collections.Counter(ws).items()))
Out[17]:
[('a', 1),
 ('an', 1),
 ('and', 2),
 ('by', 1),
 ('creating', 1),
 ('development', 1),
 ('environment', 1),
 ('existing', 1),
 ('focus', 1),
 ('for', 1),
 ('in', 1),
 ('it', 2),
 ('language', 1),
 ('making', 1),
 ('materials', 1),
 ('new', 1),
 ('on', 1),
 ('possible', 1),
 ('programming', 1),
 ('propose', 1),
 ('python', 1),
 ('scripting', 1),
 ('start', 1),
 ('teach', 1),
 ('teaching', 1),
 ('to', 3),
 ('we', 1)]

8.

In [18]:
tab = {'a':'w', 'b':'x', 'c':'y', 'd':'z', 'w':'a', 'x':'b', 'y':'c', 'z':'d'}
s = 'abcd wxyz'

s2 = ''
for c in s:
    s2 += tab.get(c, c)
In [19]:
s2
Out[19]:
'wxyz abcd'
In [19]: