๐ฒ ํ์ด์ฌ์๋ ์ ๋ง ๋ค์ํ๊ณ ์ ์ฉํ method๊ฐ ์กด์ฌํ๋ค. ์ฌ๋ฌ ์ฝ๋ฉ ๋ฌธ์ ๋ฅผ ํ๋ฉด์ ๋ค์ํ method๋ฅผ ํ์ฉํด๋ณด์๋๋ฐ, ์ด๋ฒ ํฌ์คํ ์ ํตํด ์ฒ์ ๋ณด๋, ์ ์ฉํ method๋ง ๊ณจ๋ผ ๊ฐ๋จํ ์ ๋ฆฌํ๊ณ ์ ํจ :)
#rjust
๐ฒ ์ง์ ํ ์ฒซ๋ฒ์งธ ์ธ์ ๊ธ์ ๊ธธ์ด๋งํผ ๋ฌธ์์ด์ด ์ค๋ฅธ์ชฝ์ผ๋ก ๋์ด - ๊ทธ ์์ ๊ณต๊ฐ์ ๋๋ฒ์งธ ์ธ์๋ก ์ฑ์์ง
print('abcd'.rjust(5,"0"))
#'0abcd'
#zfill
๐ฒ ์ rjust()์ ๋น์ทํ๋, 0์ด ์ฑ์์ง๋ค๊ณ ์๊ฐํ๋ฉด ๋จ
→ zfill syntax
string.zfill(len)
#'Required. A number specifying the desired length of the string'#
→ len ๊ฐ์๋งํผ์ ๋ฌธ์์ด์ด ๋ง๋ค์ด์ง๊ณ , ๊ธฐ์กด ๋ฌธ์์ด์ด ์ฐจ์งํ๋ ์๋ฆฌ ์ธ์ ๋ชจ๋ ์๋ฆฌ์ 0์ด ๋ค์ด๊ฐ๋ค. (๋ฌธ์์ด ์ ์๋ฆฌ์ 0์ด ์ฑ์์ง!)
→ ์๋ ์์ ์ฐธ์กฐ
a = "hello"
b = "welcome to the jungle"
c = "10.000"
print(a.zfill(10)) #00000hello
print(b.zfill(10)) #welcome to the jungle
print(c.zfill(10)) #000010.000
* zfill ์ถ์ฒ) https://www.w3schools.com/python/ref_string_zfill.asp
# deepcopy, shallowcopy
* ๋ด์ฉ) docu https://docs.python.org/3/library/copy.html
→ syntax
copy.Copy(x) #return a shallow copy of x
copy.deepcopy(x,[,memo]) #return a deep copy of x
→ the difference between shallow and deep copying
'The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances):
- A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.
- A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original. - two problems exist
โ recursive problems - may cause a recursive loop
โก way much copying everything
→ assignment, shallow copy, deep copy ์ฐจ์ด์ ์ ์๋ code๋ก ๋ณด์๋ฉด,
import copy
a = [1,2,3]
b = [4,5,6]
c = [a,b]
#---- assignment ----
d = c
print(id(c) == id(d)) #True
print(id(c[0]) == id(d[0])) #True
#---- shallowcopy ----
d = copy.copy(c)
print(id(c) == id(d)) #False
print(id(c[0]) == id(d[0])) #True
#---- deepcopy ----
d = copy.deepcopy(c)
print(id(c) == id(d)) #False
print(id(c[0]) == id(d[0])) #False
๐ฒ ํ ๋น์ ๋จ์ํ ๊ธฐ์กด object๋ฅผ referencingํด์ฃผ๋ ์ ๋์ ์ง๋์ง ์๊ธฐ์, ์๋ก์ด object๊ฐ ๋ง๋ค์ด์ง์ง ์์ผ๋ฏ๋ก ๊ณ ์ id๋ ์๋ก ๊ณต์ ๋จ
๐ฒ shallow copy๋ ์๋ก์ด compound object๋ฅผ ๋ง๋ค์ด์ฃผ๋, object ๋ด๋ถ๋ referencing์ ํด์ค. ๊ทธ๋ฌ๋ deep copy๋ object ๋ด๋ถ๊น์ง ๋ชจ๋ ์๋ก์ด object๋ฅผ ์์ฑํ๋ฏ๋ก ์ด ๋ถ๋ถ์ด ์ฐจ์ด์
๐ฒ shallow copy๋ dictionary์ ๊ฒฝ์ฐ dict.copy()๋ฅผ ์ฌ์ฉํ๊ณ , list์ ๊ฒฝ์ฐ [:]๋ผ๋ slicing์ ์งํํ๋ฉด ๋๋ค.
→ stackoverflow ์ด๋ user์ ํ๋ง๋
"For immutable objects, there is no need for copying because the data will never change, so Python uses the same data; ids are always the same. For mutable objects, since they can potentially change, [shallow] copy creates a new object.
Deep copy is related to nested structures. If you have list of lists, then deepcopy copies the nested lists also, so it is a recursive copy. With just (shallow)copy, you have a new outer list, but inner lists are references.
Assignment does not copy. It simply sets the reference to the old data. So you need copy to create a new list with the same contents."
import copy
a = [[1,2],[3,4,5]]
a_deepcopy = copy.deepcopy(a)
a_copy = copy.copy(a)
a[1].append(6)
print(a) #[[1, 2], [3, 4, 5, 6]]
print(a_deepcopy) #[[1, 2], [3, 4, 5]]
print(a_copy) #[[1, 2], [3, 4, 5, 6]]
→ shallow copy๋ a_copy๋ 6๊น์ง ์ฒจ๊ฐ๋ ๊ฑธ ๋ณผ ์ ์๋ค.
a.append(6)
print(a_copy) #[[1, 2], [3, 4, 5]]
→ ํ์ง๋ง, ๋จ์ผ list์์ append๋ ๊ฒฝ์ฐ๋ shallow copy๋ผ๋ ์๋ณธ ์ฒจ๊ฐ์ ์ํฅ์ ๋ฐ์ง ์๊ธฐ์ ๊ฒฐ๊ณผ๊ฐ ๋ณํ์ง ์์
* ๋ด์ฉ ์ถ์ฒ) https://stackoverflow.com/questions/17246693/what-is-the-difference-between-shallow-copy-deepcopy-and-normal-assignment-oper
'Python > Fundamentals' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
python standard libraries (0) | 2022.08.22 |
---|---|
ฮป ํํ์ (0) | 2022.08.21 |
list, string, tuple, dictionary, set (iterables) (0) | 2022.08.19 |
File/Exception/Log Handling (0) | 2022.07.14 |
python OOP (0) | 2022.07.07 |
๋๊ธ