본문 바로가기

Computer Science/Programming-Python6

Asterisk Asterisk : unpacking a container tuple, dict 등 자료형에 들어가 있는 값을 unpacking 함수의 입력값, zip 등에 유용하게 사용가능 -. tuple을 넘김 123456def asterisk_test(a, *args): print(a, args) print(type(args)) asterisk_test(1, 2, 3, 4, 5, 6) Colored by Color Scriptercs Output : 1 (2, 3, 4, 5, 6) -. dict를 넘길 때는 **를 두번씀12345def asterisk_test(a, **kargs): print(a, kargs) print(type(kargs)) asterisk_test(1, b = 2, c = 3, d = 4, e.. 2019. 5. 6.
Lambda & MapReduce (1) Lambda : 함수 이름 없이, 함수처럼 쓸 수 있는 익명함수 -. 일반적인 함수123def f(x,y): return x + yprint(f(1, 4))cs Output : 5 -. 람다 함수 1234f = lambda x, y: x + y print(f(1, 4)) cs Output : 5 (2) Map Function : Sequence 자료형 각 element에 동일한 function을 적용함 -> map(function_name, list_data) 123456ex = [1, 2, 3, 4, 5]g = lambda x: x*2f = lambda x,y: x + y print(list(map(g, ex)))print(list(map(f, ex, ex)))cs Output : [2, 4, .. 2019. 5. 6.
Enumerate & Zip (1) Enumrate : List의 element를 추출할 때 번호를 붙여서 추출 12for i, v in enumerate(['tic', 'tac', 'toe']): print(i, v)cs Output : 0 tic1 tac2 toe (2) Zip : 두 개의 list의 값을 병렬적으로 추출함 12345alist = ['a1', 'a2', 'a3']blist = ['b1', 'b2', 'b3'] for(a, b) in zip(alist, blist): print(a, b)cs Output : a1 b1a2 b2a3 b3 * list + zip 1234a, b, c = zip((1, 2, 3), (10, 20, 30), (100, 200, 300))print(a, b, c) print([sum(x) .. 2019. 5. 6.
[자료구조3#] Dictionary Key-Value 의 형태를 갖는다. 추가/삭제/변경이 가능하며, 각각의 키는 유일하다.다른 언어에서는 해쉬라 부르기도 한다. 신기하게도 파이썬에서는 LIST / Tuple -> Dic의 변동이 자유롭다. 123456today = { "year" : "2017", "month" : "02", "day" : "07"}print(today)csㅒOutput : {'year': '2017', 'month': '02', 'day': '07'} 12345678lol = [['a', 'b'], ['c', 'd'], ['e', 'f']]print(dict(lol)) lol = [('a', 'b'), ('c', 'd'), ('e', 'f')]print(dict(lol)) lol = ['ab', 'cd', 'ef']pr.. 2019. 5. 6.