#24-파이썬 기초 실습 - 튜플(Tuple)-2
2020. 2. 4. 08:01ㆍAI & BigData/Python Basics
파이썬 기초 실습 - 튜플(Tuple) - 2¶
튜플¶
- 대부분의 내용이 리스트와 동일하고 사용할 수 있는 내장 메서드만 약간 다르다.
1. index(). 리스트와 비슷하게 찾고자 하는 값이 인덱스에 있는지 알려주는 메서드¶
In [1]:
tuple_data = (1, 2, 3, 4, 5)
tuple_data.index(5) # 5이라는 데이터가 어느 인덱스를 가지고 있는지 보여준다.
Out[1]:
4
2. count(). 동일한 데이터가 몇 개 들어가있는 세어주는 메서드.¶
In [2]:
tuple_data=(1, 2, 3, 4, 5, 5)
print(tuple_data.count(5)) # 5라는 데이터의 갯수를 세어준다.
print(tuple_data.count(6)) # 6이라는 데이터의 갯수를 세어준다.
2
0
3. append. 리스트의 맨 뒤에 데이터를 추가한다.¶
- 중간에 원하는 인덱스로 추가하고 싶으면 (객체)[(인덱스):(인덱스)] 한다.
In [3]:
tuple_data = [1, 2, 3]
tuple_data.append(4) # 뒤에 데이터를 추가한다.
tuple_data # [1,2,3,4]. 뒤에 데이터를 추가됐다.
Out[3]:
[1, 2, 3, 4]
In [4]:
tuple_data.append([4, 5]) # 뒤에 리스트를 추가
tuple_data # [1, 2, 3, 4, [4, 5]]. 리스트로 추가
Out[4]:
[1, 2, 3, 4, [4, 5]]
4. insert. append와 다른 점은 중복된 값은 들어가지는 않는 점,¶
- 최소 2개의 값이어야 한다는 점, int값만 가능하다는 점이다.
In [5]:
tuple_data = [1, 2, 3]
tuple_data.insert(3, 4) # 뒤에 3,4를 추가
tuple_data # [1,2,3,4]. 중복된 값이 들어가지 않았다.
Out[5]:
[1, 2, 3, 4]
In [6]:
# 한 개라서 에러 발생
# TypeError: insert() takes exactly 2 arguments (1 given)
tuple_data.insert(5)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-d40433640a30> in <module>
1 # 한 개라서 에러 발생
2 # TypeError: insert() takes exactly 2 arguments (1 given)
----> 3 tuple_data.insert(5)
TypeError: insert() takes exactly 2 arguments (1 given)
In [7]:
# 리스트 값도 에러 발생
# TypeError: 'list' object cannot be interpreted as an integer
tuple_data.insert([4], [5])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-a67f17ba6efd> in <module>
1 # 리스트 값도 에러 발생
2 # TypeError: 'list' object cannot be interpreted as an integer
----> 3 tuple_data.insert([4], [5])
TypeError: 'list' object cannot be interpreted as an integer
In [8]:
# int값만 추가 가능하다.
# TypeError: 'str' object cannot be interpreted as an integer
tuple_data = ['a', 'b', 'c', 'd']
tuple_data.insert('a', 'b')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-8-60549797ef2d> in <module>
2 # TypeError: 'str' object cannot be interpreted as an integer
3 tuple_data = ['a', 'b', 'c', 'd']
----> 4 tuple_data.insert('a', 'b')
TypeError: 'str' object cannot be interpreted as an integer
In [9]:
# 에러 값이 없이 추가 된것을 확인 할수 있습니다.
tuple_data.insert(1, 'b')
tuple_data
Out[9]:
['a', 'b', 'b', 'c', 'd']
5. count. 동일한 데이터 카운트¶
In [10]:
tuple_data = [1, 2, 3, 3, 3, 8]
print(tuple_data.count(1)) # 1이 한 개 있으므로 1.
print(tuple_data.count(3)) # 3이 두 개 있으므로 3.
print(tuple_data.count(5)) # 5은 없으므로 0
1
3
0
In [11]:
tuple_data = ['a', 'b', 'c', 'd', 'e']
tuple_data.count('a') # 문자도 검색 가능
Out[11]:
1
In [12]:
### 6. reverse. 데이터의 인덱스를 거꾸로 바꿔 준다.
In [13]:
tuple_data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
tuple_data.reverse()
tuple_data # [0, 9, 8, 7, 6, 5, 4, 3, 2, 1]
Out[13]:
[0, 9, 8, 7, 6, 5, 4, 3, 2, 1]
6. sort. 리스트 안의 원소들을 크기순서로 정렬한다.¶
- reverse가 True 내림차순 False면 오름차순. 기본은 False
In [14]:
tuple_data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
tuple_data.sort()
tuple_data # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Out[14]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [15]:
tuple_data.sort(reverse=True)
tuple_data # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
Out[15]:
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
7. remove. 지우고 싶은 데이터를 넣으면 삭제된다.¶
- 중복된 데이터가 있으면 인덱스가 낮은 것만 삭제한다. 즉, 하나의 데이터만 삭제.
In [16]:
tuple_data = [1, 2, 2, 3, 3, 5]
tuple_data.remove(3)
tuple_data # [1, 2, 2, 3, 5]. 3 하나만 지워짐.
Out[16]:
[1, 2, 2, 3, 5]
8. extend. 리스트를 이용해서 데이터를 추가한다.¶
In [17]:
tuple_data = [10, 20, 30, 40, 50]
tuple_data.extend([60,70,80]) # 리스트로 되어 있는 데이터가 분해되어 인덱스에 추가됨
tuple_data # [10, 20, 30, 40, 50, 60, 70, 80]. 리스트로 들어간 것이 아닌 요소로 들어감.
Out[17]:
[10, 20, 30, 40, 50, 60, 70, 80]
9. pop. 값을 추출하고 Garbage Collection으로 반환한다.¶
- 인덱스를 이용하여 원하는 인덱스의 값을 출력하고 반환할 수 있다.
In [18]:
tuple_data = [1, 2, 3, 4]
pop_data = tuple_data.pop()
tuple_data # [1, 2, 3] 없어진 것을 확인.
Out[18]:
[1, 2, 3]
In [19]:
pop_data # 기본은 인덱스가 가장 높은 값을 출력하고 반환한다.
Out[19]:
4
In [20]:
num_pop = tuple_data.pop(0) # 인덱스를 이용하면 원하는 인덱스의 값을 출력 및 반환할 수 있다.
tuple_data # [1]. 없어진 것을 확인
Out[20]:
[2, 3]
In [21]:
num_pop
Out[21]:
1
In [ ]:
'AI & BigData > Python Basics' 카테고리의 다른 글
#26-파이썬 기초 실습 - 집합(Sets) - 2 (0) | 2020.02.06 |
---|---|
#25 - 파이썬 기초 실습 - 집합(Sets) - 1 (0) | 2020.02.05 |
#23-파이썬 기초 실습 - 튜플(Tuple) - 1 (0) | 2020.02.03 |
#22-파이썬 기초 실습 - 부울(Boolean) (0) | 2020.02.02 |
#21-파이썬 기초 실습 - 딕셔너리 Dictonary - 2 (0) | 2020.02.01 |