파이썬 Study/라이브러리
list - instance
코르시카
2021. 3. 11. 21:55
출처:
https://rfriend.tistory.com/330
[Python] 리스트 내장 함수 및 메소드 (Python List Built-in functions and methods)
지난번 포스팅에서는 파이썬의 자료형 중에서 리스트(Python List)의 생성 및 기본 사용법에 대해서 알아보았습니다. 이번 포스팅에서는 이어서 파이썬 리스트의 내장 함수와 메소드(Python List built-
rfriend.tistory.com
■ == compare
1) 정의 : 리스트 안의 요소를 순차 비교해준다
# cmp() has been removed in py3.x.
>>> list1 = [1, 2, 3]
>>> list2 = ['a', 'b', 'c', 'd']
>>> list3 = [1, 2, 3]
>>> list1 == list2
False
>>> list1 == list3
True
■ list.index(obj)
1) 정의 : 리스트에서 obj 요소 값이 있는 가장 작은 index 값 반환
# (2-4) list.index(obj) : Returns the lowest index in list that obj appears
>>> list4 = [1, 2, 'a', 'b', 'c', 'a']
>>> list4.index('a')
2
>>> list4.index('c')
4
■ list.insert(index, obj)
1) 정의 : 해당 index에 obj를 삽입
■ list.pop(obj=list[-1])
1) 정의 : 기존 리스트에서 마지막 요소를 제거하고, 제거된 마지막 요소를 반환
→ 괄호 안에 정수 / target 원소를 넣으면 됨
정수 선택시 해당 index를 없앤다( collections - dequeue pop left 와 유사함 )
# (2-6) list.pop(obj=list[-1]) : Removes and returns last object or obj from the list
>>> list6 = [1, 2, 'a', 'b', 'c']
>>> list6.pop() # removes the last element
'c'
>>> list6
[1, 2, 'a', 'b']
■ list.reverse()
1) 정의 : 리스의 객체를 리스트 안에서 순서를 역순으로 재배열 해줌, return 없이 inplace 방식으로
# (2-8) list.reverse() : Reverses objects of list in place
>>> list8 = [1, 2, 'a', 'b', 'c']
>>> list8.reverse()
>>>
>>> list8
['c', 'b', 'a', 2, 1]
반응형