๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ์žฅ์›์ต ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ
๐Ÿคท๐Ÿผ‍โ™€๏ธ Etc.../- C, C++, Python, Android

Python ์ปดํ”„๋ฆฌํ—จ์…˜ comprehension

by Wonit 2019. 12. 6.

Comprehension

์ปดํ”„๋ฆฌํ—จ์…˜์€ ๊ฐ„ํŽธํ•œ ๊ตฌ๋ฌธ์œผ๋กœ ๋ฐ˜๋ณต๋ฌธ๊ณผ ์กฐ๊ฑด ํ…Œ์ŠคํŠธ๋ฅผ ๊ฒฐํ•ฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.

์ฆ‰ ์ž๋ฃŒ๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ํšจ๊ณผ์ ์ธ ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค.

์ปดํ”„๋ฆฌํ—จ์…˜์—๋Š” ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜, ๋”•์…”๋„ˆ๋ฆฌ ์ปดํ”„๋ฆฌํ—จ์…˜, ์…‹ ์ปดํ”„๋ฆฌํ—จ์…˜, ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ์ปดํ”„๋ฆฌํ—จ์…˜์ด ์žˆ๋‹ค.

์ปดํ”„๋ฆฌํ—จ์…˜์˜ ํ‘œํ˜„์‹

  • ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜ : [ํ‘œํ˜„์‹ for ํ•ญ๋ชฉ in ์ˆœํšŒ ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด] + if ์กฐ๊ฑด, ๋‹ค์ค‘ for
  • ๋”•์…”๋„ˆ๋ฆฌ ์ปดํ”„๋ฆฌํ—จ์…˜ : {ํ‚ค_ํ‘œํ˜„์‹ : ๊ฐ’_ํ‘œํ˜„์‹ for ํ‘œํ˜„์‹ in ์ˆœํšŒ ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด} + if ์กฐ๊ฑด, ๋‹ค์ค‘ for
  • ์…‹ ์ปดํ”„๋ฆฌํ—จ์…˜ : {ํ‘œํ˜„์‹ for ํ‘œํ˜„์‹ in ์ˆœํšŒ ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด} + if ์กฐ๊ฑด, ๋‹ค์ค‘ for

๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜

๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์ด๋ž€ ๋ฆฌ์ŠคํŠธ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ํšจ๊ณผ์ ์ธ ๋ฐฉ๋ฒ• ์ฏค์ด๋ผ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

๋ฆฌ์ŠคํŠธ์— ๊ฐ’์„ ๋„ฃ๋Š” ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•์ค‘ ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์ž

>>> list_a = []
>>> list_a.append(1)
>>> list_a.append(2)
>>> list_a.append(3)
>>> list_a.append(4)
>>> list_a.append(5)
>>> list_a
[1,2,3,4,5]
์œ„๋Š” append()ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ–ˆ๋Š”๋ฐ ์šฐ๋ฆฌ๋Š” ์ดํ„ฐ๋ ˆ์ด์…˜์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— for๋ฌธ์œผ๋กœ ์ดํ„ฐ๋ ˆ์ด์…˜์„ ํ•ด๋ณด์ž.
num_list = []

for i in range(1,6):
    num_list.append(i)    
print(num_list)
>>>
[1,2,3,4,5]

์ฒซ ๋ฒˆ์งธ ์˜ˆ์ œ ๋ณด๋‹ค ํ›จ์”ฌ ๊ฐ„๊ฒฐํ•ด์ง€๊ณ  ํฐ ์ˆ˜๋ฅผ ์ €์žฅํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ํšจ๊ณผ์ ์ผ๋“ฏ ๋ณด์ด์ง€๋งŒ ์กฐ๊ธˆ ๋” ์˜ˆ์ˆ ์ ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์ปดํ”„๋ฆฌํ—จ์…˜์„ ์ด์šฉํ•ด ๋ณด์ž.

num_list = [number for number in range(1, 6)]
print(num_list)
>>>
[1,2,3,4,5]

์ฒซ ๋ฒˆ์งธ์˜ number์€ ๋ชฉ๋ก์— ๋Œ€ํ•œ ๊ฐ’์„ ์ƒ์„ฑํ•˜๋Š” ๋ณ€์ˆ˜์ด๊ณ  ๋‘ ๋ฒˆ์งธ์˜ number์€ for๋ฌธ์˜ ์ผ๋ถ€์ด๋‹ค.

์ปดํ”„๋ฆฌํ—จ์…˜ ์˜ˆ์ œ

1๋ถ€ํ„ฐ ์ž…๋ ฅ๋ฐ›์€ ์ˆ˜ ๊นŒ์ง€ ํ™€์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์ปดํ”„๋ฆฌํ—จ์…˜
input_num = input('ํ™€์ˆ˜๋ฅผ ๊ตฌ๋ถ„ํ•  ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”_1')

num_proc = [in_num for in_num in range(1, int(input_num)) if in_num % 2 == 1]
print(num_proc)
์ปดํ”„๋ฆฌํ—จ์…˜์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด
input_num2 = input('ํ™€์ˆ˜๋ฅผ ๊ตฌ๋ถ„ํ•  ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”_2')
num_proc2 = []
for in_num2 in range(1, int(input_num2)):
    if in_num2 % 2 == 1:
        num_proc2 = in_num2
        print(num_proc2)

์ค‘์ฒฉ ๋ฃจํ”„์— ์ƒ์‘ํ•˜๋Š” ์ปดํ”„๋ฆฌํ—จ์…˜

์ผ๋ฐ˜์ ์ธ ์ด์ค‘ ๋ฐ˜๋ณต
rows = range(1, 4)
cols = range(1, 3)
for row in rows:
    for col in cols:
        print(row, col)
>>>
1 1
1 2
2 1
2 2
3 1
3 2
์ปดํ”„๋ฆฌํ—จ์…˜์„ ์ด์šฉํ•œ ์ด์ค‘ ๋ฐ˜๋ณต
rows = range(1, 4)
cols = range(1, 3)
cells = [(row, col) for row in rows for col in cols]
for cell in cells:
    print(cell)
>>>
(1, 1)
(1, 2)
(2, 1)
(2, 2)
(3, 1)
(3, 2)

๋”•์…”๋„ˆ๋ฆฌ ์ปดํ”„๋ฆฌํ—จ์…˜

๋”•์…”๋„ˆ๋ฆฌ๋Š”

์…‹ ์ปดํ”„๋ฆฌํ—จ์…˜

์ œ๋„ˆ๋ ˆ์ดํ„ฐ ์ปดํ”„๋ฆฌํ—จ์…˜

์ˆ˜์ •์ค‘...

๋Œ“๊ธ€