๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Python/๋ฐฑ์ค€

๋ฐฑ์ค€ 11650๋ฒˆ : ์ขŒํ‘œ ์ •๋ ฌํ•˜๊ธฐ (python)

by coderSohyun 2023. 5. 19.

 

 

 

 

๋ฌธ์ œ๋ฅผ ํ†ตํ•ด์„œ ์•Œ ์ˆ˜ ์žˆ๋Š” ์  :

1. 2์ฐจ์› ํ‰๋ฉด ์œ„์˜ ์  N๊ฐœ → 2์ฐจ์› ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•ด๋ณผ๊นŒ?

2. ์ขŒํ‘œ๋ฅผ x์ขŒํ‘œ๊ฐ€ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์œผ๋กœ

3. x์ขŒํ‘œ๊ฐ€ ๊ฐ™์œผ๋ฉด, y์ขŒํ‘œ๊ฐ€ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์œผ๋กœ 

4. ์ •๋ ฌ → sorted(), sort() ํ•จ์ˆ˜ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์Œ

 

ํ•„์š”ํ•œ ์ž…๋ ฅ๊ฐ’ :

1. ์ฒซ์งธ ์ค„ : ์ ์˜ ๊ฐœ์ˆ˜ N

2. ๋‘˜์งธ ์ค„ ~ : i๋ฒˆ์ ์˜ ์œ„์น˜ xi์™€ yi (์œ„์น˜๊ฐ€ ๊ฐ™์€ ๋‘ ์ ์€ ์—†๋‹ค)

     → ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ ์ž…๋ ฅ ๊ฐ’์„ ๋ฐ›๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ๋ฐ˜๋ณต๋ฌธ ๋Œ๋ ค์•ผ๋จ ํŒŒ์•… ๊ฐ€๋Šฅ

 

์ถœ๋ ฅ๊ฐ’ :

1. ์ฒซ์งธ ์ค„ ~ N๊ฐœ์˜ ์ค„์— ์ ์„ ์ •๋ ฌํ•œ ๊ฒฐ๊ณผ

→ (ํ•œ ์ค„๋กœ ๋ฐฐ์—ด์„ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๊ณ )

      ๊ฐ i๋ฒˆ์งธ ๋ฐฐ์—ด์˜ ์š”์†Œ๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ์ถœ๋ ฅํ•ด์•ผํ•จ์„ ๋ณด๊ณ  ์ด ๋˜ํ•œ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ ค์„œ ์ถœ๋ ฅ์„ ํ•ด์•ผํ•˜๋Š”๊ตฌ๋‚˜ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์Œ

 

 

 

๐Ÿ‘‰ ํŒŒ์•…ํ•œ ๊ฒƒ์„ ๋ฐ”ํƒ•์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋ณด์ž!

STEP 1 >

๋งŽ์€ ๊ฐ’์„ ์ž…๋ ฅ ๋ฐ›๊ณ  ์žˆ์œผ๋ฏ€๋กœ

์‹œ๊ฐ„ ์ดˆ๊ณผ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ,

์•„๋ž˜์™€ ๊ฐ™์ด import sys ๋ฅผ ํ†ตํ•ด์„œ input()์„ ๋ฐ›๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค.

import sys

N = int(sys.stdin.readline().rstrip())

x, y = map(int, sys.stdin.readline().rstrip().split())

 

STEP 2 >

์ด๋•Œ, xi์™€ yi๋Š” ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด์„œ ์ž…๋ ฅ์„ ๋ฐ›์•„์•ผ ๋˜๊ธฐ ๋•Œ๋ฌธ์—

N์˜ ํฌ๊ธฐ ๋งŒํผ์˜ for๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ xi์™€ yi์˜ ๊ฐ’์„ ์ž…๋ ฅ ๋ฐ›๋Š”๋‹ค.

import sys

N = int(sys.stdin.readline().rstrip())

for i in range(N):
    [x, y] = map(int, input().split())

 

STEP 3 >

xi์™€ yi์˜ ์ ๋“ค์€ 2์ฐจ์› ๋ฐฐ์—ด ์•ˆ์— ์žˆ์„ ์š”์†Œ๋“ค์ด๋ฏ€๋กœ

array ๋ฐฐ์—ด์„ ํ•˜๋‚˜ ๋งŒ๋“ค๊ณ 

๊ฐ๊ฐ์˜ xi์™€ yi ์ ๋“ค์„ ํ•˜๋‚˜์˜ ์š”์†Œ๋กœ appendํ•ด์ค€๋‹ค. 

