TIL27. Python : List Comprehension

ID์งฑ์žฌยท2021๋…„ 10์›” 8์ผ
0

Python

๋ชฉ๋ก ๋ณด๊ธฐ
34/39
post-thumbnail

๐Ÿ“Œ ์ด ํฌ์ŠคํŒ…์—์„œ๋Š” Python์˜ comprehensions ๋ฌธ๋ฒ•์— ๋Œ€ํ•ด์„œ ์ •๋ฆฌํ•˜์˜€์Šต๋‹ˆ๋‹ค.



๐ŸŒˆ List Comprehension

๐Ÿ”ฅ list comprehension์ด ๋ญ˜๊นŒ์š”..?

๐Ÿ”ฅ comprehension์ด ๊ฐ€๋Šฅํ•œ sequence type

๐Ÿ”ฅ Assignments



1. list comprehension์ด ๋ญ˜๊นŒ์š”..?

๐Ÿค” ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์„ ์™œ ์“ธ๊นŒ์š”?

โœ”๏ธ 1๋ถ€ํ„ฐ 10๊นŒ์ง€ ์ •์ˆ˜ ์ค‘ ํ™€์ˆ˜๋งŒ ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ ๊ฐ์ฒด์— ๋‹ด์œผ๋ ค๊ณ  ํ•˜๋ฉด ์ตœ์†Œ 4๊ฐœ์˜ line์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

res = []
for i in range(1,11):
  if i % 2:
    res.append(i)
print(res) # [1, 3, 5, 7, 9]

โœ”๏ธ ์ด๋ฒˆ์—” filter ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์œ„์™€ ๊ฐ™์€ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

res = list(filter(lambda x: x % 2, range(1, 11)))
print(res) # [1, 3, 5, 7, 9]

โœ”๏ธ ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์ด์€ ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ถ•์•ฝํ˜• ํ‘œํ˜„์‹์ด๊ณ , ๊ตฌ์กฐ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.
โœ”๏ธ ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์€ ์ง๊ด€์ ์ด๊ณ  ๊ฐ„๊ฒฐํ•˜๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ์•„์งˆ ์ˆ˜๋ก ์ผ๋ฐ˜์ ์ธ ์ƒํ™ฉ์—์„œ ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์ด ๋” ๋‚˜์€ ํšจ์œจ์„ฑ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค.

res = [i for i in range(1,11) if i % 2]
print(res) # [1, 3, 5, 7, 9]

โœ”๏ธ ๋˜ํ•œ ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์€ if๋ฌธ์„ ์˜ต์…˜์œผ๋กœ ์„ ํƒํ•˜์—ฌ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๊ณ , ์ปดํ”„๋ฆฌํ—จ์…˜ ๋ฌธ๋ฒ•์„ ์ค‘์ฒฉ์‹œํ‚ค๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, ์ปดํ“จ๋ฆฌํ—จ์…˜ ๋‚ด์— ๊นŠ์ด๊ฐ€ 3๋‹จ๊ณ„ ์ด์ƒ ๋Š˜์–ด๋‚˜๋ฉด ๊ฐ€๋…์„ฑ์ด ํ˜„์ €ํžˆ ์ค„์–ด๋“ญ๋‹ˆ๋‹ค.

a = [[i for i in range(10) if i % 2] for j in range(4)]
print(a) # [[1, 3, 5, 7, 9], [1, 3, 5, 7, 9], [1, 3, 5, 7, 9], [1, 3, 5, 7, 9]]

๐Ÿค” ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜๊ณผ ์ผ๋ฐ˜ ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ์ฒด ์ฐธ์กฐ ๋ฐฉ์‹

โœ”๏ธ ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์€ ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜์— appendํ•˜๋Š” ๊ณผ์ •์ด ์ƒ๋žต๋œ ์ถ•์•ฝํ˜•์ด๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ์š”์†Œ๊ฐ€ ๋ชจ๋‘ ๊ฐ™์€ ๊ฐ’์ผ์ง€๋„ ๋‹ค๋ฅธ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.

p1 = [['py']*3 for n in range(3)] 
p2 = [['py']*3]*3
print(p1) # [['py', 'py', 'py'], ['py', 'py', 'py'], ['py', 'py', 'py']]
print(p2) # [['py', 'py', 'py'], ['py', 'py', 'py'], ['py', 'py', 'py']]
print(id(p1[0]), id(p1[1]), id(p1[2])) # 140179978965888 140179978972096 140179978930048
print(id(p2[0]), id(p2[1]), id(p2[2])) # 140180045237888 140180045237888 140180045237888

