피곤피곤 함수라이프
디폴트 파라미터인 매개변수 입력요청
def answering(a=42):
print(f'Life is{a}')
메모리 효율 vs 안정성
리스트 관련 함수를 비파괴적으로 쓰고 싶으면 연산자를 써라~
a.extend([8,10])
def function():
return 1 #리턴은 처음 시행한 하나만 작동된다.
return 2
함수는 호출을 해야 실행된다.
def function():
global a
a = "전역변수"
a
b = "전역변수"
재귀함수: 자기자신을 호출하는 함수 파이썬은 1000개가 맥시멈호출이다.
익명함수:lambda함수
(lambda x,y: x+y)(10,20)
전제: 리스트는 연산자 사용이 가능하다.
example = [[1,2,3],[4,[5,6]],7,[8,[9]]]
여기서 재귀함수를 통하여 순회를 할려면, 결과 빈 리스트를 하나 만들어두고
빈 리스트에 예시가 리스트인지 판단후 추가한다.
example = [[1,2,3],[4,[5,6]],7,[8,[9]]]
def unlist(line):
result=[]
for item in line:
if type(item) == list:
result += unlist(item)
else:
result += [item]
return result
print(unlist(example))
여기서 만약에 빈 리스트를 만들어 두지않고 example 자체를 편집한다고 하면?
def unlist(line):
for i in range(len(line)):
example = [[1,2,3],[4,[5,6]],7,[8,[9]]]
(본인 코드 아님)
def unlist(example):
return sum((unlist(i) if type(i) == list else [i] for i in example), [])
sum(iterable, start=0) return iterable의 합 + start값
Leave a comment