๐ง Algorithm/Baekjoon๐ก
๐กโจ์ค๊ฐ์ ๊ฒ : split
4:Bee
2023. 12. 26. 19:51
728x90
split()ํจ์๋ ์ ํํ ๋ฌด์์ธ๊ฐ.
split์ ๋ฌธ์์ด์ ํน์ ๊ตฌ๋ถ์๋ฅผ ๊ธฐ์ค์ผ๋ก list๋ก ์ ํํด์ฃผ๋ ํจ์์ด๋ค. ์ฆ, input, ๋ฌธ์์ด์ list๋ก ๋ฐ์๊ณ ๊ทธ๊ฒ์ listํ์์ผ๋ก ์ ์๋๋ ๊ฒ์ด๋ค. ์์๋ฅผ ๋ณด๋ฉฐ ํด๋น ํจ์๋ฅผ ์ดํดํด๋ณด์
N = input().split()
print(N)
1 2 3 4
['1', '2', '3', '4']
['1', '2', '3', '4']
์ด์ ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ ๋์จ๋ค. ๋ค๋ฅธ ์์๋ ํ๋ฒ ์ดํด๋ณด์.
n = "a.b.c.d.e.f"
sp0 = n.split()
sp1 = n.split('.')
print(f"sp0 =>{sp0}")
print(f"sp0 =>{sp1}")
n = "a b c d e f"
sp2 = n.split(sep=' ')
print(f"sp0 =>{sp2}")
sp0 =>['a.b.c.d.e.f']
sp1 =>['a', 'b', 'c', 'd', 'e', 'f']
sp2 =>['a', 'b', 'c', 'd', 'e', 'f']
sp1 =>['a', 'b', 'c', 'd', 'e', 'f']
sp2 =>['a', 'b', 'c', 'd', 'e', 'f']
split์ ๋ฌธ์์ด์ ๊ตฌ๋ถํ๋ ํจ์๋ผ ์ค๋ช ํ๋ค. sep๋ ์ผ๋ฐ ์ ์ธ split()์ ๊ดํธ์์ ๋ฃ๋ ๊ธฐํธ์ ๋ค๋ฅธ ๊ฒ์ด ์๋ค. ์ฆ sp2์ ๋ณ์์ n.split(' ')๋ผ๊ณ ์์ฑํด๋ ๊ฐ์ ๊ธฐ๋ฅ์ ์ํํ๋ฉฐ ๊ฒฐ๊ณผ๋ ๋ค๋ฅด์ง ์๊ณ ๊ฐ๋ค.
728x90