Python/Fundamentals

(useful) Methods

metamong 2022. 11. 13.

๐Ÿ˜ฒ ํŒŒ์ด์ฌ์—๋Š” ์ •๋ง ๋‹ค์–‘ํ•˜๊ณ  ์œ ์šฉํ•œ 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

๋Œ“๊ธ€