![]() |
Basic Grammar 2
1 Advanced Python CH3
2 Tuple
불변한 데이터 타입, 변경 삭제 불가
= 4,5,6
tup tup
(4, 5, 6)
= (4,5,6),(7,8)
nested_tup nested_tup
((4, 5, 6), (7, 8))
tuple([4,0,2])
(4, 0, 2)
= tuple('study')
tup tup
('s', 't', 'u', 'd', 'y')
0] tup[
's'
### 잘못된 예시 : 변경 불가
= tuple(['foo', [1,2], True])
tup 2] = False tup[
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[10], line 4
1 ### 잘못된 예시 : 변경 불가
3 tup = tuple(['foo', [1,2], True])
----> 4 tup[2] = False
TypeError: 'tuple' object does not support item assignment
1].append(3)
tup[ tup
('foo', [1, 2, 3], True)
4, None, 'foo') + (6, 0) + ('bar',) (
(4, None, 'foo', 6, 0, 'bar')
'foo','bar') * 4 (
('foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'bar')
3 Unpacking tuples
tuple의 각 요소들을 변수에 할당
= (4,5,6)
tup = tup
a,b,c b
5
= 4,5,(6,7)
tup = tup
a,b,(c,d) a,b,c
(4, 5, 6)
= a
tmp = b
a = tmp
b
=1,2
a, b a
1
= a,b
b,a a
2
b
1
= [(1,2,3), (4,5,6), (7,8,9)]
seq for a,b,c in seq:
print('a={0}, b={1}, c={2}'.format(a,b,c))
a=1, b=2, c=3
a=4, b=5, c=6
a=7, b=8, c=9
= [(4,4,4), (7,7,7), (9,9,9)]
seq2 for z,y,x in seq2:
print("z={0}, y={1}, x={2}".format(z,y,x))
z=4, y=4, x=4
z=7, y=7, x=7
z=9, y=9, x=9
for a in seq:
print(a)
(1, 2, 3)
(4, 5, 6)
(7, 8, 9)
for c in seq2:
print(c)
(4, 4, 4)
(7, 7, 7)
(9, 9, 9)
# * 마크 : * 부터 출력X
= 1,2,3,4,5
values * rest = values
a,b, a,b
(1, 2)
rest
[3, 4, 5]
*_ = values
a,b, _
[3, 4, 5]
= 9,9,0,1,1,5
values2 * hi = values2
z,x, print(z,x)
print(hi)
9 9
[0, 1, 1, 5]
4 Tuple methods
= (1,2,2,2,3,4,2)
a 2) a.count(
4
4.1 Quiz 1
숫자 2가 몇개 있는지 세는 코드를 loop문 이용해 만들기
= 0
count for i in a:
if i ==2:
+=1
count print(count)
4
5 List
= list([2,3,7,None])
a_list = ('foo','bar','baz')
tup = list(tup)
b_list a_list, b_list
([2, 3, 7, None], ['foo', 'bar', 'baz'])
1] = 'peekaboo'
b_list[ b_list
['foo', 'peekaboo', 'baz']
= range(10)
gen
genlist(gen)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
6 Adding & Removing elements
'dwarf')
b_list.append( b_list
['foo', 'peekaboo', 'baz', 'dwarf']
# 자리 지정 추가
1, 'red')
b_list.insert( b_list
['foo', 'red', 'peekaboo', 'baz', 'dwarf']
# pop : 제거
2)
b_list.pop( b_list
['foo', 'red', 'baz', 'dwarf']
# 추가
'foo')
b_list.append( b_list
['foo', 'red', 'baz', 'dwarf', 'foo']
# 제거 : 앞에서부터 순서대로 삭제
'foo')
b_list.remove( b_list
['red', 'baz', 'dwarf', 'foo']
'dwarf' in b_list, 'dwarf' not in b_list
(True, False)
7 Concatenating and combining lists
4, None, 'foo'] + [7,8,(2,3)] [
[4, None, 'foo', 7, 8, (2, 3)]
# extend : 콘캣 함수
= [4, None,'foo']
x 7,8,(2,3)])
x.extend([ x
[4, None, 'foo', 7, 8, (2, 3)]
8 Sorting
# 오름 차순
= [7,2,5,1,3]
a
a.sort() a
[1, 2, 3, 5, 7]
# key : 조건 ex) key=len 길이 순서
# 길이 같으면 원래 순서 유지
= ['saw','small','He','foxes','six']
b =len)
b.sort(key b
['He', 'saw', 'six', 'small', 'foxes']
9 Binary search and maintaining a sorted list
# bisect : 특정 값이 들어갈 위치 탐색
import bisect
= [1,2,2,2,3,4,7]
c 2), bisect.bisect(c,5) bisect.bisect(c,
(4, 6)
# insort : 특정 값이 들어갈 위치에 삽입
6)
bisect.insort(c, c
[1, 2, 2, 2, 3, 4, 6, 7]
10 Slicing
= [7,2,3,7,5,6,0,1]
seq seq
[7, 2, 3, 7, 5, 6, 0, 1]
# 3번째 부터 4번째 전까지 (3번째) 선택 + 변경
3:4] = [6,3]
seq[ seq
[7, 2, 3, 6, 3, 5, 6, 0, 1]
# 5번째 전까지 출력
5] seq[:
[7, 2, 3, 6, 3]
# 3번째 부터 출력
3:] seq[
[6, 3, 5, 6, 0, 1]
# 뒤에서 4개 출력
-4:] seq[
[5, 6, 0, 1]
# 뒤에서 끝 2개 제외 6개 출력
-6:-2] seq[
[6, 3, 5, 6]
# 앞에서부터 간격 2로 출력
2] seq[::
[7, 3, 3, 6, 1]
# 뒤에서부터 간격 1로 출력
-1] seq[::
[1, 0, 6, 5, 3, 6, 3, 2, 7]
10.1 Quiz 2
- 1,0,6,5 출력
seq
[7, 2, 3, 6, 3, 5, 6, 0, 1]
-4:][::-1] seq[
[1, 0, 6, 5]
4:-1] seq[:
[1, 0, 6, 5]
11 do something with value
# enumerate() : 인덱스, 값 순회
= ['foo','bar','baz']
some_list = {}
mapping for i, v in enumerate(some_list):
= i
mapping[v] mapping
{'foo': 0, 'bar': 1, 'baz': 2}
= ['asd', 'zxc','qwe']
s = {}
m for a,b in enumerate(s):
= a
m[b] m
{'asd': 0, 'zxc': 1, 'qwe': 2}
# sorted() : 작은것, 순서대로 정렬
sorted([7,1,2,6,0,3,2]), sorted('horse race')
([0, 1, 2, 2, 3, 6, 7], [' ', 'a', 'c', 'e', 'e', 'h', 'o', 'r', 'r', 's'])
# zip() : 순서쌍으로 묶기
= ['foo','bar','baz']
seq1 = ['one','two','three']
seq2 = zip(seq1, seq2)
zipped list(zipped)
[('foo', 'one'), ('bar', 'two'), ('baz', 'three')]
# 개수 안 맞으면 최소 개수 출력
= [False, True]
seq3 list(zip(seq1, seq2, seq3))
[('foo', 'one', False), ('bar', 'two', True)]
# enumerate() + zip() 활용
for i, (a,b) in enumerate(zip(seq1, seq2)):
print('{0}: {1}, {2}'.format(i,a,b))
0: foo, one
1: bar, two
2: baz, three
for a,(b,c,d) in enumerate(zip(seq1,seq2,seq3)):
print("{0}: {1}, {2}".format(a,b,c,d))
0: foo, one
1: bar, two
# reversed() : 거꾸로
list(reversed(range(10)))
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
12 dict
# key 값 : 'a','b', value 값 : 'some value',[1, 2, 3, 4]
= {}
empty_dict = {'a' : 'some value', 'b' : [1,2,3,4]}
d1 d1
{'a': 'some value', 'b': [1, 2, 3, 4]}
# 딕셔너리에 새로운 key-value 쌍 추가
'an integer'] = 7
d1[ d1
{'a': 'some value', 'b': [1, 2, 3, 4], 'an integer': 7}
# key에 대한 value값 반환
'b'] d1[
[1, 2, 3, 4]
'an integer'] d1[
7
'b' in d1
True
5] = 'some value'
d1[ d1
{'a': 'some value', 'b': [1, 2, 3, 4], 'an integer': 7, 5: 'some value'}
'dummy'] = 'another value'
d1[ d1
{'a': 'some value',
'b': [1, 2, 3, 4],
'an integer': 7,
5: 'some value',
'dummy': 'another value'}
# del : 순서쌍 삭제
del d1[5]
d1
{'a': 'some value',
'b': [1, 2, 3, 4],
'an integer': 7,
'dummy': 'another value'}
# dummy 순서쌍 삭제후, ret에 value값 할당
= d1.pop('dummy')
ret ret
'another value'
d1
{'a': 'some value', 'b': [1, 2, 3, 4], 7: 'an integer'}
# key값, value값 확인
list(d1.keys()), list(d1.values())
(['a', 'b', 'an integer'], ['some value', [1, 2, 3, 4], 7])
'b' : 'foo', 'c' : 12})
d1.update({ d1
{'a': 'some value', 'b': 'foo', 'an integer': 7, 'c': 12}
13 Creating dicts from sequences
# dict + zip
= dict(zip(range(5), reversed(range(5))))
mapping mapping
{0: 4, 1: 3, 2: 2, 3: 1, 4: 0}
14 Default values
= ['apple','bat','bar','atom','book']
words = {}
by_letter for word in words:
= word[0]
letter if letter not in by_letter:
= [word]
by_letter[letter] else:
by_letter[letter].append(word) by_letter
{'a': ['apple', 'atom'], 'b': ['bat', 'bar', 'book']}
15 Valid dict key types
# 해시값 반환
hash('string'), hash((1,2,(2,3)))
(-5910375871019062728, -9209053662355515447)
# list형식은 변경 가능한 객체이므로 해시값 X
hash((1,2,[2,3]))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[178], line 3
1 # list형식은 변경 가능한 객체이므로 해시값 X
----> 3 hash((1,2,[2,3]))
TypeError: unhashable type: 'list'
= {}
d tuple([1,2,3])] = 5
d[ d
{(1, 2, 3): 5}
16 set
# 중복값 없이 나열
set([2,2,2,1,3,3])
{1, 2, 3}
# 중괄호로 가능
2,2,2,1,3,3} {
{1, 2, 3}
11,22,11,22} {
{11, 22}
= {1,2,3,4,5}
a = {3,4,5,6,7,8} b
# union(), | : 합집합
| b a.union(b), a
({1, 2, 3, 4, 5, 6, 7, 8}, {1, 2, 3, 4, 5, 6, 7, 8})
# intersection, & : 교집합
& b a.intersection(b), a
({3, 4, 5}, {3, 4, 5})
= a.copy()
c |= b
c c
{1, 2, 3, 4, 5, 6, 7, 8}
= a.copy()
d &= b # d에 값 다시 할당
d d
{3, 4, 5}
= [1,2,3,4]
my_data = {tuple(my_data)}
my_set my_set
{(1, 2, 3, 4)}
# 부분집합, 상위집합
= {1,2,3,4,5}
a_set 1,2,3}.issubset(a_set), a_set.issuperset({1,2,3}) {
(True, True)
1,2,3} == {3,2,1} {
True
17 List, Set, Dict Comprehensions
= ['a','as','bat','car','dove','python']
strings for x in strings if len(x) >2] [x.upper()
['BAT', 'CAR', 'DOVE', 'PYTHON']
= {len(x) for x in strings}
unique_lengths unique_lengths
{1, 2, 3, 4, 6}
set(map(len, strings))
{1, 2, 3, 4, 6}
17.1 Quiz 3
- 각 객체가 몇번째에 있는지 출력
= {val : index for index, val in enumerate(strings)}
loc_mapping loc_mapping
{'a': 0, 'as': 1, 'bat': 2, 'car': 3, 'dove': 4, 'python': 5}
### 루프문 써서 만들기
= {}
loc_mapping for index, val in enumerate(strings):
= index
loc_mapping[val]
loc_mapping
{'a': 0, 'as': 1, 'bat': 2, 'car': 3, 'dove': 4, 'python': 5}
17.2 Quiz 4
def multiply_by_two(x):
return x * 2
= [1,2,3,4,5]
my_list = list(map(multiply_by_two, my_list))
result
print(result)
[2, 4, 6, 8, 10]
### lambda로 간결히 사용
= [1,2,3,4,5]
my_list = list(map(lambda x: x*2, my_list))
result print(result)
[2, 4, 6, 8, 10]
= list(map(lambda x: x*3,a))
result2 result2
[3, 6, 9, 12, 15]
18 Nested list comprehensions
= [['John','Emily','Michael','Mary','Steven'],
all_data 'Maria','Juan','Javier','Natalia','Pilar']] [
### e가 한개 이상인것 추출
= []
names_of_interest
for names in all_data:
= [name for name in names if name .count('e')>=1]
enough_es
names_of_interest.extend(enough_es)
names_of_interest
['Michael', 'Steven', 'Javier']
= [name for names in all_data for name in names
result if name.count('e') >= 2]
result
['Steven']
= [(1,2,3),(4,5,6),(7,8,9)]
some_tuples = [x for tup in some_tuples for x in tup]
flattened flattened
[1, 2, 3, 4, 5, 6, 7, 8, 9]
= ['사과','바나나','수박','딸기']
fruits for i in range(len(fruits)):
print(i,fruits[i])
0 사과
1 바나나
2 수박
3 딸기
for i, fruit in enumerate(fruits):
print(i,fruit)
0 사과
1 바나나
2 수박
3 딸기
for x in tup] for tup in some_tuples] [[x
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
19 Functions
= None
a
def bind_a_variable():
global a
= []
a
bind_a_variable()
print(a)
[]
func()
def func():
= []
a for i in range(5):
a.append(i)
a
[]
= []
a def func():
for i in range(5):
a.append(i)
a
[]
def f():
=5
a=6
b=7
creturn {'a' : a, 'b' : b, 'c' : c}
20 Functions are object
= [' Alabama ','Georgia!','Georgia','georgia','FlOrIda',
states 'sounth carolina##', 'West virginia?']
states
[' Alabama ',
'Georgia!',
'Georgia',
'georgia',
'FlOrIda',
'sounth carolina##',
'West virginia?']
import re
def clean_strings(strings):
= []
result for value in strings:
= value.strip() # 앞뒤 공백 제거
value = re.sub('[!#?]','',value) # !,#,? → 공백 대체
value = value.title() # 문자열을 제목 형태로 (첫 대문자, 나머지 소문자)
value # 정리된 문자열 value를 result 리스트에 추가
result.append(value) return result
clean_strings(states)
['Alabama',
'Georgia',
'Georgia',
'Georgia',
'Florida',
'Sounth Carolina',
'West Virginia']
def remove_punctuation(value):
return re.sub('[!#?]','',value)
= [str.strip, remove_punctuation, str.title]
clean_ops
def clean_strings(strings, ops):
= []
result for value in strings:
for function in ops:
= function(value)
value
result.append(value)return result
clean_strings(states, clean_ops)
['Alabama',
'Georgia',
'Georgia',
'Georgia',
'Florida',
'Sounth Carolina',
'West Virginia']
for x in map(remove_punctuation, states):
print(x)
Alabama
Georgia
Georgia
georgia
FlOrIda
sounth carolina
West virginia
### 첫 대문자, 나머지 소문자
'good man'.capitalize()
'Good man'
### 각 단어 첫 대문자
'good man'.title()
'Good Man'
21 Anonymous (Lambda) Functions
def apply_to_list(some_list, f):
return [f(x) for x in some_list]
= [4,0,1,5,6]
ints
lambda x: x*2) apply_to_list(ints,
[8, 0, 2, 10, 12]
= ['foo','card','bar','aaaa','abab']
strings strings
['foo', 'card', 'bar', 'aaaa', 'abab']
=lambda x: len(set(x)))
strings.sort(key strings
['aaaa', 'foo', 'abab', 'bar', 'card']
list(map(lambda x: x+10, [1,2,3,4,5,6]))
[11, 12, 13, 14, 15, 16]
22 Generators
= {'a':1, 'b':2, 'c':3}
some_dict for key in some_dict:
print(key)
a
b
c
= iter(some_dict)
dict_iterator dict_iterator
<dict_keyiterator at 0x22ea41bfe50>
list(dict_iterator)
['a', 'b', 'c']
def squares(n=10):
print('Generating squares from 1 to {0}'.format(n**2))
for i in range(1, n+1):
yield i**2
= squares()
gen gen
<generator object squares at 0x0000022EA41D5AC0>
for x in gen:
print(x, end='')
Generating squares from 1 to 100
149162536496481100
23 Generator expressions
= (x **2 for x in range(100))
gen gen
<generator object <genexpr> at 0x0000022EA41D5EB0>
sum(x**2 for x in range(100))
dict((i, i**2) for i in range(5))
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
24 itertools module
import itertools
= lambda x: x[0]
first_letter = ['Alan','Adam','Wes','Will','Albert','Steven']
names for letter, names in itertools.groupby(names, first_letter):
print(letter, list(names))
A ['Alan', 'Adam']
W ['Wes', 'Will']
A ['Albert']
S ['Steven']
25 Errors & Exception Handling
float('1.2345')
1.2345
float('something')
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[125], line 1
----> 1 float('something')
ValueError: could not convert string to float: 'something'
def attempt_float(x):
try:
return float(x)
except:
return x
'1.2345') attempt_float(
1.2345
'something') attempt_float(
'something'
float((1,2))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[129], line 1
----> 1 float((1,2))
TypeError: float() argument must be a string or a number, not 'tuple'
def attempt_float(x):
try:
return float(x)
except (TypeError, ValueError):
return x
'1.2345') attempt_float(
1.2345