#입력이 간단하면 잘 작동한다.
def sort_priority(values, group):
def helper(x):
if x in group:
print((0,x))
return (0, x)
print((1,x))
return (1, x)
values.sort(key=helper)
#helper2=[(1, 8), (0, 3), (1, 1), (0, 2), (0, 5), (1, 4), (0, 7), (1, 6)]
#numbers.sort(key=helper2)
numbers = [8, 3, 1, 2, 5, 4, 7, 6]
group = {2, 3, 5, 7}
sort_priority(numbers, group)
print(numbers)
(1, 8)
(0, 3)
(1, 1)
(0, 2)
(0, 5)
(1, 4)
(0, 7)
(1, 6)
[2, 3, 5, 7, 1, 4, 6, 8]
numbers = [8, 3, 1, 2, 5, 4, 7, 6]
group = [2, 3, 5, 7]
numbers.sort()
group.sort()
numbers= group+numbers
print(numbers)
[2, 3, 5, 7, 1, 2, 3, 4, 5, 6, 7, 8]
def helper(x):
if x in group:
return (0, x)
return (1, x)
File "", line 2
return (0, x)
^
SyntaxError: 'return' outside function
numbers = [8, 3, 1, 2, 5, 4, 7, 6]
group = {2, 3, 5, 7}
helper2=[(1, 8), (0, 3), (1, 1), (0, 2), (0, 5), (1, 4), (0, 7), (1, 6)]
print(helper2[0])
print(numbers)
numbers.sort(key=helper2)
for i in range(8):
numbers.sort(key=helper2[i])
print(numbers)
(1, 8)
[8, 3, 1, 2, 5, 4, 7, 6]
TypeError Traceback (most recent call last)
in
4 print(helper2[0])
5 print(numbers)
----> 6 numbers.sort(key=helper2)
7 for i in range(8):
8 numbers.sort(key=helper2[i])
TypeError: 'list' object is not callable
def sort_priority(values):
for i in range(8):
values.sort(key=helper2[i])
sort_priority(numbers)
print(numbers)
TypeError Traceback (most recent call last)
in
2 for i in range(8):
3 values.sort(key=helper2[i])
----> 4 sort_priority(numbers)
5 print(numbers)
in sort_priority(values)
1 def sort_priority(values):
2 for i in range(8):
----> 3 values.sort(key=helper2[i])
4 sort_priority(numbers)
5 print(numbers)
TypeError: 'tuple' object is not callable
helper2=[(1, 8), (0, 3), (1, 1), (0, 2), (0, 5), (1, 4), (0, 7), (1, 6)]
print(helper2)
helper2.sort()
print(helper2)
[(1, 8), (0, 3), (1, 1), (0, 2), (0, 5), (1, 4), (0, 7), (1, 6)][(0, 2), (0, 3), (0, 5), (0, 7), (1, 1), (1, 4), (1, 6), (1, 8)]
def sss(values,group):
def helper3(x):
for x in group:
print(x)
return x
values.sort(key=helper3)
group=[(0, 2), (0, 3), (0, 5), (0, 7), (1, 1), (1, 4), (1, 6), (1, 8)]
numbers = [8, 3, 1, 2, 5, 4, 7, 6]
sss(numbers,group)
print(numbers)
(0, 2)
(0, 2)
(0, 2)
(0, 2)
(0, 2)
(0, 2)
(0, 2)
(0, 2)
[8, 3, 1, 2, 5, 4, 7, 6]
def sort_priority2(numbers, group):
found = False
def helper(x):
if x in group:
found = True # 문제를 쉽게 해결할 수 있을 것 같다
return (0, x)
return (1, x)
numbers.sort(key=helper)
return found
numbers = [8, 3, 1, 2, 5, 4, 7, 8]
group = {2, 3, 5, 7}
found = sort_priority2(numbers, group)
print('발견:', found)
print(numbers)
발견: False
[2, 3, 5, 7, 1, 4, 8, 8]
foo = does_not_exist * 5
NameError Traceback (most recent call last)
in
----> 1 foo = does_not_exist * 5
NameError: name 'does_not_exist' is not defined
#위 예시 증명
def test1():
x=False
def test2():
nonlocal x
x=True
test2()
print(x)
z= test1()
print(z)
False
None
# helper 함수의 클로저 안에서 이 대입문은 helper 영역 안에 새로운 변수를 정의하는 것으로 취급일뿐이다.
def sort_priority2(numbers, group):
found = False # 영역: 'sort_priority2'
def helper(x):
if x in group:
found = True # 영역: 'helper' -- 좋지 않음!
return (0, x)
return (1, x)
numbers.sort(key=helper)
return found
초보 프로그래머 에게는 영역 지정 버그라고 부르기도 한다.
def sort_priority2(numbers, group):
found = False
def helper(x):
nonlocal found # 추가함
if x in group:
found = True
return (0, x)
return (1, x)
numbers.sort(key=helper)
return found
위 예시에서 nonlocal 문은 대입할 데이터가 클로저 밖에 있어서 다른 영역에 속한다는 사실을 알려준다.
사용은 추천하지 않는다.
함수가 매우 길 경우 nonlocal 대입이 이뤄지는 위치 거리가 멀어져서 이해하기 힘들어진다.
nonlocal : 지역변수가 아님을 선언
nonlocal 이 사용된 함수 바로 한단계 바깥쪽에 위치한 변수와 바인딩을 할 수 있다.
#nonlocal의 예시
x = 20 # 전역변수 (global variable)
def f():
x = 40
def g():
nonlocal x
x = 80
g() # 함수 g를 실행하여 nonlocal이 적용되도록 한다.
print(x) # 함수 f에서의 x값이 출력된다.(함수 g에서 nonlocal 의 영향을 받아 변수가 80으로 변경되었다.)
f()
print(x) # 모든 함수 실행이 끝나고, 변수 x를 출력한다.(출력값은 처음값인 20이다)
80
20
class Sorter:
def __init__(self, group):
self.group = group
self.found = False
def __call__(self, x):
if x in self.group:
self.found = True #클로저가 아니기떄문에 여기 True값을 찾음
return (0, x)
return (1, x)
numbers = [8, 3, 1, 2, 5, 4, 7, 6]
group = {2, 3, 5, 7}
sorter = Sorter(group)
numbers.sort(key=sorter)
assert sorter.found is True
True