์ž๋ฃŒ๊ตฌ์กฐ ๐Ÿ—„ List(+string) method

yeeun leeยท2020๋…„ 6์›” 28์ผ
0

1. list method

์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ’€๊ฑฐ๋‚˜ ํฌ๋กค๋ง์„ ํ•  ๋•Œ, ๋ฐ์ดํ„ฐ๋ฅผ ๋‚ด ์ž…๋ง›๋Œ€๋กœ ๊ณ ์ณ์•ผํ•  ์ผ๋“ค์ด ๋งŽ๋‹ค. w3schools.com์„ ์ฐธ๊ณ ํ•ด ๋ฆฌ์ŠคํŠธ ๋ฉ”์†Œ๋“œ์™€ ๋ฌธ์ž์—ด ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

append๋Š” ์›Œ๋‚™ ๋งŽ์ด ์จ์„œ ์ œ์™ธํ–ˆ๋‹ค.

index

๋ฆฌ์ŠคํŠธ(fruits)๊ฐ€ ๊ฐ€์ง„ ์ธ์ž(cherry) ์ค‘ ์ฒซ๋ฒˆ์งธ index(2)์„ return. ๋งŒ์•ฝ ๋’ค์— cherry๊ฐ€ ๋˜ ์žˆ์–ด๋„ 2๋ฅผ returnํ•˜๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค. The index() method returns the position at the first occurrence of the specified value.

fruits = ['apple', 'banana', 'cherry']

x = fruits.index("cherry")

# result 
x = 2

extend

๋ฆฌ์ŠคํŠธ์™€ ๋‹ค๋ฅธ data type(tuple, set ๋“ฑ)์„ ํ•ฉ์น  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

๋ฆฌ์ŠคํŠธ๋„ extend๋ฅผ ์จ์„œ ํ•ฉ์น  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ๋‹จ์ˆœํ•˜๊ฒŒ ๋”ํ•˜๊ธฐ๋กœ๋„ concatenate ๊ฐ€๋Šฅํ•˜๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•ด๋‹น ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ๋“ค์–ด๊ฐ€๋Š” data์˜ type์€ iterableํ•ด์•ผ ํ•œ๋‹ค(int๋Š” ์•ˆ ๋˜๊ณ , string์€ ํ•œ ๊ธ€์ž์”ฉ ์ถ”๊ฐ€ ๊ฐ€๋Šฅ)

fruits = ['apple', 'banana', 'cherry']
cars = ('Ford', 'BMW', 'Volvo')

fruits.extend(cars)

# result 
fruits = ['apple', 'banana', 'cherry', 'Ford', 'BMW', 'Volvo']

insert

์›ํ•˜๋Š” ๋ฆฌ์ŠคํŠธ ์ธ๋ฑ์Šค์— element๋ฅผ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค. ๋ฉ”์†Œ๋“œ ๊ตฌ์กฐ๋ฅผ ๋ดค์„ ๋•Œ ๋‹น์—ฐํ•œ ๋ง์ด๊ธด ํ•˜์ง€๋งŒ, ๋‹ค๋ฅธ ํ•จ์ˆ˜๋“ค๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ ๊ธฐ๋ณธ ์ธ์ž๋ฅผ 2๊ฐœ ๋ฐ›๋Š”๋‹ค. (์ธ๋ฑ์Šค, ์š”์†Œ)

fruits = ['apple', 'banana', 'cherry']

fruits.insert(1, "orange")
# result 
fruits = ['apple', 'orange', 'banana', 'cherry']

pop

๊ทธ๋ƒฅ pop()์„ ํ•˜๊ฒŒ ๋˜๋ฉด ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค์˜ ๊ฐ’์„ ์ œ๊ฑฐํ•œ ๋’ค ํ•ด๋‹น ๊ฐ’์„ returnํ•˜๊ณ , index ๊ฐ’์„ ์ง€์ •ํ•˜๋ฉด ๊ทธ index์˜ ๊ฐ’์„ ์ œ๊ฑฐํ•œ ๋’ค์— returnํ•œ๋‹ค.

fruits = ['apple', 'banana', 'cherry']
bnn = fruits.pop(1)

# result
fruits = ['apple', 'cherry']
bnn = 'banana'

fruits = ['apple', 'banana', 'cherry']
crr = fruits.pop()

# result
crr = 'cherry'

remove

์š”์ฒญํ•œ ๊ฐ’์˜ first matching value ์„ ์ œ๊ฑฐํ•ด์ค€๋‹ค. The remove() method removes the first occurrence of the element with the specified value.

a = [0, 2, 3, 2]
a.remove(2)

# result
a = [0, 3, 2]

del

๋ฆฌ์ŠคํŠธ์˜ ํŠน์ • ์ธ๋ฑ์Šค๋ฅผ ์ง€์ •ํ•ด์„œ ์ œ๊ฑฐํ•œ๋‹ค.

