[Python/ํŒŒ์ด์ฌ] [๐Ÿฅˆ5] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 10814 - ๋‚˜์ด์ˆœ ์ •๋ ฌ

keyneneยท2022๋…„ 10์›” 13์ผ
0

Python

๋ชฉ๋ก ๋ณด๊ธฐ
3/26
post-custom-banner

๐Ÿ“–[Python/ํŒŒ์ด์ฌ][๐Ÿฅˆ5] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 10814 - ๋‚˜์ด์ˆœ ์ •๋ ฌ

๐Ÿ“œ๋ฌธ์ œ


๐Ÿ“•ํ’€์ด๋ฐฉํ–ฅ

์ž…๋ ฅ์„ ๋ฐ›์•„ 2์ฐจ์›๋ฐฐ์—ด์— ๋„ฃ๊ณ  ์ฒซ๋ฒˆ์งธ์—ด(๋‚˜์ด)๋งŒ ์ •๋ ฌํ•˜์ž.
(๋‚˜์ด๊ฐ€ ๊ฐ™์„ ๋•Œ๋Š” ๋จผ์ € ๊ฐ€์ž…ํ•œ ์ˆœ์„œ = ์ž…๋ ฅํ•œ ์ˆœ์„œ์ด๋ฏ€๋กœ ๋‘๋ฒˆ์งธ์—ด(์ด๋ฆ„)์€ ์ •๋ ฌํ•˜์ง€ ์•Š์Œ)
์ •๋ ฌํ•œ ๋ฌธ์ž์—ด์€ format์„ ์ด์šฉํ•˜์—ฌ ํ˜•์‹์— ๋งž๊ฒŒ ์ถœ๋ ฅํ•ด๋ณด์ž


๐Ÿ“์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„์ˆœ์„œ

  1. tc(test case)๋งŒํผ a,b๋กœ ์ž…๋ ฅ๋ฐ›์•„ person์— 2์ฐจ์›๋ฐฐ์—ด ํ˜•ํƒœ๋กœ appendํ•˜๊ธฐ
  2. .sort(key=lambda x:x[0])๋ฅผ ์ด์šฉํ•˜์—ฌ ์›ํ•˜๋Š”ํ–‰๋งŒ ์ •๋ ฌํ•˜๊ธฐ
  3. .format()๋ฅผ ์ด์šฉํ•˜์—ฌ ์›ํ•˜๋Š” ํ˜•ํƒœ๋กœ ์ถœ๋ ฅํ•˜๊ธฐ

๐Ÿ’ป๊ฒฐ๊ณผ์ฝ”๋“œ

import sys

tc = int(input())
person = []

for _ in range(tc):
    a,b = sys.stdin.readline().split()
    person.append([int(a),b]) 
    #[[a,b],[a,b]...(tc๋งŒํผ)]ํ˜•ํƒœ๋กœ ์ €์žฅ๋จ

#x[0] ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ์ •๋ ฌ
person.sort(key=lambda x:x[0]) 

for i in range(len(person)):
    print("{} {}".format(person[i][0], person[i][1]))

โœ๏ธ1. person์— 2์ฐจ์› ๋ฐฐ์—ด๋กœ ์ €์žฅ

import sys

tc = int(input())
person = []

for _ in range(tc):
    a,b = sys.stdin.readline().split()
    person.append([int(a),b])
    
#for _ in range(input()): ํ•ด๋„ ๋ฌด๊ด€ํ•จ    
tc ์ˆ˜๋งŒํผ a,b๋ฅผ split()๊ณต๋ฐฑ ๊ธฐ์ค€์œผ๋กœ ์ž…๋ ฅ๋ฐ›๊ณ 
person์ด๋ผ๋Š” ๋นˆ ๋ฆฌ์ŠคํŠธ์— append
ex) tc=2์ด๋ฉด
    [[a,b], [a,b]] ์ด๋ ‡๊ฒŒ ์ž…๋ ฅํ•œ๋Œ€๋กœ ์ €์žฅ๋  ๊ฒƒ์ž„

โœ๏ธ2. sort(key=lambda x:x[0]) ์ด์šฉํ•˜์—ฌ ์›ํ•˜๋Š” ํ–‰ ์ •๋ ฌ

person.sort(key=lambda x:x[0])