โœ”๏ธ ์ด์— ๋ฐ์ดํ„ฐ์˜ ์ˆ˜์ •์ด ๋ฐœ์ƒ๋˜์—ˆ์„ ๋•Œ, ๊ฐ ์š”์†Œ๊ฐ€ ์ž‘์šฉ๋˜๋Š” ๋ฐฉ์‹์— ์ฐจ์ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
โœ”๏ธ ์ฆ‰, ๊ฐ ๊ฐ์˜ ์š”์†Œ๊ฐ€ ๋…๋ฆฝ์  ๊ฐ์ฒด๋กœ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ฐธ์กฐํ•ด์„œ ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ๋ฌธ์ œ๋ฅผ ์˜ˆ๋ฐฉํ•˜๊ณ  ์‹ถ์„ ๋•Œ์—๋Š” ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์ด ์•ˆ์ „ํ•ฉ๋‹ˆ๋‹ค.

p1 = [['py']*3 for n in range(3)] 
p2 = [['py']*3]*3
p1[1][1] = 'js'
p2[1][1] = 'js'
print(p1) # [['py', 'py', 'py'], ['py', 'js', 'py'], ['py', 'py', 'py']]
print(p2) # [['py', 'js', 'py'], ['py', 'js', 'py'], ['py', 'js', 'py']]


2. comprehension์ด ๊ฐ€๋Šฅํ•œ sequence type

๐Ÿค” dictํ˜•

โœ”๏ธ dict, set ๋“ฑ์€ sequence type์ด๊ธฐ ๋•Œ๋ฌธ์— comprehension ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
โœ”๏ธ 1๋ถ€ํ„ฐ 10๊นŒ์ง€ ์ˆ˜๋ฅผ key๋กœํ•˜๊ณ  ์ œ๊ณฑํ•œ ์ˆ˜๋ฅผ value๋กœํ•˜๋Š” dict๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

squares = {}
for i in range(1, 11):
    squares[i] = i**2
print(squares) # {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}

โœ”๏ธ dict comprehension์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

squares = {i: i**2 for i in range(1, 11)}
print(squares) # {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}

๐Ÿค” setํ˜•

โœ”๏ธ set๋„ comprehension์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ์ˆœ์„œ๋ฅผ ๋ณด์žฅํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

res = {i*i for i in range(1, 11) if i % 2}
print(res) # {1, 9, 81, 49, 25}


3. Assignments

โœ”๏ธ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋„์‹œ๋ชฉ๋ก์˜ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ฃผ์–ด์กŒ์„๋•Œ, ๋„์‹œ์ด๋ฆ„์ด S๋กœ ์‹œ์ž‘ํ•˜์ง€ ์•Š๋Š” ๋„์‹œ๋งŒ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค ๋•Œ ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ๋ณด์„ธ์š”.

cities = ["Tokyo","Shanghai","Jakarta","Seoul","Guangzhou","Beijing","Karachi","Shenzhen","Delhi"]
def func(l):
  res = [i for i in l if i[0] != 'S']
  return res
print(func(cities)) # ['Tokyo', 'Jakarta', 'Guangzhou', 'Beijing', 'Karachi', 'Delhi']

โœ”๏ธ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋„์‹œ, ์ธ๊ตฌ์ˆ˜๊ฐ€ ํŠœํ”Œ์˜ ๋ฆฌ์ŠคํŠธ๋กœ ์ฃผ์–ด์กŒ์„๋•Œ, ํ‚ค๊ฐ€ ๋„์‹œ, ๊ฐ’์ด ์ธ๊ตฌ์ˆ˜์ธ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ ์ปดํ”„๋ฆฌํ—จ์…˜์„ ์‚ฌ์šฉํ•œ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ๋ณด์„ธ์š”.

population_of_city = [('Tokyo', 36923000), ('Shanghai', 34000000), ('Jakarta', 30000000), ('Seoul', 25514000), ('Guangzhou', 25000000), ('Beijing', 24900000), ('Karachi', 24300000), ('Shenzhen', 23300000), ('Delhi', 21753486)]
def func(l):
  res = {i[0]:i[1] for i in l}
  return res
print(func(population_of_city))
"""
{'Tokyo': 36923000, 'Shanghai': 34000000, 'Jakarta': 30000000,
 'Seoul': 25514000, 'Guangzhou': 25000000, 'Beijing': 24900000, 
 'Karachi': 24300000, 'Shenzhen': 23300000, 'Delhi': 21753486
 } 
"""
profile
Keep Going, Keep Coding!

0๊ฐœ์˜ ๋Œ“๊ธ€