๋ฐ˜๋ณต๋ฌธ์—์„œ ์š”์†Œ๋ฅผ ์ง€์›Œ์•ผํ•  ์ผ์ด ๋งŽ์€๋ฐ, del์„ ์“ธ ๊ฒฝ์šฐ ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ธฐ๊ธฐ ํ›จ์”ฌ ์‰ฝ๋‹ค.
๋ฌผ๋ก  ๋ฐ˜๋ณต๋ฌธ ์ž์ฒด์—์„œ ๊ฐ€๊ธ‰์  ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ์ผ์€ ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ๋‹ค๋งŒ del์€ ์ธ๋ฑ์Šค๋กœ ํ•ญ๋ชฉ์„ ์ง€์šฐ๋Š”๋ฐ, ํ•œ๋ฒˆ ํ•ญ๋ชฉ์„ ์ง€์šฐ๊ฒŒ ๋˜๋ฉด ๊ทธ ๋’ค์˜ ๋ชจ๋“  ์š”์†Œ๋“ค์˜ ์ธ๋ฑ์Šค๊ฐ€ ๋ฐ”๋€Œ๊ฒŒ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ํ›จ์”ฌ ์—๋Ÿฌ๊ฐ€ ๋‚  ํ™•๋ฅ ์ด ๋†’๋‹ค๋Š” ๊ฒƒ์„ ์œ ์˜ํ•˜์ž.

๊ตณ์ด ๋ฐ˜๋ณต๋ฌธ์—์„œ ์š”์†Œ๋ฅผ ์ง€์šฐ๊ณ  ์‹ถ๋‹ค๋ฉด remove๋ฅผ ์“ฐ๋Š” ๊ฒƒ์ด ๋‚ซ๋‹ค. ํ•˜์ง€๋งŒ ํ•ด๋‹น ๊ฐ’์ด ์—†์„ ๋•Œ์— ๋Œ€ํ•œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋Š” ํ•ด์ค˜์•ผ ํ•œ๋‹ค!

a = [3, 2, 2, 1]
del a[1]

# result 
a = [3, 2, 1]

sort

๋ฆฌ์ŠคํŠธ ์š”์†Œ๋“ค์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ฆฌํ•œ๋‹ค. argument์— ๋”ฐ๋ผ ๊ธฐ์ค€์€ ๋ฐ”๊ฟ”์ค„ ์ˆ˜ ์žˆ๋‹ค. The sort() method sorts the list ascending by default. You can also make a function to decide the sorting criteria(s).

cars = ['Ford', 'BMW', 'Volvo']
cars.sort()

# result
cars = ['BMW', 'Ford', 'Volvo']

reverse

๋ฆฌ์ŠคํŠธ ์š”์†Œ๋“ค์˜ ์ˆœ์„œ๋ฅผ ๊ฑฐ๊พธ๋กœ ๋’ค์ง‘์–ด์ค€๋‹ค. The reverse() method reverses the sorting order of the elements.

fruits = ['apple', 'banana', 'cherry']
fruits.reverse()

# result
fruits = ['cherry', 'banana', 'apple']

2. string method

strip

๋ณดํ†ต ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•ด์ค„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ๋ฌธ์ž์˜ ์ „ํ›„์— ์žˆ๋Š” ํŠน์ • ๊ฐ’์„ ์—†์•จ ๋•Œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

  • strip: ์ธ์ž๋กœ ์ „๋‹ฌ๋œ ๋ฌธ์ž๋ฅผ ์ „, ํ›„๋กœ ์ œ๊ฑฐ
  • rstrip: ์•ž์—์„œ ์ œ๊ฑฐ
  • lstrip: ๋’ค์—์„œ ์ œ๊ฑฐ

์š” ํ•จ์ˆ˜๋Š” ์˜ˆ์ „์— ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ’€ ๋•Œ, ์–‘์ˆ˜๋ฅผ ์ด์ง„์ˆ˜๋กœ ๋ฐ”๊ฟ”์„œ 0 ๊ฐ’์„ ๋„์ถœํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๋‹ค๊ฐ€ ์ฒ˜์Œ ์“ฐ๊ฒŒ ๋๋‹ค. ์–ด๋”˜๊ฐ€ ๊ฐ’์„ ๋„ฃ์„ ๋•Œ ๋’ค์— ์žˆ๋Š” ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•ด์ฃผ๋Š” ํ•จ์ˆ˜๋กœ๋งŒ ์•Œ๊ณ  ์žˆ์—ˆ๋Š”๋ฐ, ๋งŒ์•ฝ 1๊ณผ 1 ์‚ฌ์ด์˜ 0 ๊ฐ’์„ ๊ตฌํ•ด์•ผ ํ•  ๋•Œ, ์ธ์ž๋กœ 0์„ ์ค„ ๊ฒฝ์šฐ ๋’ค์— ์žˆ๋Š” 0์„ ์ œ๊ฑฐํ•ด์คŒ์œผ๋กœ์จ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

