Skip to content

KOBIC 차세대 생명정보 교육 파이썬기초편 3강 #

Find similar titles

5회 업데이트 됨.

Edit
  • 최초 작성자
    yeye
  • 최근 업데이트
    yeye

파이썬 기초편 3강. 파이썬 배열형 자료구조 #

실습코드 #

리스트 (List) #

#!python
# 정수로 구성된 리스트
>>> a = [1, 2, 3, 4, 10]

# 문자열로 구성된 리스트
>>> a = ['apple', 'banana', 'melon']

# 다양한 자료형으로 구성된 리스트
>>> a = [1, 'apple', 0.332, 'banana']

# 리스트를 요소로 가진 리스트
>>> a = [['apple', 'banana', 'melon'], [1, 3.2, 4, 6]]

>>> type(a)
<class 'list'>

리스트 인덱싱 #

#!python
>>> a = ['apple', 'banana', 'melon', 'orange']
>>> a[3]
'orange'
>>> a[0]
'apple'
>>> a[-1]
'orange'
>>> a[-4]
'apple'
# 인덱스로 리스트의 요소 바꾸기
>>> a[0] = 'pineapple'
>>> a
['pineapple', 'banana', 'melon', 'orange']

# 요소 삭제
>>> del a[1]
>>> a
['pineapple', 'melon', 'orange']

리스트 슬라이싱 #

#!python
>>> a = ['apple', 'banana', 'melon', 'orange']
>>> a[:3]
['apple', 'banana', 'melon']
>>> a[:-1]
['apple', 'banana', 'melon']
>>> a[2:4]
['melon', 'orange']
>>> a[2:]
['melon', 'orange']
>>> a[1:3]
['banana', 'melon']
# 슬라이싱으로 리스트의 요소 바꾸기
>>> a[:2] = ['APPLE', 'BANANA']
>>> a
['APPLE', 'BANANA', 'melon', 'orange']

리스트 연산 #

#!python
# 리스트 합치기
>>> a = ['A', 'B', 'C']
>>> b = ['G', 'T']
>>> a + b
['A', 'B', 'C', 'G', 'T']
# 리스트 반복하기
>>> a * 2
['A', 'B', 'C', 'A', 'B', 'C']
>>> (a * 2) + b
['A', 'B', 'C', 'A', 'B', 'C', 'G', 'T']

# 리스트 끼리 '*' 연산은 안됨
>>> a * b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'list'

리스트에 제공하는 다양한 기능 #

#!python
# .append()
>>> a = ['A', 'B', 'C']
>>> a.append('D')
>>> a
['A', 'B', 'C', 'D']

# .insert()
>>> a.insert(0, 'val')
>>> a
['val', 'A', 'B', 'C', 'D']

# .extend()
>>> a.extend(['Good', 'Bad'])
>>> a
['val', 'A', 'B', 'C', 'D', 'Good', 'Bad']

# .index()
>>> a.index('Good')
5
>>> a.index('A')
1

# .remove()
>>> a.remove('val')
>>> a
['A', 'B', 'C', 'D', 'Good', 'Bad']

# .sort()
>>> a.sort()
>>> a
['A', 'B', 'Bad', 'C', 'D', 'Good']

# .max(), .min()
>>> a = [10, 2, 100, 99, 32]
>>> max(a)
100
>>> min(a)
2
# 문자열로 이루어진 튜플은 알파벳 순서로 판단
>>> b = ['B', 'C', 'FS', 'S']
>>> max(b)
'S'
>>> min(b)
'B'

# list()
>>> a = (1, 2, 3)
>>> list(a)
[1, 2, 3]

튜플 (tuple) #

#!python
# 빈 튜플 생성
>>> a = ()
# 정수로 구성된 튜플
>>> a = (1, 2, 3, 4, 10)
# 문자열로 구성된 튜플
>>> a = ('apple', 'banana', 'melon')
# 다양한 자료형으로 구성된 튜플
>>> a = (1, 'apple', 0.332, 'banana')
# 튜플과 리스트를 요소로 가진 튜플
>>> a = (['apple', 'banana', 'melon'], (1, 3.2, 4, 6))
>>> type(a)
<class 'tuple'>

# 요소가 하나인 튜플의 자료형은 튜플이 아님 
>>> a = ('A')
>>> type(a)
<class 'str'>

# 요소가 하나인 튜플을 만들기 위해서는 ','를 붙여야 함 
>>> b = ('A', )
>>> type(b)
<class 'tuple'>

