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

Python ์ˆœํšŒํ•˜๊ธฐ for๋ฌธ์— ๋Œ€ํ•œ ๋ชจ๋“  ๊ฒƒ.

by Wonit 2019. 12. 6.

์ˆœํšŒํ•˜๊ธฐ : for

1. ์ดํ„ฐ๋ ˆ์ดํ„ฐ

 

ํŒŒ์ด์ฌ์—์„œ ์ดํ„ฐ๋ ˆ์ดํ„ฐ๋Š” ์ž์ฃผ ์œ ์šฉํ•˜๊ฒŒ ์“ฐ์ธ๋‹ค.

 

์ดํ„ฐ๋ ˆ์ดํ„ฐ(Iterator)์€ ๋ฐ˜๋ณต๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์„ ๋œปํ•œ๋‹ค.

 

๋‹ค์Œ์€ ์‹œํ€€์Šค๋ฅผ ์ดํ„ฐ๋ ˆ์ดํ„ฐ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ์˜ˆ์ œ์ด๋‹ค.

fruit = ['apple', 'mango', 'banana']
pos = 0
while pos < len(fruit):
    temp_fruit = fruit[pos]
    print(fruit[pos])
    pos += 1

์œ„๋Š” while๋ฌธ์œผ๋กœ ์‹œํ€€์Šค๋ฅผ ์ˆœํšŒํ•˜์˜€๋Š”๋ฐ for์„ ์‚ฌ์šฉํ•˜๋ฉด ๋”์šฑ ์˜ˆ์ˆ ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

>>> fruit = ['apple', 'banana', 'apeach']
>>> for fruit_iter in fruit:
...     print(fruit_iter)
...
apple
banana
apeach

์ด์ฒ˜๋Ÿผ ์ดํ„ฐ๋ ˆ์ดํ„ฐ๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋ ค๋ฉด ์ˆœํšŒ ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด๋ผ๋Š” ์กฐ๊ฑด์ด ๋งŒ์กฑํ•ด์•ผํ•˜๋Š”๋ฐ ์ˆœํšŒ ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด๋ž€

 

  • ๋ฌธ์ž์—ด
  • ๋ฆฌ์ŠคํŠธ
  • ํŠœํ”Œ
  • ๋”•์…”๋„ˆ๋ฆฌ

 

๋ฌธ์ž์—ด

 

ํ•œ ๋ฒˆ์— ํ•œ ๋ฌธ์ž๋ฅผ ์ˆœํšŒํ•œ๋‹ค.

>>> cat = 'cat'
>>> for letters in cat:
...     print(letters)
... 
...
c
a
t

 

๋ฆฌ์ŠคํŠธ

๋ฆฌ์ŠคํŠธ๋Š” ์œ„์— ๋‚˜์˜จ ์˜ˆ์ œ์™€ ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค.

>>> fruit = ['apple', 'banana', 'apeach']
>>> for fruit_iter in fruit:
...     print(fruit_iter)
...
apple
banana
apeach

 

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

  • ํ‚ค๋ฅผ ์ˆœํšŒ : ๊ธฐ๋ณธ ๊ฐ’
accounts = {'jang': 1684, 'kim': 1584, 'hong': 1412}
for value in accounts:
    print(value)

 

  • ๊ฐ’์„ ์ˆœํšŒ : values()ํ•จ์ˆ˜ ์‚ฌ์šฉ
accounts = {'jang': 1684, 'kim': 1584, 'hong': 1412}

for value in accounts.values():
    print(value)

 

ํŠœํ”Œ

  • ํ‚ค์™€ ๊ฐ’ ๋ชจ๋‘ ๋ฐ˜ํ™˜ : items() ํ•จ์ˆ˜ ์‚ฌ์šฉ
accounts = {'jang': 1684, 'kim': 1584, 'hong': 1412}

for value in accounts.items():
    print(value)
>>>
('jang', 1684)
('kim', 1584)
('hong', 1412)

 

  • ํŠœํ”Œ์—์„œ ํ‚ค์™€ ๊ฐ’์„ ๋ณ€์ˆ˜์— ํ• ๋‹น : ํŠœํ”Œ์€ ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์”ฉ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ์Œ์„ ์ด์šฉ
accounts = {'jang': 1684, 'kim': 1584, 'hong': 1412}
for value, contents in accounts.items():
    print(value, ',',contents)
