์ƒˆ์†Œ์‹

๐Ÿ ํŒŒ์ด์ฌ (Python)

ํŒŒ์ด์ฌ (Python) ๋ฌธ์ž ์ž…๋ ฅ, map(), split() , sys.stdin.readline() , rstrip()

  • -

 

 

* map

ํ•จ์ˆ˜ ์‚ฌ์šฉ๋ฒ•
map(<fucntion>, <iterable>) # iterable : list, tuple, dictionary, str

 

 

 

 

map ์˜ˆ์ œ- ์‹ค์ˆ˜ํ˜•์œผ๋กœ ๋œ a์˜ ์š”์†Œ๋ฅผ, ์ •์ˆ˜ํ˜•์œผ๋กœ ๋ฐ”๊พธ๊ธฐ.
# map(ํ•จ์ˆ˜, ๋ฆฌ์ŠคํŠธ or ํŠœํ”Œ์˜ ๋ณ€์ˆ˜๋ช…)
a = [1.5 , 1.9 , 13.5]
b = list(map(int, a))
print(b)

 

 

 

 

map ์˜ˆ์ œ - ๋ฆฌ์ŠคํŠธ ์š”์†Œ์— 3์”ฉ ๊ณฑํ•ด์ฃผ๊ธฐ.
a = [3,4,5,2,4,3,5,13,91]

def mul(n):
    n *=3
    return n
    
print(list(map(mul,a)))

 

 


 

 

*split

 

split ์˜ˆ์ œ - ๋ฌธ์ž์—ด ๊ณต๋ฐฑ์œผ๋กœ ๋‚˜๋ˆ„๊ธฐ
x = "i am student, you are a girl"
b= x.split()  # ๊ด„ํ˜ธ์•ˆ์— ์–ด๋–ค ๋ฌธ์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ถ„๋ฆฌํ• ์ง€ ์ ์Šต๋‹ˆ๋‹ค.
print(b)

split()๊ด„ํ˜ธ ์•ˆ์— ์•„๋ฌด๊ฒƒ๋„ ๋„ฃ์ง€ ์•Š์œผ๋ฉด, ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. 

 

 

 

์—ฌ๋Ÿฌ๊ฐœ์˜ ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›์„๋•Œ
a,b,c = map(int,input().split())

์ž…๋ ฅ ์˜ˆ : 1 9 983 
# ๊ฐ๊ฐ 1, 9 , 983์„ a,b,c์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

 

 

 


 

 

* rstrip, lstrip, strip

rstrip() = right ์˜ค๋ฅธ์ชฝ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.   - ์—ฌ๋Ÿฌ์ค„์˜ ์ž…๋ ฅ์„ ๋ฐ›์„๋•Œ, \n๋„ ๊ฐ™์ด ๋“ค์–ด์˜ค๊ฒŒ ๋˜๋Š”๋ฐ ์ด๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ํŽธ๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

lstrip() = left ์™ผ์ชฝ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.

strip() = ์–‘์ชฝ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. 

 

 

 

์—ฌ๋Ÿฌ์ค„์˜ ์ž…๋ ฅ์„ ๋ฐ›์•„ ๋ฆฌ์ŠคํŠธ๋กœ ์ €์žฅ
arr = []
while(True):
	arr.append(sys.stdin.readline().rstrip())
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.