[Python]Comprehension 공부
List comprehension
1
ll = [1,2,3,4,5]
1
2
h_1 = [x**2 for x in ll]
h_1
[1, 4, 9, 16, 25]
제어문 추가
1
2
h_2 = [x**2 for x in ll if x % 2 == 0]
h_2
[4, 16]
Dict,Set comprehension
1
s = {1,2,3,4,5}
1
2
s_1 = [x**2 for x in s]
s_1
[1, 4, 9, 16, 25]
1
t = [('a' , 1 ),('b' , 2 )]
1
2
d = {x:y**2 for x,y in t}
d
{'a': 1, 'b': 4}