โ€ปsort์˜ key์— lambda๋ฅผ ์ด์šฉํ•œ ์—ด ์ •๋ ฌ
-์‚ฌ์šฉ๋ฐฉ๋ฒ•
  .sort(key=lambda x: /*์›ํ•˜๋Š” ์˜ต์…˜๋Œ€๋กœ ์ •๋ ฌ*/ )
-์ข…๋ฅ˜
  1) ์ฒซ ๋ฒˆ์งธ ์—ด ๊ธฐ์ค€ ์ •๋ ฌ (์˜ค๋ฆ„์ฐจ์ˆœ)
     .sort(key=lambda x:x[0])
  2) ๋‘ ๋ฒˆ์งธ ์—ด ๊ธฐ์ค€ ์ •๋ ฌ (์˜ค๋ฆ„์ฐจ์ˆœ)
     .sort(key=lambda x:x[1])
  3) ์ฒซ ๋ฒˆ์งธ ์—ด ๊ธฐ์ค€ ์ •๋ ฌ, ๊ฐ™์œผ๋ฉด ๋‘ ๋ฒˆ์งธ ์—ด ๊ธฐ์ค€ ์ •๋ ฌ
     .sort(key=lambda x:(x[0], x[1]))
  4) ์ฒซ ๋ฒˆ์งธ ์—ด ๊ธฐ์ค€ ์ •๋ ฌ (๋‚ด๋ฆผ์ฐจ์ˆœ)
     .sort(key=lambda x:-x[0])

โœ๏ธ3. format() ์‚ฌ์šฉํ•˜์—ฌ ์›ํ•˜๋Š”๋Œ€๋กœ ์ถœ๋ ฅ

for i in range(len(person)):
    print("{} {}".format(person[i][0], person[i][1]))


โ€ปformat ์ด์šฉํ•˜์—ฌ ์›ํ•˜๋Š” ํ˜•์‹๋Œ€๋กœ ์ถœ๋ ฅ
-์‚ฌ์šฉ๋ฐฉ๋ฒ•
  print('{1} {0}'.format(์š”์†Œ1, ์š”์†Œ0)) #{์š”์†Œ1} {์š”์†Œ0} ํ˜•์‹์œผ๋กœ ์ถœ๋ ฅํ•ด์คŒ
  ๐Ÿ‘‰๐Ÿป"{}(๋นˆ์ค‘๊ด„ํ˜ธ)"๋ฉด ์š”์†Œ ์ˆœ์„œ๋Œ€๋กœ, "{0},{0}"๋ฉด ๊ฐ™์€ ์š”์†Œ 2๋ฒˆ ์ถœ๋ ฅ
-๋‹ค๋ฅธ๋ฐฉ๋ฒ•
  s = '{] {}'.format(์š”์†Œ1, ์š”์†Œ2)   
  print(s)  # ์ด๊ฒƒ๋„ ๊ฐ™์€๊ฒฐ๊ณผ
  ๐Ÿ‘‰๐Ÿป๋ฌผ๋ก , {}๊ฐœ์ˆ˜(์š”์†Œ์˜๊ฐœ์ˆ˜)๋Š” ์กฐ์ ˆ๊ฐ€๋Šฅํ•จ

๐Ÿ“š๋‹ค๋ฅธ ์•Œ๊ณ ๋ฆฌ์ฆ˜

์ถœ๋ ฅ๋ถ€๋ถ„ format๋Œ€์‹  ๋‹ค๋ฅธํ˜•์‹์œผ๋กœ ๊ตฌํ˜„ํ•ด๋ณด์•˜์Œ

import sys

tc = int(input())
person = []

for _ in range(tc):
    a,b = sys.stdin.readline().split()
    person.append([int(a),b]) 

person.sort(key=lambda x:x[0]) 

############์—ฌ๊ธฐ๋งŒ ๋‹ฌ๋ผ์ง##############
for i in person:
  print(*i, sep=" ")
print์˜ ์ถœ๋ ฅํ•  ๋ณ€์ˆ˜(i)์˜ ์•ž์— *์„ ๋ถ™์ด๋ฉด listํ˜•์‹์ธ []๋ฅผ ๋–ผ๊ณ  ์ถœ๋ ฅํ•ด์คŒ
profile
keynene
post-custom-banner

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