Python에서 "세 개의 점" ... 연산자의 의미
Python에서 "세 개의 점" ... 연산자의 의미
슬라이싱에서의 ... 연산자
슬라이싱은 리스트, 문자열, 튜플과 같은 시퀀스 자료형의 일부분을 추출하는 데 사용됩니다. ... 연산자는 슬라이싱에서 다음과 같은 역할을 합니다.
- 시퀀스의 끝까지 추출:
list_data = [1, 2, 3, 4, 5]
result = list_data[2:]
# 결과: [3, 4, 5]
위 코드에서 list_data[2:]
는 2번째 인덱스부터 끝까지의 모든 요소를 추출합니다.
- 시퀀스의 일부분을 추출:
list_data = [1, 2, 3, 4, 5]
result = list_data[1:-1]
# 결과: [2, 3, 4]
인덱싱은 시퀀스의 특정 위치에 있는 요소를 추출하는 데 사용됩니다. ... 연산자는 인덱싱에서 다음과 같은 역할을 합니다.
- 모든 요소 추출:
list_data = [1, 2, 3, 4, 5]
result = list_data[:]
# 결과: [1, 2, 3, 4, 5]
- 특정 요소를 제외한 모든 요소 추출:
list_data = [1, 2, 3, 4, 5]
result = list_data[:2] + list_data[3:]
# 결과: [1, 2, 4, 5]
위 코드에서 list_data[:2] + list_data[3:]
는 2번째 인덱스에 있는 요소를 제외한 모든 요소를 추출합니다.
numpy와 ... 연산자
numpy
라이브러리에서 ... 연산자는 다음과 같은 용도로 사용됩니다.
- 배열의 특정 축에 대한 모든 요소 추출:
import numpy as np
array_data = np.array([[1, 2, 3], [4, 5, 6]])
result = array_data[:, ...]
# 결과: array([[1, 2, 3],
# [4, 5, 6]])
위 코드에서 array_data[:, ...]
는 배열의 모든 행과 모든 열을 추출합니다.
import numpy as np
array_data = np.array([[1, 2, 3], [4, 5, 6]])
result = array_data[:, 1:]
# 결과: array([[2, 3],
# [5, 6]])
iterator와 ... 연산자
... 연산자는 iterator
객체에서 다음과 같은 역할을 합니다.
- 이터레이터의 모든 요소를 추출:
def my_iterator():
for i in range(5):
yield i
iterator_data = my_iterator()
result = list(iterator_data)
# 결과: [0, 1, 2, 3, 4]
위 코드에서 list(iterator_data)
는 이터레이터의 모든 요소를 리스트로 변환합니다.
예제 코드
# 1. 슬라이싱
list_data = [1, 2, 3, 4, 5]
# 리스트의 끝까지 추출
result = list_data[2:]
print(result) # [3, 4, 5]
# 리스트의 일부분을 추출
result = list_data[1:-1]
print(result) # [2, 3, 4]
# 2. 인덱싱
list_data = [1, 2, 3, 4, 5]
# 모든 요소 추출
result = list_data[:]
print(result) # [1, 2, 3, 4, 5]
# 특정 요소를 제외한 모든 요소 추출
result = list_data[:2] + list_data[3:]
print(result) # [1, 2, 4, 5]
# 3. NumPy
import numpy as np
array_data = np.array([[1, 2, 3], [4, 5, 6]])
# 모든 행과 모든 열 추출
result = array_data[:, ...]
print(result) # [[1 2 3]
# [4 5 6]]
# 모든 행과 2번째 열부터 끝까지의 모든 열 추출
result = array_data[:, 1:]
print(result) # [[2 3]
# [5 6]]
# 4. Iterator
def my_iterator():
for i in range(5):
yield i
iterator_data = my_iterator()
# 이터레이터의 모든 요소 추출
result = list(iterator_data)
print(result) # [0, 1, 2, 3, 4]
참고:
- ... 연산자는 슬라이싱과 인덱싱 작업 모두에서 사용할 수 있습니다.
- ... 연산자는
numpy
배열과iterator
객체에서도 사용할 수 있습니다. - ... 연산자는 코드를 간결하게 만들고 가독성을 향상시키는 데 도움이 됩니다.
연산자의 대체 방법
슬라이싱
len()
함수와range()
함수를 사용하여 슬라이스의 시작 및 끝 인덱스를 계산할 수 있습니다.- for 루프를 사용하여 슬라이스의 각 요소를 직접 추출할 수 있습니다.
예시:
# len() 함수와 range() 함수 사용
list_data = [1, 2, 3, 4, 5]
result = list_data[1:4]
# 동일한 결과를 얻는 코드
start_index = 1
end_index = 4
result = []
for i in range(start_index, end_index):
result.append(list_data[i])
print(result) # [2, 3, 4]
# for 루프 사용
list_data = [1, 2, 3, 4, 5]
result = []
for i in range(len(list_data)):
if i >= 1 and i < 4:
result.append(list_data[i])
print(result) # [2, 3, 4]
인덱싱
list.copy()
메서드를 사용하여 리스트의 복사본을 만들고 원하는 요소를 제거할 수 있습니다.- for 루프를 사용하여 리스트의 각 요소를 검사하고 원하는 요소를 추출할 수 있습니다.
# list.copy() 메서드 사용
list_data = [1, 2, 3, 4, 5]
result = list_data.copy()
result.remove(3)
# 동일한 결과를 얻는 코드
result = []
for i in list_data:
if i != 3:
result.append(i)
print(result) # [1, 2, 4, 5]
# for 루프 사용
list_data = [1, 2, 3, 4, 5]
result = []
for i in list_data:
if i not in [3]:
result.append(i)
print(result) # [1, 2, 4, 5]
NumPy
np.arange()
함수를 사용하여 특정 범위의 배열을 만들 수 있습니다.
import numpy as np
# np.arange() 함수 사용
array_data = np.array([[1, 2, 3], [4, 5, 6]])
result = array_data[:, 1:]
# 동일한 결과를 얻는 코드
result = np.arange(array_data.shape[0])[:, np.newaxis] + 1
for i in range(array_data.shape[0]):
for j in range(1, array_data.shape[1]):
result[i, j] = array_data[i, j]
print(result) # [[2 3]
# [5 6]]
# for 루프 사용
import numpy as np
array_data = np.array([[1, 2, 3], [4, 5, 6]])
result = np.empty((array_data.shape[0], array_data.shape[1] - 1))
for i in range(array_data.shape[0]):
for j in range(1, array_data.shape[1]):
result[i, j - 1] = array_data[i, j]
print(result) # [[2 3]
# [5 6]]
Iterator
list()
함수를 사용하여 이터레이터를 리스트로 변환할 수 있습니다.
def my_iterator():
for i in range(5):
yield i
iterator_data = my_iterator()
# list() 함수 사용
result = list(iterator_data)
# 동일한 결과를 얻
python numpy iterator