728x90
시퀀스 데이터 구조
1. 문자열
문자열 조회, 탐색 메서드
- s.find(x, i) : 문자열의 i번째 인덱스 부터 시작해서 x가 처음으로 나오는 위치를 반환한다.
없으면, -1을 반환한다.
i는 입력하지 않아도 되고 0 이 기본값으로 설정된다.
sample = 'alphabet'
find_a = sample.find('a')
print(find_a) # 0
find_next_a = sample.find('a', 3)
print(find_next_a) # 4
find_k = sample.find('k')
print(find_k) # -1
- s.index(x, i) : 문자열의 i번째 인덱스 부터 시작해서 x가 처음으로 나오는 위치를 반환한다.
없으면, 오류가 발생한다.
i는 입력하지 않아도 되고 0 이 기본값으로 설정된다.
sample = 'alphabet'
index_a = sample.index('a')
print(index_a) # 0
index_next_a = sample.index('a', 3)
print(index_next_a) # 4
index_k = sample.index('k')
print(index_k) # ValueError: substring not found
- s.isaplha() : 문자열이 알파벳으로 이루어져 있는지 확인하는 메소드, True or False를 반환한다.
sample = 'alphabet'
print(sample.isaplha()) # True
sample_2 = 'alphabet2'
print(sample.isalpha()) # False
- s.isupper(), s.islower() : 문자열의 대, 소문자 여부를 확인하는 메소드, True or False를 반환한다.
sample_01 = 'CAPITAL'
print(sample_01.isupper()) # True
print(sample_01.islower()) # False
sample_02 = 'word'
print(sample_02.isupper()) # False
print(sample_02.islower()) # True
sample_03 = 'WOrd'
print(sample_03.isupper()) # False
print(sample_03.islower()) # False
- s.count(x, i) : 문자열의 i번 인덱스부터 x가 몇개있는지 찾는 메소드이다. 길이가 2 이상인 문자열의 개수도 찾을 수 있다.
i는 입력하지 않아도 되고 0 이 기본값으로 설정된다.
sample = 'apple'
print(sample.count('p')) # 2
print(sample.count('ppl')) # 1
print(sample.count('no')) # 0
문자열 조작 메서드
일반적으로 문자열 조작 메서드는 기존 문자열을 토대로 새로운 문자열을 만들어내어 새로운 변수로 할당하는 방식을 사용한다.
- s.replace(old, new, count) : 문자열내부의 old라는 문자열을 count 개수만큼 찾아서 new로 바꾼다.
count는 설정하지 않을 수 있으며, 설정하지 않으면 문자열 전체를 순회하여 바꾼다.
sample = 'Good Morning!'
new_sample = sample.replace('Morning', 'Evening')
print(new_sample) # Good Evening!
new_sample_01 = sample.replace('o', 'G', 2)
print(new_sample_01) # GGGd Morning!
- s.strip(x) : 문자열의 앞 뒤에 있는 x라는 문자열을 제거한다.
x를 입력하지 않으면 공백을 제거한다.
sample = ' Hello '
new_sample = sample.strip()
print(new_sample) # Hello
- s.split(x) : 문자열을 x를 기준으로 분리하여 리스트로 반환한다.
x를 입력하지 않으면 띄어쓰기를 기준으로 분리한다.
sample = 'Hello, World'
new_sample = sample.split('r')
print(new_sample) # ['Hello, Wo', 'ld']
new_sample_01 = sample.split()
print(new_sample_01) # ['Hello,', 'World']
- x.join(s) : iterable한 요소를 x를 이용해서 연결하여 문자열로 반환한다.
words = ['Hello', 'World']
sample = ','.join(words)
print(sample) # Hello,World
- 메서드는 붙여서 이어 사용할 수 있다.
sample = 'Hello, Worlp'
new_sample = sample.replace('p', 'd').split(', ')
print(new_sample) # ['Hello', 'World']
2. 리스트
리스트 탐색 및 정렬 메서드
- arr.index(x, start, end) : 리스트의 start인덱스부터 end인덱스 사이에서 가장 왼쪽에 있는 x의 인덱스를 반환한다.
x가 없다면 오류를 반환한다.
arr = [1, 2, 3]
print(arr.index(2)) # 1
print(arr.index(5)) # ValueError: 11 is not in list
- arr.count(x) : 리스트에서 x가 등장하는 횟수를 반환한다.
arr = [1, 2, 2, 3, 4, 5, 5, 5, 5, 5]
print(arr.count(3)) # 1
print(arr.count(4)) # 1
print(arr.count(5)) # 5
- arr.sort() : 원본의 리스트를 오름차순으로 정렬한다.
내림차순으로 정렬하고 싶다면,reverse = True
를 괄호안에 넣어주면 된다.
arr = [5, 3, 1, 2, 4]
arr.sort()
print(arr) # [1, 2, 3, 4, 5]
arr.sort(reverse = True)
print(arr) # [5, 4, 3, 2, 1]
- arr.reverse() : 리스트의 순서를 역순으로 변경한다.
arr = [1, 2, 5, 8, 3, 5]
arr.reverse()
print(arr) # [5, 3, 8, 5, 2, 1]
리스트 요소 변경 메서드
- arr.append(x) : 리스트의 마지막에 x를 추가한다.
arr = [1, 2, 3]
arr.append(4)
print(arr) # [1, 2, 3, 4]
- arr.extend(x) : 리스트의 끝에 x(iterable)의 모든 항목을 추가한다.
arr = [1, 2, 3]
sample = [4, 5, 6]
arr.extend(sample)
print(arr) # [1, 2, 3, 4, 5, 6]
- arr.insert(i, x) : 리스트의 i번 인덱스에 x를 삽입한다.
arr = [1, 2, 3, 4, 5]
arr.index(3, 10)
print(arr) # [1, 2, 3, 10, 4, 5]
- arr.remove(x) : 리스트의 가장 왼쪽에 있는 x를 제거한다.
만약 x가 없다면 오류를 반환한다.
arr = [1, 2, 3, 4, 5, 3]
arr.remove(3)
print(arr) # [1, 2, 4, 5, 3]
arr.remove(10) # ValueError: 101 is not in list
- arr.pop(x) : 리스트의 x번 인덱스의 항목을 반환 후 제거한다.
x를 입력하지 않으면 가장 마지막 항목을 반환 후 제거한다.
arr = [1, 2, 2, 3, 4, 5, 6]
sample = arr.pop(1)
print(sample, arr) # 2 [1, 2, 3, 4, 5, 6]
sample2 = arr.pop()
print(sample2, arr) # 6 [1, 2, 3, 4, 5]
- arr.clear() : 리스트의 모든 항목을 삭제한다.
arr = [1, 2, 3, 4, 5, 6, 7, 8]
arr.clear()
print(arr) # []
반응형
'내가 만드는 개발자 교안 > Python' 카테고리의 다른 글
02. 04 파이썬 기초 04 - 자료구조 02 비시퀀스 데이터 구조(세트, 딕셔너리) (0) | 2024.07.27 |
---|---|
02. 02 파이썬 기초 02 - 데이터 타입 (Data Types) (0) | 2024.07.26 |
02. 01 파이썬 기초 01 - 파이썬 기본 구조 (0) | 2024.07.14 |
01. 03 파이썬 IDE 설치하기 02 / VSCode for macOS (0) | 2024.07.10 |