๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿš€ Development/Python

[Python] ๋ฆฌ์ŠคํŠธ, ๋‹ค์ค‘ ๋ฐฐ์—ด ์ •๋ ฌ ๋ฐฉ๋ฒ• (์˜ˆ์ œ ํฌํ•จ)

by Jay Din 2024. 9. 2.
728x90
๋ฐ˜์‘ํ˜•

 

1. ๊ธฐ๋ณธ์ ์ธ ๋ฆฌ์ŠคํŠธ ์ •๋ ฌ

๋ฆฌ์ŠคํŠธ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋งค์šฐ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค.

Python์˜ ๋‚ด์žฅ ํ•จ์ˆ˜์ธ sorted() ๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ๊ฐ์ฒด์˜ sort() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

# ์˜ˆ์ œ ๋ฆฌ์ŠคํŠธ
numbers = [5, 3, 8, 6, 7, 2]

# sorted() ์‚ฌ์šฉ
sorted_numbers = sorted(numbers)
print(sorted_numbers)  # [2, 3, 5, 6, 7, 8]

# ์›๋ž˜ ๋ฆฌ์ŠคํŠธ๋Š” ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
print(numbers)  # [5, 3, 8, 6, 7, 2]

# sort() ์‚ฌ์šฉ
numbers.sort()
print(numbers)  # [2, 3, 5, 6, 7, 8]

 

2. ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ

์˜ค๋ฆ„์ฐจ์ˆœ์ด ์•„๋‹Œ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋ ค๋ฉด reverse=True ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

# ์˜ˆ์ œ ๋ฆฌ์ŠคํŠธ
numbers = [5, 3, 8, 6, 7, 2]

# sorted() ์‚ฌ์šฉ (๋‚ด๋ฆผ์ฐจ์ˆœ)
sorted_numbers = sorted(numbers, reverse=True)
print(sorted_numbers)  # [8, 7, 6, 5, 3, 2]

# sort() ์‚ฌ์šฉ (๋‚ด๋ฆผ์ฐจ์ˆœ)
numbers.sort(reverse=True)
print(numbers)  # [8, 7, 6, 5, 3, 2]

 

3. key ๋ฅผ ์‚ฌ์šฉํ•œ ์ •๋ ฌ

์ •๋ ฌ ๊ธฐ์ค€์„ ์ปค์Šคํ„ฐ๋งˆ์ด์ง•ํ•  ๋•Œ key ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฌธ์ž์—ด ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ธธ์ด์— ๋”ฐ๋ผ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ€ ๊ฐ™์œผ๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์ „์ˆœ(์•ŒํŒŒ๋ฒณ ์ˆœ)์œผ๋กœ ์ •๋ ฌ๋ฉ๋‹ˆ๋‹ค.

์ด๋Š” Python์˜ sorted() ํ•จ์ˆ˜์™€ sort() ํ•จ์ˆ˜๊ฐ€ ์•ˆ์ • ์ •๋ ฌ(stable sort) ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์•ˆ์ • ์ •๋ ฌ์€ ์ •๋ ฌ ๊ธฐ์ค€์ด ๊ฐ™์€ ์š”์†Œ๋“ค์ด ์›๋ž˜์˜ ์ˆœ์„œ๋ฅผ ์œ ์ง€ํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

# ์˜ˆ์ œ ๋ฌธ์ž์—ด ๋ฆฌ์ŠคํŠธ
words = ["apple", "banana", "cherry", "date"]

# ๋ฌธ์ž์—ด ๊ธธ์ด ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ
sorted_words = sorted(words, key=len)
print(sorted_words)  # ['date', 'apple', 'cherry', 'banana']

# sort() ์‚ฌ์šฉ
words.sort(key=len)
print(words)  # ['date', 'apple', 'cherry', 'banana']

 

4. ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ (๋‹ค์ค‘ ๋ฐฐ์—ด) ์ •๋ ฌ

๋ฆฌ์ŠคํŠธ ์•ˆ์— ๋ฆฌ์ŠคํŠธ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ, ํŠน์ • ์š”์†Œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ์•„๋ž˜์™€ ๊ฐ™์€ 2์ฐจ์› ๋ฆฌ์ŠคํŠธ๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

# ์˜ˆ์ œ ๋‹ค์ค‘ ๋ฐฐ์—ด
pairs = [[1, 3], [4, 1], [2, 2], [3, 4]]

# ๋‘ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ
sorted_pairs = sorted(pairs, key=lambda x: x[1])
print(sorted_pairs)  # [[4, 1], [2, 2], [1, 3], [3, 4]]

# ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ
pairs.sort(key=lambda x: x[0])
print(pairs)  # [[1, 3], [2, 2], [3, 4], [4, 1]]

 

5. ๋”•์…”๋„ˆ๋ฆฌ ์ •๋ ฌ

ํŒŒ์ด์ฌ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ํ‚ค ๋˜๋Š” ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋”•์…”๋„ˆ๋ฆฌ๋Š” ์ˆœ์„œ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์—, ์ •๋ ฌ๋œ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

5.1 ํ‚ค ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ

# ์˜ˆ์ œ ๋”•์…”๋„ˆ๋ฆฌ
student_scores = {'John': 88, 'Emma': 92, 'Sam': 75, 'Oliver': 95}

# ํ‚ค ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ
sorted_by_keys = sorted(student_scores)
print(sorted_by_keys)  # ['Emma', 'John', 'Oliver', 'Sam']

# ํ‚ค์™€ ๊ฐ’์„ ๋ชจ๋‘ ํฌํ•จํ•˜์—ฌ ์ •๋ ฌ
sorted_by_keys = sorted(student_scores.items())
print(sorted_by_keys)  # [('Emma', 92), ('John', 88), ('Oliver', 95), ('Sam', 75)]

 

5.2 ๊ฐ’ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ

# ๊ฐ’ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ
sorted_by_values = sorted(student_scores.items(), key=lambda x: x[1])
print(sorted_by_values)  # [('Sam', 75), ('John', 88), ('Emma', 92), ('Oliver', 95)]

 

5.3 ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ

๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋ ค๋ฉด reverse=True ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

# ๊ฐ’ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
sorted_by_values_desc = sorted(student_scores.items(), key=lambda x: x[1], reverse=True)
print(sorted_by_values_desc)  # [('Oliver', 95), ('Emma', 92), ('John', 88), ('Sam', 75)]

 

 

728x90
๋ฐ˜์‘ํ˜•

'๐Ÿš€ Development > Python' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Python] 2์ฐจ์›” ๋ฐฐ์—ด ์„ ์–ธ ๋ฐฉ๋ฒ•  (0) 2023.09.20