Algorithm/Python

[파이썬] - index 함수

say! 2022. 8. 31. 11:42
728x90

# index 함수

리스트, 배열에서 해당 값의 위치를 찾아주는 함수이다.

중복된 값이 있으면 가장 최소 위치(가장 앞) 반환함

위치 순서는 0부터 시작

alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
position = alphabet.index('f')
print(position)
# 5 출력

index(값, 시작, 끝)

alphabet = ['f', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']

position = alphabet.index('f')
print(position)    # 0 출력

position = alphabet.index('f', 2, 7)
print(position)    # 6 출력, 2번째에서 7번째 위치에서 찾음