DAY34. Python Basic ์๋ฃ๊ตฌ์กฐ (1)list, tuple
list
ํ์) ๋ณ์ = [๊ฐ1, ๊ฐ2, ...]
1์ฐจ์ ๋ฐฐ์ด ๊ตฌ์กฐ(vector)
๋ค์ํ ์๋ฃํ ์ ์ฅ
์์ ์กด์ฌ, ์์ธ ์ฌ์ฉ
๊ฐ ์์ (์ถ๊ฐ, ์ฝ์
, ์์ , ์ญ์ ) ๊ฐ๋ฅ
1. ๋จ์ผ list & ์์ธ
lst = [1, 2, 3, 4, 5]
print(lst) # [1, 2, 3, 4, 5]
len(lst) # 5
์์ธ(index) : ๊ฐ์ ์์น(0 ๋ถํฐ ์์)
lst[:] # ์ ์ฒด ์์
lst[0] # ์ฒซ๋ฒ์งธ ์์
lst[-1] # ๋ง์ง๋ง ์์
์ฌ๋ผ์ด์ฑ : ๋ฒ์ ์ฐธ์กฐ
lst[0:3] # [1, 2, 3]
list + range
x = list(range(1, 101))
x # range(1, 101) -> [1, 2, ... ,100]
len(x) # 100
print(x[:5]) # [1, 2, 3, 4, 5]
print(x[-5:]) # [96, 97, 98, 99, 100]
2์ฉ ์ฆ๊ฐ : ๋ณ์[start:stop[:step]]
print(x[::2]) # ํ์ ์์ธ
print(x[1::2]) # ์ง์ ์์ธ
2. ์ค์ฒฉ list & ์์ธ
y = [['a','b','c'], [1,2,3]]
print(y) # [['a', 'b', 'c'], [1, 2, 3]]
len(y) # 2
print(y[0]) # ['a', 'b', 'c']
print(y[1]) # [1, 2, 3]
print(y[0][1]) # b
print(y[1][2]) # 3
3. ๊ฐ ์์ (์ถ๊ฐ, ์ฝ์
, ์์ , ์ญ์ )
num = ['one', 2, 'three', 4]
print(num) # ['one', 2, 'three', 4]
len(num) # 4
type(num) # list
object.method()
method : ๊ฐ์ฒด์์ ํธ์ถํ ์ ์๋ ํจ์(function)
1) list ์ถ๊ฐ
num.append('five')
print(num) # ['one', 2, 'three', 4, 'five']
2) list ์ญ์
num.remove('three')
print(num) # ['one', 2, 4, 'five']
3) list ์์ : ์์ธ ์ด์ฉ
num[1] = 'two'
print(num) # ['one', 'two', 4, 'five']
4) list ์ฝ์
num.insert(0, 'zero')
print(num) # ['zero', 'one', 'two', 4, 'five']
4. list ์ฐ์ฐ
x = [1, 2, 3, 4]
y = [1.5, 7.2]
1) list ๊ฒฐํฉ(+)
z = x + y # new object
print('z=', z) # z= [1, 2, 3, 4, 1.5, 7.2]
2) list ํ์ฅ
x.extend(y) # old object : ๋จ์ผ list
print('x=', x) # x= [1, 2, 3, 4, 1.5, 7.2]
3) list ์ถ๊ฐ
x.append(y) # old object : ์ค์ฒฉ list
print('x=', x) # x= [1, 2, 3, 4, 1.5, 7.2, [1.5, 7.2]]
4) list ๋ฐ๋ณต(*)
result = y * 2
print(result) # [1.5, 7.2, 1.5, 7.2]
# [1, 2, 3, 4]
x * 0.5 # TypeError:
5) ๊ฐ ์์ ์ฐ์ ์ฐ์ฐ
print(y) # [1.5, 7.2]
y[0] * 0.5
for i in y :
print(i * 0.5)
5. list ๊ฐ์ฒด ์ ๊ณต ๋ฉ์๋
ํ์) ๊ฐ์ฒด.๋ฉ์๋()
dir(object) -> object ํธ์ถ ๊ฐ๋ฅ ๋ฉ์๋ ๋ชฉ๋ก ํ์ธ
type(x) # list
dir(x)
print(x) # [1, 2, 3, 4, 1.5, 7.2, [1.5, 7.2]]
x.count(4) # 1
x.pop(0) # 1
print(x) # [2, 3, 4, 1.5, 7.2, [1.5, 7.2]]
y.sort() # ์ค๋ฆ์ฐจ์
print(y) # [1.5, 7.2]
y.sort(reverse= True) # ๋ด๋ฆผ์ฐจ์
print(y) # [7.2, 1.5]
x.clear() # ๋ชจ๋ ์์ ์ ๊ฑฐ
print(x) # []
6. list์์ ์์ ์ฐพ๊ธฐ
if ์์ in list๊ฐ์ฒด :
์์๊ฐ ์๋ ๊ฒฝ์ฐ
else :
์์๊ฐ ์๋ ๊ฒฝ์ฐ
num = [] # ๋น list - ์ซ์ ์ ์ฅ
for i in range(100) : # 0 ~ 99
num.append(i)
print(num)
์์ ์ฐพ๊ธฐ
if int(input()) in num :
print('ํด๋น ์ซ์ ์์')
else :
print('ํด๋น ์ซ์ ์์')
์งํฉ๊ด๋ จ : ์นผ๋ผ ์ ํ ์
set1 = {0,1,2,3,4} # ์นผ๋ผ
set2 = {2, 4} # ์นผ๋ผ ์ ํ
์์ ์ถ๊ฐ
print(set1) # {0, 1, 2, 3, 4}
print(set2) # {2, 4}
set1.union(set2) # ํฉ์งํฉ : {0, 1, 2, 3, 4}
set1.difference(set2) # ์ฐจ์งํฉ : {0, 1, 3}
# {0,1,2,3,4} - {2, 4} = {0, 3, 5}
set1.intersection(set2) # ๊ต์งํฉ : {2, 4}
๋ฆฌ์คํธ ๋ดํฌ
- list์์์ for์ if๋ฌธ์ ํ ์ค๋ก ํํํ ๋ฌธ๋ฒ
ํ์1) ๋ณ์ = [์คํ๋ฌธ for ๋ณ์ in ์ด๊ฑฐํ๊ฐ์ฒด ]
์คํ ์์ : 1.for๋ฌธ > 2.์คํ๋ฌธ > 3. ๋ณ์ ์ ์ฅ
ํ์2) ๋ณ์ = [์คํ๋ฌธ for ๋ณ์ in ์ด๊ฑฐํ๊ฐ์ฒด if ์กฐ๊ฑด์ ]
์คํ ์์ : 1.for๋ฌธ > 2.if๋ฌธ > [3. ์คํ๋ฌธ > 4. ๋ณ์ ์ ์ฅ]
ํ์1) ๋ณ์ = [์คํ๋ฌธ for ๋ณ์ in ์ด๊ฑฐํ๊ฐ์ฒด]
x = [2, 4, 1, 5, 8]
print(x)
1) x๋ณ๋์ ์ ๊ณฑ(**) ๊ณ์ฐ
print(x ** 2) # TypeError
list + for ์
result = [] # ๋น list
for i in x :
result.append(i ** 2)
print(result) # [4, 16, 1, 25, 64]
list ๋ดํฌ : ์คํ๋ฌธ = ์ ๊ณฑ(**)
result = [i ** 2 for i in x ]
print(result) # [4, 16, 1, 25, 64]
2) ์ง์/ํ์ ํ๋จ : ์คํ๋ฌธ = ํ ์ค if๋ฌธ
result2 = ['์ง์' if i % 2 == 0 else 'ํ์' for i in x]
print(result2) # ['์ง์', '์ง์', 'ํ์', 'ํ์', '์ง์']
ํ์2) ๋ณ์ = [์คํ๋ฌธ for ๋ณ์ in ์ด๊ฑฐํ๊ฐ์ฒด if ์กฐ๊ฑด์ ]
num = list(range(100)) # 0 ~ 99
print(num)
5์ ๋ฐฐ์ ์ถ์ถ
result3 = [n for n in num if n % 5 == 0]
print(result3)
# [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95]
len(result3) # 20
tuple
ํ์) ๋ณ์ = (๊ฐ1, ๊ฐ2, ...)
1์ฐจ์ ๋ฐฐ์ด๊ตฌ์กฐ(vector)
- ์์ ์กด์ฌ, ์์ธ ์ฌ์ฉ
- ์์ ๋ถ๊ฐ
- list ์๋ ๋น ๋ฆ
tuple ์
tp = (10) # tp = 10
print(tp) # 10
type(tp) # int
tp = (10,)
print(tp) # (10,)
type(tp) # tuple
์์ ์กด์ฌ, ์์ธ ์ฌ์ฉ
tp2 = (1,2,3,4,5)
print(tp2) # (1, 2, 3, 4, 5)
tp2[0] # 1
tp2[-1] # 5
tp2[:3] # (1, 2, 3)
tp2[2:] # (3, 4, 5)
์์ ๋ถ๊ฐ
tp2[1] = 20 # TypeError
for + tuple
for t in tp2 :
print(t)
print(t**2)
zip() : vector ์์ ๋ฌถ์ -> tuple ๋ฐํ
name = ['ํ๊ธธ๋','์ด์์ ','๊ฐ๊ฐ์ฐฌ']
pay = [100,200,300]
zip_re = zip(name, pay)
print(zip_re) # object info
for z in zip_re :
print(z)
('ํ๊ธธ๋', 100)
('์ด์์ ', 200)
('๊ฐ๊ฐ์ฐฌ', 300)