>>>
jang, 1684
kim, 1584
hong, 1412

 

2. break ์ค‘๋‹จํ•˜๊ธฐ์™€ continue

 
for๋ฌธ์˜ break์™€ continue๋Š” while๋ฌธ๊ณผ ๋™์ผํ•˜๊ฒŒ ๋™์ž‘ํ•˜๋ฏ€๋กœ python while๋ฌธ ์ •๋ณต์˜ ๊ฒŒ์‹œ๊ธ€๋กœ ํ™•์ธํ•˜๊ธฐ ๋ฐ”๋ž€๋‹ค.

 

3. break ํ™•์ธํ•˜๊ธฐ : else

while๋ฌธ๊ณผ ๊ฐ™์ด, for๋ฌธ์—์„œ๋„ ๋ชจ๋“  ํ•ญ๋ชฉ์„ ์ˆœํšŒ ํ–ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ถ€๊ฐ€์  else๋ฌธ์„ ์‚ฌ์šฉํ•œ๋‹ค.

 

for ๋ฌธ์—์„œ accounts์— jung๊ฐ€ ์žˆ์œผ๋ฉด ์ถœ๋ ฅํ•˜๊ณ  ์—†์œผ๋ฉด else๋ฌธ์„ ์‹คํ–‰ํ•˜๋Š” ์˜ˆ์ œ
accounts = {'jang': 1684, 'kim': 1584, 'hong': 1412}

for value, contents in accounts.items():
    if value == 'jung':
        print(value, ',',contents)
        break;
else:
    print('jung์€ ์—†์Šต๋‹ˆ๋‹ค.')

 

4. ์—ฌ๋Ÿฌ ์‹œํ€€์Šค๋กœ ์ˆœํšŒํ•˜๊ธฐ : zip()

zip()ํ•จ์ˆ˜๋Š” ์—ฌ๋Ÿฌ ์‹œํ€€์Šค๋ฅผ ๋ณ‘๋ ฌ๋กœ ์ˆœํšŒํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

days = ['Mon', 'Tus', 'Fri']
fruits = ['apple', 'banana', 'peach']
drinks = ['water', 'coffee', 'beer']
desserts = ['ice cream', 'tea', 'herb', 'coke']

for day, fruit, drink, dessert in zip(days, fruits, drinks, desserts):
    print(day, ': days ', drink, ':drinks, ' , fruit, ': fruits', dessert, ': desserts')
>>>
Mon : days  water :drinks,  apple : fruits ice cream : desserts
Tus : days  coffee :drinks,  banana : fruits tea : desserts
Fri : days  beer :drinks,  peach : fruits herb : desserts

์ด๋Ÿฐ ๋ฐฉ์‹์œผ๋กœ zip()ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ์ˆœํšŒํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์—ฌ๋Ÿฌ ์‹œํ€€์Šค์ค‘ ๊ฐ€์žฅ ์งง์€ ์‹œํ€€์Šค๊ฐ€ ์™„๋ฃŒ๋˜๋ฉด zip()์€ ๋ฉˆ์ถ˜๋‹ค๋Š” ์ ์„ ์œ ๋…ํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ๋‹ค๋ฅธ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ™•์žฅํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด coke๋Š” ์ถœ๋ ฅ๋˜์ง€ ์•Š์„ ๊ฒƒ์ด๋‹ค.

 

5. ์ˆซ์ž ์‹œํ€€์Šค ์ƒ์„ฑํ•˜๊ธฐ : range()

range() ํ•จ์ˆ˜๋Š” ๋ฆฌ์ŠคํŠธ๋‚˜ ํŠœํ”Œ๊ฐ™์€ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์ €์žฅํ•˜์ง€ ์•Š๊ณ  ํŠน์ • ๋ฒ•์œ„ ๋‚ด์— ์ˆซ์ž ์ŠคํŠธ๋ฆผ์„ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ ์ด๋Š” ์ˆœํšŒ ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด์ด๋‹ค.

 

range()ํ•จ์ˆ˜๋Š” ์Šฌ๋ผ์ด์Šค์˜ ์‚ฌ์šฉ๋ฒ•๊ณผ ๋น„์Šทํ•˜๋‹ค.

 

>>> for x in range(0, 3):
...     print(x)
...
0
1
2
>>> list(range(0, 3))
[0, 1, 2]

๋Œ“๊ธ€