티스토리 뷰

728x90
for idx, x in enumerate(xs):
    print(idx, x)

https://stackoverflow.com/questions/522563/accessing-the-index-in-for-loops

 

Accessing the index in 'for' loops

How do I access the index in a for loop? xs = [8, 23, 45] for x in xs: print("item #{} = {}".format(index, x)) Desired output: item #1 = 8 item #2 = 23 item #3 = 45

stackoverflow.com

https://codechacha.com/ko/python-for-loop-with-index/

 

Python - for문에서 index 사용하기, 3가지 방법

for 반복문에서 요소와 함께 index를 함께 사용하는 방법을 소개합니다. 아래와 같이 range를 사용하면 for문에서 index 참조가 가능하며 index로 요소에 접근할 수 있습니다. enumerate()를 이용하면 요소

codechacha.com

 

728x90

'공부' 카테고리의 다른 글

[kotlin] `sealed class` vs `enum class`  (0) 2022.08.06
trunk based development  (0) 2022.08.06
[logback-spring.xml] get `application.yaml`  (0) 2022.08.06
[Python] files in dir  (0) 2022.08.05
[HBase] compaction  (0) 2022.08.05
댓글