본문 바로가기

old/Programming

파이썬의 자료구조-리스트 List

반응형

리스트 List

정의

  • 파이썬의 자료구조 중 하나
  • 다른 언어의 어레이array와 매우 비슷함.

파이썬 자료구조의 종류

  1. 리스트 List
  2. 튜플 Tuple
  3. 딕션어리 Dictionary
  4. 셋 set

선언


temp=[]
temp=list()
temp=[5,9,44]

인덱싱

특징

list안에 list 넣기 가능 - 이 경우 이는 다른 언어의 2D array와 같음
string 역시 기본적으로 array이므로, 똑같은 현상이 발생

슬라이싱

[시작 인덱스 : 마지막 인덱스-1 : 조건 인덱스]

  • [0:8:3] = index 0 에서 7 까지 3을 더해가며 선택하라
  • [:8:] = index 0 에서 7 까지 1을 더해가며 선택하라
  • [7::] = index 7 에서 -1 까지 1을 더해가며 선택하라
  • [::-1] = index 0 에서 7 까지 -1을 더해가며 선택하라

a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(a[0:8:3])  # [0, 3, 6]
print(a[:8:])  # [0, 1, 2, 3, 4, 5, 6, 7]
print(a[7::])  # [7, 8, 9]
print(a[::-1])  # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

사칙연산


a=[1,2,3]
b=["apple","house","computer"]

print(a+b) # [1, 2, 3, 'apple', 'house', 'computer']
# print(a+3) # error
# print(a-b)  # error
# print(a-3) # error
# print(a*b) # error
print(b*3) # ['apple', 'house', 'computer', 'apple', 'house', 'computer', 'apple', 'house', 'computer']
print(b*0) # []
# print(a/b) # error
# print(a/3) # error

새로운 요소 추가

  • append() = 맨 뒤에 하나의 요소를 추가합니다.
  • extend() = 맨 뒤에 여러 개의 요소를 추가합니다.
  • insert() = 원하는 인덱스에 하나의 요소 혹은 리스트를 추가합니다.
  • [] = 원하는 인덱스에 여러 개의 요소를 추가합니다.

a=[1,2,3,4,5,6,7,8,9]
b=["apple","house","computer"]


a.append("test")
print(a) #[1, 2, 3, 4, 5, 6, 7, 8, 9, 'test']
a=[1,2,3,4,5,6,7,8,9]
a.append(b)
print(a) #[1, 2, 3, 4, 5, 6, 7, 8, 9, ['apple', 'house', 'computer']]
a=[1,2,3,4,5,6,7,8,9]
a.extend("test")
print(a) #[1, 2, 3, 4, 5, 6, 7, 8, 9, 't', 'e', 's', 't']
a=[1,2,3,4,5,6,7,8,9]
a.extend(b)
print(a) #[1, 2, 3, 4, 5, 6, 7, 8, 9, 'apple', 'house', 'computer']
a=[1,2,3,4,5,6,7,8,9]
a.insert(2,"test")
print(a) #[1, 2, 'test', 3, 4, 5, 6, 7, 8, 9]
a=[1,2,3,4,5,6,7,8,9]
a.insert(2,b)
print(a) #[1, 2, ['apple', 'house', 'computer'], 3, 4, 5, 6, 7, 8, 9]
a=[1,2,3,4,5,6,7,8,9]
a[3:]=b
print(a) #[1, 2, 3, 'apple', 'house', 'computer']
a=[1,2,3,4,5,6,7,8,9]
a[:4]=b
print(a) #['apple', 'house', 'computer', 5, 6, 7, 8, 9]
a=[1,2,3,4,5,6,7,8,9]
a[6:6]=b
print(a) #[1, 2, 3, 4, 5, 6, 'apple', 'house', 'computer', 7, 8, 9]
a=[1,2,3,4,5,6,7,8,9]
a[6:7]=b
print(a) #[1, 2, 3, 4, 5, 6, 'apple', 'house', 'computer', 8, 9]

요소 제거

  • remove() = 값에 따라 제거합니다.
  • del = 인덱스에 따라 요소 혹은 리스트를 제거합니다.
  • pop() = 인덱스 혹은 가장 마지막 요소를 제거하고 그 값을 리턴합니다.

a=[1,2,3,4,5,6,7,8,9]
a.remove(1) # delete value 1
print(a) # [2, 3, 4, 5, 6, 7, 8, 9]
a=[1,2,3,4,5,6,7,8,9]
# a.remove(98) # error
# print(a)
a=[1,2,3,4,5,6,7,8,9]
del a[3] # delete index 3
print(a) # [1, 2, 3, 5, 6, 7, 8, 9]
a=[1,2,3,4,5,6,7,8,9]
del a[3:7] # delete index 3 to 6
print(a) # [1, 2, 3, 8, 9]
a=[1,2,3,4,5,6,7,8,9]
temp=a.pop() # return and delete the last element of the list
print(a) # [1, 2, 3, 4, 5, 6, 7, 8]
print(temp) # 9
a=[1,2,3,4,5,6,7,8,9]
temp=a.pop(1) # return and delete the index 1
print(a) # [1, 3, 4, 5, 6, 7, 8, 9]
print(temp) # 2

 

반응형

'old > Programming' 카테고리의 다른 글

퀵정렬 Quick Sort  (0) 2021.09.22
삽입 정렬 Insertion Sort  (0) 2021.09.16
선택정렬 Selection Sort  (0) 2021.09.11
버블정렬 Bubble Sort  (0) 2021.09.10
[Java코드]알파벳 오더로 정렬하기  (0) 2021.09.09