Python - index 함수

dobyming·2022년 12월 28일
0
post-thumbnail

index() 함수

  • Syntax : (배열).index(값)
  • Parameter : Any type (string, number, list, etc.). The element to search for
  • time complexity : O(n) time (type의 크기만큼)

Note: The index() method only returns the first occurrence of the value.

W3School site에 나와있는 index() 함수의 주요 특징이다.즉, 배열에 같은 값들이 있을때 최초 발견된 값의 위치를 반환해준다는것을 의미한다.

예를 들면,

fruits = [4, 55, 64, 32, 16, 32]

x = fruits.index(32)

위와 같은 코드에서 '32' 라는 값이 중복되어 fruits 배열에 담겨져 있다.이때 x 를 출력해보면, 32가 최초 등장한 위치 3 을 return한다.

0개의 댓글