튜플 인덱싱 #

#!python
>>> a = ('apple', 'banana', 'melon', 'orange')
>>> a[3]
'orange'
>>> a[0]
'apple'
>>> a[-1]
'orange'
>>> a[-4]
'apple'
# 요소변경, 삭제는 불가함
>>> a[0] = 'pineapple'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> del a[1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object doesn't support item deletion

튜플 슬라이싱 #

#!python
>>> a = ('apple', 'banana', 'melon', 'orange')
>>> a[:3]
('apple', 'banana', 'melon')
>>> a[:-1]
('apple', 'banana', 'melon')
>>> a[2:4]
('melon', 'orange')
>>> a[2:]
('melon', 'orange')
>>> a[1:3]
('banana', 'melon')
>>> a[:]
('apple', 'banana', 'melon', 'orange')

튜플 연산 #

#!python
# 리스트 합치기
>>> a = ('A', 'B', 'C', 'D')
>>> b = ('G', 'T')
>>> a + b
('A', 'B', 'C', 'D', 'G', 'T')
>>> a + b + a
('A', 'B', 'C', 'D', 'G', 'T', 'A', 'B', 'C', 'D')
# 리스트 반복하기
>>> a * 2
('A', 'B', 'C', 'D', 'A', 'B', 'C', 'D')
>>> (a * 2) + b
('A', 'B', 'C', 'D', 'A', 'B', 'C', 'D', 'G', 'T')
# 리스트 끼리 '*' 연산은 안됨
>>> a * b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'tuple''

튜플에서 제공하는 다양한 기능 #

#!python
# len()
>>> a = ('A', 'B', 'C')
>>> len(a)
3

# max(), min()
>>> a = (10, 2, 100, 99, 32)
>>> max(a)
100
>>> min(a)
2
# 문자열로 이루어진 튜플은 알파벳 순서로 판단
>>> b = ('B', 'C', 'FS', 'S')
>>> max(b)
'S'
>>> min(b)
'B'

# tuple()
>>> a = [1, 2, 3]
>>> tuple(a)
(1, 2, 3)

# .count()
>>> a = (1, 1, 2, 2, 2, 3, 4, 3, 5)
>>> a.count(1)
2

# .index()
>>> a = ('A', 'A', 'B', 'C', 'B')
>>> a.index('B')
2

연습문제 #

1번 #

리스트 x = ['sheep', 'puppy', 'cat', ['dog', 'cow', 'pig'], 1, 3.22] 을 인덱싱, 슬라이싱, 다양한 기능 등을 사용하여 주어진 형태로 만드시오.

  • ['dog', 'cow', 'pig']
  • ['dog', 'cow']
  • 3.22
  • 'ee' (문자열 슬라이싱 이용)
  • [['dog', 'cow', 'pig'], 1, 3.22]
  • 3 (숫자형 형변환 이용)
  • ['sheep', 'puppy', 'cat', ['dog', 'cow', 'piglet'], 1, 3.22]

2번 #

주어진 튜플 x = ('Simple', 'is', [12, 13, 14], 'better', 'than', 'complex.', 100)에서 아래 코드(1)~(6)를 실행했을때 어떤 결과가 나올지 예상하시오.

  • x[:-4]
  • x[3:-1]
  • (x3, x[:4]) + (x[4:])
  • x2
  • x[4][2:4]
  • x2 + ['one', 'two']

연습문제 풀이 #

1번 #

#!python
>>> x[3]
['dog', 'cow', 'pig']
>>> x[3][:2]
['dog', 'cow']
>>> x[-1]
3.22
>>> x[0][2:4]
'ee'
>>> x[-3:]
[['dog', 'cow', 'pig'], 1, 3.22]
>>> int(x[-1])
3
>>> x[3][2] = 'piglet'
>>> x
['sheep', 'puppy', 'cat', ['dog', 'cow', 'piglet'], 1, 3.22]

2번 #

#!python
>>> x[:-4]
('Simple', 'is', [12, 13, 14])
>>> x[3:-1]
('better', 'than', 'complex.')
>>> (x[3], x[:4]) + (x[4:])
('better', ('Simple', 'is', [12, 13, 14], 'better'), 'than', 'complex.', 100)
>>> x[2][2]
14
>>> x[4][2:4]
'an'
>>> x[3]
'better'
>>> x[2] + ['one', 'two']
[12, 13, 14, 'one', 'two']
0.0.1_20240318_1_v95