Python Data Structure¶
In [1]:
import warnings
warnings.filterwarnings('ignore')
I. String¶
- 문자열(String) 생성
In [2]:
S1 = 'The truth is out there.'
print(S1)
The truth is out there.
1) Concatenation¶
- '+': 문자열 연결
- '*': 문자열 반복
In [3]:
print('=' * 40)
print('\t', S1)
print('=' * 40)
======================================== The truth is out there. ========================================
In [4]:
S2 = 'The truth is'
S3 = ' out there.'
In [5]:
S2 + S3
Out[5]:
'The truth is out there.'
In [6]:
S2 * 3
Out[6]:
'The truth isThe truth isThe truth is'
In [7]:
S3 * 3
Out[7]:
' out there. out there. out there.'
2) Length¶
In [8]:
len(S1)
Out[8]:
23
3) Indexing¶
In [9]:
print(S1)
The truth is out there.
In [10]:
S1[1]
Out[10]:
'h'
In [11]:
S1[5]
Out[11]:
'r'
In [ ]:
S1[0]
In [12]:
S1[-1]
Out[12]:
'.'
In [13]:
S1[-13]
Out[13]:
'i'
In [14]:
S1[-18]
Out[14]:
'r'
4) Slicing¶
In [15]:
print(S1)
The truth is out there.
- with Indexing
In [16]:
S1[4] + S1[5] + S1[6] + S1[7] + S1[8]
Out[16]:
'truth'
- with Slicing
- S1[이상:미만]
In [17]:
S1[4:9]
Out[17]:
'truth'
In [18]:
S1[-19:-14]
Out[18]:
'truth'
In [26]:
S1[17:22]
Out[26]:
'there'
In [20]:
S1[-6:-1]
Out[20]:
'there'
In [22]:
S1[10:12]
Out[22]:
'is'
In [23]:
S1[:12]
Out[23]:
'The truth is'
In [24]:
S1[13:]
Out[24]:
'out there.'
5) Formatting¶
- '%s': String
In [27]:
'The X-Files is an American science fiction %s.' % 'Drama'
Out[27]:
'The X-Files is an American science fiction Drama.'
- 우측정렬
In [28]:
'%7s' % 'Drama'
Out[28]:
' Drama'
- 좌측정렬
In [29]:
'%-7s' % 'Drama'
Out[29]:
'Drama '
- '%d': Integer
In [30]:
'The program spanned nine seasons, with %d episodes.' % 202
Out[30]:
'The program spanned nine seasons, with 202 episodes.'
- '%f': Floating Point
In [31]:
'%f' % 3.141593
Out[31]:
'3.141593'
In [32]:
'%.2f' % 3.141593
Out[32]:
'3.14'
In [34]:
'%5.3f' % 3.141593
Out[34]:
'3.142'
- 2개 값 대입
In [35]:
String = 'Drama'
Integer = 202
'The X-Files is an American science fiction %s, with %d episodes.' % (String, Integer)
Out[35]:
'The X-Files is an American science fiction Drama, with 202 episodes.'
II. List¶
1) [ ] 기호로 생성 - 값 변경 가능
In [70]:
L1 = [1, 3, 5, 7, 9]
print(L1)
[1, 3, 5, 7, 9]
In [37]:
print(type(L1))
print(type(L1[0]))
<class 'list'> <class 'int'>
In [38]:
L2 = ['HP', 'IBM', 'DELL', 'EMC', 'MS']
print(L2)
['HP', 'IBM', 'DELL', 'EMC', 'MS']
In [39]:
print(type(L2))
print(type(L2[0]))
<class 'list'> <class 'str'>
In [40]:
L3 = [1, '삼', 5, '칠', 9]
print(L3)
[1, '삼', 5, '칠', 9]
In [41]:
print(type(L3))
print(type(L3[0]))
print(type(L3[1]))
<class 'list'> <class 'int'> <class 'str'>
In [42]:
L4 = [1, 3, ['HP', 'MS']]
print(L4)
[1, 3, ['HP', 'MS']]
In [43]:
print(type(L4))
print(type(L4[1]))
print(type(L4[2]))
print(type(L4[2][0]))
<class 'list'> <class 'int'> <class 'list'> <class 'str'>
In [44]:
L5 = [5, 7, ('IBM', 'EMC')]
print(L5)
[5, 7, ('IBM', 'EMC')]
In [45]:
print(type(L5))
print(type(L5[1]))
print(type(L5[2]))
print(type(L5[2][0]))
<class 'list'> <class 'int'> <class 'tuple'> <class 'str'>
2) Indexing¶
- with L1
In [46]:
print(L1)
[1, 3, 5, 7, 9]
In [47]:
L1[2]
Out[47]:
5
In [48]:
L1[2] + L1[4]
Out[48]:
14
In [49]:
L1[-2]
Out[49]:
7
- with L4
In [50]:
print(L4)
[1, 3, ['HP', 'MS']]
In [51]:
L4[1]
Out[51]:
3
In [52]:
L4[2]
Out[52]:
['HP', 'MS']
In [53]:
L4[2][1]
Out[53]:
'MS'
In [54]:
L4[2][0] + L4[2][1]
Out[54]:
'HPMS'
3) Slicing¶
- with L1
In [55]:
print(L1)
[1, 3, 5, 7, 9]
In [56]:
L1[1:4]
Out[56]:
[3, 5, 7]
In [57]:
L1[:3]
Out[57]:
[1, 3, 5]
In [58]:
L1[2:]
Out[58]:
[5, 7, 9]
- with L6
In [59]:
L6 = [1, 3, 5, [2, 4, 6]]
print(L6)
[1, 3, 5, [2, 4, 6]]
In [60]:
L6[2:]
Out[60]:
[5, [2, 4, 6]]
In [61]:
L6[3]
Out[61]:
[2, 4, 6]
In [62]:
L6[3][0:2]
Out[62]:
[2, 4]
4) Change Values¶
- 5 to 6
In [63]:
print(L1)
[1, 3, 5, 7, 9]
In [64]:
L1[2] = 6
print(L1)
[1, 3, 6, 7, 9]
5) Delete Values¶
In [65]:
print(L1)
[1, 3, 6, 7, 9]
In [66]:
L1[2:4] = []
print(L1)
[1, 3, 9]
In [67]:
del L1[2]
print(L1)
[1, 3]
- Error
In [71]:
del L1
#print(L1)
6) Function( )¶
In [80]:
L7 = [8, 3, 9, 2, 1]
print(L7)
[8, 3, 9, 2, 1]
- 오름차순 정렬
In [81]:
L7.sort()
print(L7)
[1, 2, 3, 8, 9]
- 역순 정렬
In [82]:
L7.sort(reverse=True)
print(L7)
[9, 8, 3, 2, 1]
In [83]:
L7.reverse()
print(L7)
[1, 2, 3, 8, 9]
- 마지막에 값('0') 추가
In [78]:
#L7[5] = 0
#print(L7)
In [77]:
L7.append(0)
print(L7)
[9, 8, 3, 2, 1, 0]
- 2번 인덱스에 값('5') 추가
In [84]:
L7.insert(2, 5)
print(L7)
[1, 2, 5, 3, 8, 9]
7) Operators¶
In [93]:
L8 = [85, 93, 75, 97, 69]
L9 = [91, 90, 85, 97, 89]
- Concatenation
- '+': 리스트 연결
In [94]:
L8 + L9
Out[94]:
[85, 93, 75, 97, 69, 91, 90, 85, 97, 89]
- Concatenation
- '*': 리스트 반복
In [95]:
L8 * 2
Out[95]:
[85, 93, 75, 97, 69, 85, 93, 75, 97, 69]
In [96]:
L9 * 3
Out[96]:
[91, 90, 85, 97, 89, 91, 90, 85, 97, 89, 91, 90, 85, 97, 89]
III. Tuple¶
1) ( ) 기호로 생성 - 값 변경 불가능
In [97]:
T1 = (1, 2)
print(T1)
(1, 2)
- Error-1
In [101]:
#del T1[0]
- Error-2
In [100]:
#T1[0] = 'a'
2) Tuple in Tuple¶
In [102]:
T3 = (1, 2, (3, 4))
print(T3)
(1, 2, (3, 4))
- Error-3
In [104]:
#T3[2][0] = 6
3) List in Tuple¶
In [106]:
T4 = (1, 2, [3, 4])
print(T4)
(1, 2, [3, 4])
- Change Values
In [107]:
T4[2][1] = 6
print(T4)
(1, 2, [3, 6])
IV. Dictionary¶
- 순서가 없음(Unordered)
- Key와 Value 한쌍으로 구성
1) {Key:Value} 구조 선언¶
In [121]:
D1 = {'Name':'LEE', 'Age':24}
print(D1)
{'Name': 'LEE', 'Age': 24}
In [122]:
print(type(D1))
print(type(D1['Name']))
print(type(D1['Age']))
<class 'dict'> <class 'str'> <class 'int'>
In [112]:
#D1[0]
In [123]:
D1['Name']
Out[123]:
'LEE'
2) Key:Value 추가¶
In [124]:
D1['Height'] = 183
print(D1)
{'Name': 'LEE', 'Age': 24, 'Height': 183}
3) Key:Value 삭제¶
In [125]:
del D1['Age']
print(D1)
{'Name': 'LEE', 'Height': 183}
4) Function( )¶
- Key 확인
In [126]:
D1.keys()
Out[126]:
dict_keys(['Name', 'Height'])
- Value 확인
In [127]:
D1.values()
Out[127]:
dict_values(['LEE', 183])
- Key:Value 삭제(초기화)
In [128]:
print(D1)
D1.clear()
print(D1)
{'Name': 'LEE', 'Height': 183} {}
5) Dictionary with List¶
In [129]:
L1 = ['Red', 'Green', 'Blue']
L2 = [255, 127, 63]
In [130]:
D2 = {x : y for x, y in zip(L1, L2)}
print(D2)
{'Red': 255, 'Green': 127, 'Blue': 63}
V. Casting¶
1) Data Type¶
- int to float
In [133]:
print(type(9))
print(type(float(9)))
<class 'int'> <class 'float'>
- str to float
In [135]:
print(type('9.4'))
print(type(float('9.4')))
<class 'str'> <class 'float'>
- float to int
In [136]:
print(type(9.0))
print(9.0)
print(type(int(9.0)))
print(int(9.0))
<class 'float'> 9.0 <class 'int'> 9
- str to int
In [137]:
print(type('9'))
print('9')
print(type(int('9')))
print(int('9'))
<class 'str'> 9 <class 'int'> 9
- float to int
- Warning!!!
In [140]:
print(type(3.14))
print(3.14)
print(type(int(3.14)))
print(int(3.14))
<class 'float'> 3.14 <class 'int'> 3
- int to str
In [142]:
print(type(9))
print(str(9))
print(type(str(9)))
<class 'int'> 9 <class 'str'>
- float to str
In [143]:
print(type(3.14))
print(type(str(3.14)))
<class 'float'> <class 'str'>
2) Data Structure¶
- List to Tuple
In [144]:
tuple([1, 3, 5, 7, 9])
Out[144]:
(1, 3, 5, 7, 9)
- Tuple to List
In [145]:
list((1, 3, 5, 7, 9))
Out[145]:
[1, 3, 5, 7, 9]
- List to Dictionary
In [147]:
dict([('A', 123), ('B', 234), ['C', 567], ['D', 890]])
Out[147]:
{'A': 123, 'B': 234, 'C': 567, 'D': 890}
'# Coding > 데이터 분석을 위한 Python' 카테고리의 다른 글
Python 넘파이 (0) | 2023.10.02 |
---|---|
Python 클래스 & 패키지 (0) | 2023.10.02 |
Python 함수와 모듈 (0) | 2023.10.02 |
Python 제어문 (0) | 2023.10.02 |
Python 데이터 유형과 연산자 (0) | 2023.10.02 |