๋ฐ์ดํ„ฐ๋ถ„์„๊ฐ€ ๊ณผ์ •/Python

DAY34. Python Basic ์ž๋ฃŒ๊ตฌ์กฐ (1)list, tuple

LEE_BOMB 2021. 11. 2. 13:54
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)