[ Python ] | enumerate 사용법
1. 정의
- get a counter in a loop
- display item counts
- Use enumerate() with conditional statements
- Unpack values returned by enumerate()
String처럼 변경불가능(immutable)한 객체를 변경가능(mutable)한 객체로 변경
index를 함께 저장한다
- 변경불가능(immutable) : Int, String, Tuple
- 변경가능(mutable) : List, Dictionary, Set
2. 선언
enumerate(iterable, start=0)
list = ["eat", "pray", "love"]
for i in enumerate(list):
print(i)
#(0, 'eat')
#(1, 'pray')
#(2, 'love')
for count, i in enumerate(list, 100):
print(count,i)
#100 eat
#101 pray
#102 love
start에 인자를 지정하면 넣어준 인자부터 인덱스가 시작된다
3. 기본 매소드
4. 활용
- list s의 원소 갯수만큼 i번 반복 (option : start 시작 인덱스 지정)
for i, x in enumerate(s, start=1): #8개의 set에
x.add(int(str(N) * i)) #각 횟수만큼의 N을 넣음(연속으로 이어붙인 수)
https://eunsera.tistory.com/24
[프로그래머스] [Python] Level3_N으로 표현
https://programmers.co.kr/learn/courses/30/lessons/42895 코딩테스트 연습 - N으로 표현 programmers.co.kr 동적계획법 Dynamic Programming 문제이다. 경우에 따라서 검사하는 range를 동적으로 설정해주기에..
eunsera.tistory.com
참고 : https://realpython.com/python-enumerate/
Python enumerate(): Simplify Looping With Counters – Real Python
Once you learn about for loops in Python, you know that using an index to access items in a sequence isn't very Pythonic. So what do you do when you need that index value? In this tutorial, you'll learn all about Python's built-in enumerate(), where it's u
realpython.com