binaryNumber = format(529, 'b')
binaryNumberList = binaryNumber.strip('0').split('1')

print(binaryNumberList)
# ['', '0000', '000', '']

replace

๋ฌธ์ž์—ด ๋‚ด์˜ ํŠน์ • ๋ฌธ์ž๋ฅผ ์›ํ•˜๋Š” ๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์ค„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ๋‚ด๊ฐ€ '1991 .4 .1.' ์ด๋Ÿฐ์‹์œผ๋กœ database์— formattingํ•ด์„œ ๋„ฃ์„ ์ˆ˜ ์—†๋Š” ๋ฌธ์ž๋ฅผ ๋ฐ›์•˜๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ, ๊ณต๋ฐฑ ๋ฐ dot์„ dash๋กœ ๋ฐ”๊ฟ”์„œ ๋ฐ์ดํ„ฐ ํ˜•ํƒœ๋ฅผ ๋งž์ถœ ์ˆ˜๊ฐ€ ์žˆ๋‹ค.

date = '1991. 4. 1.'
date_formatted = date.replace('. ','-').rstrip('.')

join

๋ฆฌ์ŠคํŠธ์— ๋‹ด๊ธด ๋ฌธ์ž์—ด์„ ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ํ•ฉ์ณ์ค„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ๋”ฐ์˜ดํ‘œ ์•ˆ์—๋Š” ๋ฌธ์ž์—ด์„ ๋ถ™์—ฌ์ค„ ๋ฌธ์ž๊ฐ€, ๋ฉ”์†Œ๋“œ ์ธ์ž๋กœ ๋ฐฐ์—ด์ด ๋“ค์–ด๊ฐ„๋‹ค.

time
['10', '31', '00']

':'.join(time)
# result: '10:31:00'

zfill

zero fill์˜ ์ค„์ž„๋ง๋กœ, ๋ฌธ์ž์—ด ์•ž์„ 0์œผ๋กœ ์ฑ„์›Œ์ค€๋‹ค. ๋”ฐ์˜ดํ‘œ ์•ˆ์— ๋ณ€๊ฒฝ๋  ์ˆซ์ž๋ฅผ, ๋ฉ”์†Œ๋“œ ์ธ์ž ์•ˆ์—๋Š” ๊ณ ์ •๋  0์˜ ๊ฐœ์ˆ˜๋ฅผ ๋„ฃ๋Š”๋‹ค.

์ƒํ’ˆ ๋ฒˆํ˜ธ(MF000002 ๋“ฑ) ๊ฐ™์€ ๊ฒƒ๋“ค์„ ๋งŒ๋“ค ๋•Œ ์œ ์šฉํ•˜๋‹ค. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ƒ์„ฑ ์‹œ์— AI(auto increment), UQ(unique) ์ฒ˜๋Ÿผ ZF ์†์„ฑ์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๊ธฐ๋„ ํ•˜์ง€๋งŒ, ํŒŒ์ด์ฌ์—์„œ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

>>> "3".zfill(8)
'00000003'
>>> "24".zfill(8)
'00000024'
>>> "34234".zfill(8)
'00034234'

ํ™œ์šฉ ์˜ˆ์‹œ

strip, replace๋ฅผ ๋‘˜ ๋‹ค ์‚ฌ์šฉํ•ด๋ณด์ž. ์กฐํšŒ์ˆ˜ 114,334ํšŒ ๋ผ๋Š” string์„ int๋กœ ๋ฐ”๊พธ๊ณ  ์‹ถ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค.

view = '114,334ํšŒ'
view_detail = int(view.rstrip('ํšŒ').replace(',',''))

#result
114334

์‰ผํ‘œ๊ฐ€ ์žˆ๋Š” ๋ฌธ์ž๋Š” ์ •์ˆ˜๋กœ ๋ฐ”๊พธ๋Š”๊ฒŒ ๋ถˆ๊ฐ€ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๋ฐ˜๋“œ์‹œ ์‰ผํ‘œ๋„ ์ œ๊ฑฐํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค!

๊ทธ ์™ธ์—๋„ ๋ฌธ์ž์—ด์€ ์—ฌ๋Ÿฌ ๋ฉ”์†Œ๋“œ๊ฐ€ ์กด์žฌํ•œ๋‹ค. ์•ˆ ์จ๋ณธ ๊ฒƒ๋“ค์ด ๊ฝค ๋งŽ์•„์„œ ํ•˜๋‚˜์”ฉ ์จ๋ด์•ผ๊ฒ ๋‹ค. (์ถœ์ฒ˜๋Š” ์ด๋ฏธ์ง€ ์•ˆ์—)

profile
์ด์‚ฌ๊ฐ„ ๋ธ”๋กœ๊ทธ: yenilee.github.io

0๊ฐœ์˜ ๋Œ“๊ธ€