import sys

N = int(sys.stdin.readline().rstrip())

array = []
for i in range(N):
    x, y = map(int, sys.stdin.readline().rstrip().split())
    array.append([x,y])

 

STEP 4 >

์ดํ›„, sorted() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ๋ฐฐ์—ด array๋ฅผ

x์ขŒํ‘œ๊ฐ€ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์œผ๋กœ x์ขŒํ‘œ๊ฐ€ ๊ฐ™์œผ๋ฉด, y์ขŒํ‘œ๊ฐ€ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์ธ

๋ฐฐ์—ด s_array๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค.

import sys

N = int(sys.stdin.readline().rstrip())

array = []
for i in range(N):
    x, y = map(int, sys.stdin.readline().rstrip().split())
    array.append([x,y])

s_array = sorted(array)

 

STEP 5 >

๋งˆ์ง€๋ง‰์œผ๋กœ, ์ฃผ์–ด์ง„ ์ถœ๋ ฅ๊ฐ’์ฒ˜๋Ÿผ ์ถœ๋ ฅ๋˜๊ฒŒ

for๋ฌธ์„ N์˜ ํฌ๊ธฐ๋งŒํผ ๋Œ๋ ค์„œ

s_array ๋ฐฐ์—ด์— ์ €์žฅ๋œ i๋ฒˆ์งธ์˜ xi์™€ yi ๊ฐ’์„ ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ๋œ๋‹ค. 

import sys

N = int(sys.stdin.readline().rstrip())

array = []
for i in range(N):
    x, y = map(int, sys.stdin.readline().rstrip().split())
    array.append([x,y])

s_array = sorted(array)

for i in range(N):
    print(s_array[i][0], s_array[i][1])

 

 

 

๐Ÿ‘‰ ์ตœ์ข… ์ฝ”๋“œ๋Š” ์ด๋Ÿฌํ•˜๋‹ค

import sys

N = int(sys.stdin.readline().rstrip())

array = []
for i in range(N):
    x, y = map(int, sys.stdin.readline().rstrip().split())
    array.append([x,y])

s_array = sorted(array)

for i in range(N):
    print(s_array[i][0], s_array[i][1])

 

 

 

๐Ÿ‘‰ Q&A

๐Ÿ“Œ ์™œ sorted() ํ•จ์ˆ˜์—์„œ lambda๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜๋ƒ?

#ex1) 
array.sort(key = lambda x: (x[0],x[1]))

#ex2)
arr = sorted(arr, key= lambda y: y[1])
arr = sorted(arr, key= lambda x: x[0])

#ex3) 
arr.sort(key=lambda a : (a[0], a[1]))

#my_code
s_array = sorted(array)

 

 

 

๐Ÿ‘‰ ์•Œ๋ฉด ์ข‹์€ ์ฝ”๋“œ๋“ค

๐Ÿ“์ด๋ ‡๊ฒŒ ๋ฏธ๋ฆฌ ์“ฐ๊ธฐ๋งŒ ํ•˜๋ฉด, ๊ทธ ์ดํ›„์— ๋ฐ”๋กœ input() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ. 

import sys

input = sys.stdin.readline

N = int(input())

#N = int(sys.stdin.readline().rstrip())

(์ฐธ๊ณ ) https://puleugo.tistory.com/42

 

๐Ÿ“์ด๋Ÿฐ์‹์œผ๋กœ ์ฝ”๋“œ๋ฅผ ๋‹จ์ถ• ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Œ. 

array = []
for _ in range(N) :
    array.append(list(map(int, input().split())))
    
#my_code
#array = []
#for i in range(N):
#    x, y = map(int, sys.stdin.readline().rstrip().split())
#    array.append([x,y])

 

๐Ÿ“*๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ๋ฆฌ์ŠคํŠธ ์•ˆ์˜ ์š”์†Œ๋“ค๋งŒ ๊บผ๋‚ด์„œ ํ•œ ์ค„์— ํ•œ ๋ฒˆ์— ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค.

#ex1)
for x in arr:
    print(*x)
    
#ex2)    
for i in range(0, N):
    print(*arr[i])

#my_code
#for i in range(N):
#    print(s_array[i][0], s_array[i][1])

(์ฐธ๊ณ ) https://yeomss.tistory.com/160

'Python > ๋ฐฑ์ค€' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

๋ฐฑ์ค€ 2293๋ฒˆ : ๋™์ „ 1  (0) 2023.08.17