본문 바로가기
Computer & Program/python

[python] List

by TDRemon 2024. 1. 16.
반응형

안녕하세요. TDR입니다.

오늘은 python에서의 list(배열) 자료구조의 기본적인 것에 대해 정리해 보겠습니다.

## 생성
list_object = []
list_object = list()
list_object = [1, 2, 3, 4]

## 삽입
# list 마지막에 value 추가
list_object.append(value)
# index 위치에 value 추가
list_object.insert(index, value)
# list_object += [5, 6]과 동일
list_object.extend([5, 6]


## 읽기
# 3을 읽음
re = list_object[2]


## 수정
# list_object = [1, 2, 5, 4]가 됨
list_object[2] = 5


## 삭제
# index 위치 삭제
del list_object[index]
# index 위치부터 마지막까지 삭제
# del list_object[index:len(list_object) - 1]과 동일
del list_object[index:]
# list_object에서 value값 삭제
# value가 여러개이면 첫번째 값 삭제
list_object.remove(value)
# index 위치의 값을 꺼내고 삭제
re = list_object.pop(index)
# list_object의 마지막 값 삭제
re = list_object.pop()


## 특징
# list_object[-1]과 같이 마이너스 인덱스 사용 가능
# e.g.) list_object[-1]  ==  list_object[len(list_object) - 1]
# 여러가지 형태의 값을 넣을 수 있음
# e.g.) list_object = [1, "name", ["car", 5]]
# list안의 값들에 대해 수정, 삭제, 추가 가능

# list_object에서 value가 몇 개있는지 개수
count = list_object.count(value)
# value 값의 index 출력
index = list_object.index(value)
# list_object의 값 순서 바꾸기
list_object.reverse()
# 오름차순 정렬
list_object.sort()
# 내림차순 정렬
list_object.sort(reverse = True)
# 오름차순 정렬 후 새로운 배열로 할당. list_object는 변하지 않음
result_list_object = sorted(list_object)
# list_object의 모든 값 삭제
list_object.clear()

 

반응형

'Computer & Program > python' 카테고리의 다른 글

[python] Dictionary  (0) 2024.01.24
[python] Copy (Shallow vs Deep)  (0) 2024.01.19
[python] Set - 02  (0) 2024.01.19
[python] Set - 01  (0) 2024.01.19
[python] Tuple  (0) 2024.01.16

댓글