Python 리스트에서 상위 N 개 요소 찾는 방법

PythonBeginner
지금 연습하기

소개

Python 리스트는 항목의 정렬된 컬렉션을 저장하는 기본적인 데이터 구조입니다. 리스트에서 상위 N 개 요소를 찾는 것은 많은 데이터 분석 및 처리 작업에 필수적인 기술입니다. 최고 점수를 받은 학생, 가장 인기 있는 제품 또는 데이터 세트에서 가장 큰 값을 식별해야 하는 경우, 이러한 요소를 효율적으로 추출하는 방법을 아는 것은 매우 중요합니다.

이 Lab 에서는 Python 리스트에서 상위 N 개 요소를 찾는 다양한 방법을 배우게 됩니다. 내장 함수와 특수 모듈을 모두 살펴보고, 이러한 기술을 실제 시나리오에 적용하는 방법을 보게 될 것입니다. 이 Lab 을 마치면 데이터 처리 능력을 향상시킬 수 있는 Python 리스트 조작 기술에 대한 확실한 이해를 갖게 될 것입니다.

Python 리스트 생성 및 이해

이 첫 번째 단계에서는 Python 리스트의 기본 사항과 이를 생성하는 방법을 배우게 됩니다. 리스트는 숫자, 문자열, 심지어 다른 리스트를 포함하여 다양한 유형의 요소를 저장할 수 있는 다재다능한 데이터 구조입니다.

리스트 생성

숫자의 간단한 리스트를 생성하는 것으로 시작해 보겠습니다. WebIDE 에서 /home/labex/project 디렉토리에 list_basics.py라는 새 Python 파일을 생성합니다.

  1. "File" 메뉴 (또는 사이드바의 파일 아이콘) 를 클릭합니다.
  2. "New File"을 선택합니다.
  3. list_basics.py 이름을 입력합니다.
  4. 다음 코드를 파일에 추가합니다.
## Creating a list of numbers
numbers = [15, 7, 27, 9, 42, 8, 31, 17]

## Print the original list
print("Original list:", numbers)

## Print the length of the list
print("List length:", len(numbers))

## Access elements by index
print("First element:", numbers[0])
print("Last element:", numbers[-1])

## Slicing a list
print("First three elements:", numbers[:3])
print("Last three elements:", numbers[-3:])

이제 코드를 실행하여 출력을 확인합니다.

  1. WebIDE 에서 터미널을 엽니다 (아직 열려 있지 않은 경우).
  2. 다음 명령으로 스크립트를 실행합니다.
python3 list_basics.py

다음과 유사한 출력이 표시되어야 합니다.

Original list: [15, 7, 27, 9, 42, 8, 31, 17]
List length: 8
First element: 15
Last element: 17
First three elements: [15, 7, 27]
Last three elements: [8, 31, 17]

리스트 수정

이제 리스트를 수정해 보겠습니다. 다음 코드를 list_basics.py에 추가합니다.

## Adding elements to a list
numbers.append(50)
print("After append:", numbers)

## Inserting an element at a specific position
numbers.insert(2, 99)
print("After insert:", numbers)

## Removing elements
numbers.remove(9)  ## Remove by value
print("After remove:", numbers)

popped_value = numbers.pop()  ## Remove and return the last element
print("Popped value:", popped_value)
print("After pop:", numbers)

스크립트를 다시 실행하여 리스트가 어떻게 변경되는지 확인합니다.

python3 list_basics.py

이제 출력에 다음이 포함되어야 합니다.

After append: [15, 7, 27, 9, 42, 8, 31, 17, 50]
After insert: [15, 7, 99, 27, 9, 42, 8, 31, 17, 50]
After remove: [15, 7, 99, 27, 42, 8, 31, 17, 50]
Popped value: 50
After pop: [15, 7, 99, 27, 42, 8, 31, 17]

이것은 Python 리스트가 생성 후 변경 가능 (mutable) 하며, 이는 데이터 조작에 유연하게 해주는 중요한 특성임을 보여줍니다.

Python 에서 리스트 정렬하기

상위 N 개 요소를 찾기 전에 Python 에서 리스트를 정렬하는 방법을 이해하는 것이 중요합니다. 정렬은 요소를 특정 순서, 일반적으로 오름차순 (가장 작은 것에서 가장 큰 것) 또는 내림차순 (가장 큰 것에서 가장 작은 것) 으로 정렬합니다.

sorted() 를 사용한 기본 정렬

프로젝트 디렉토리에 sorting_lists.py라는 새 파일을 생성하고 다음 코드를 추가합니다.

## Creating a list of numbers
scores = [85, 92, 78, 91, 88, 76, 94, 87]

## Sort in ascending order (default)
sorted_scores = sorted(scores)
print("Original scores:", scores)
print("Sorted scores (ascending):", sorted_scores)

## Sort in descending order
desc_scores = sorted(scores, reverse=True)
print("Sorted scores (descending):", desc_scores)

## Original list remains unchanged
print("Original scores after sorting:", scores)

스크립트를 실행하여 정렬된 리스트를 확인합니다.

python3 sorting_lists.py

다음과 유사한 출력이 표시되어야 합니다.

Original scores: [85, 92, 78, 91, 88, 76, 94, 87]
Sorted scores (ascending): [76, 78, 85, 87, 88, 91, 92, 94]
Sorted scores (descending): [94, 92, 91, 88, 87, 85, 78, 76]
Original scores after sorting: [85, 92, 78, 91, 88, 76, 94, 87]

sorted() 함수는 원본 리스트를 변경하지 않고 새로운 정렬된 리스트를 반환합니다.

sort() 메서드를 사용한 정렬

이제 리스트를 제자리에서 수정하는 sort() 메서드를 살펴보겠습니다. 다음 코드를 sorting_lists.py에 추가합니다.

## Creating another list
prices = [12.99, 8.50, 15.75, 9.99, 11.25]
print("\nOriginal prices:", prices)

## Sort the list in place (ascending)
prices.sort()
print("After sort() (ascending):", prices)

## Sort the list in place (descending)
prices.sort(reverse=True)
print("After sort(reverse=True) (descending):", prices)

스크립트를 다시 실행합니다.

python3 sorting_lists.py

추가 출력은 다음과 같아야 합니다.

Original prices: [12.99, 8.5, 15.75, 9.99, 11.25]
After sort() (ascending): [8.5, 9.99, 11.25, 12.99, 15.75]
After sort(reverse=True) (descending): [15.75, 12.99, 11.25, 9.99, 8.5]

사용자 지정 키를 사용한 정렬

키 함수를 사용하여 특정 기준에 따라 리스트를 정렬할 수도 있습니다. 다음 코드를 sorting_lists.py에 추가합니다.

## List of strings
names = ["Alice", "bob", "Charlie", "david", "Eva"]
print("\nOriginal names:", names)

## Sort alphabetically (case-sensitive)
print("Sorted alphabetically (case-sensitive):", sorted(names))

## Sort alphabetically (case-insensitive)
print("Sorted alphabetically (case-insensitive):", sorted(names, key=str.lower))

## List of tuples (name, age)
people = [("Alice", 25), ("Bob", 19), ("Charlie", 32), ("David", 22)]
print("\nOriginal people:", people)

## Sort by age (second element of each tuple)
print("Sorted by age:", sorted(people, key=lambda person: person[1]))

## Sort by name length
print("Sorted by name length:", sorted(people, key=lambda person: len(person[0])))

스크립트를 다시 한 번 실행합니다.

python3 sorting_lists.py

추가 출력은 사용자 지정 기준을 사용하여 정렬하는 방법을 보여줍니다.

Original names: ['Alice', 'bob', 'Charlie', 'david', 'Eva']
Sorted alphabetically (case-sensitive): ['Alice', 'Charlie', 'Eva', 'bob', 'david']
Sorted alphabetically (case-insensitive): ['Alice', 'bob', 'Charlie', 'david', 'Eva']

Original people: [('Alice', 25), ('Bob', 19), ('Charlie', 32), ('David', 22)]
Sorted by age: [('Bob', 19), ('David', 22), ('Alice', 25), ('Charlie', 32)]
Sorted by name length: [('Bob', 19), ('Alice', 25), ('David', 22), ('Charlie', 32)]

이러한 정렬 기술을 이해하는 것은 다음 단계에서 살펴볼 리스트에서 상위 N 개 요소를 찾는 데 필수적입니다.

sorted() 를 사용하여 상위 N 개 요소 찾기

이제 Python 리스트와 정렬을 이해했으므로 리스트에서 상위 N 개 요소를 찾는 데 집중해 보겠습니다. 가장 간단한 방법은 reverse=True 매개변수와 함께 sorted() 함수를 사용한 다음 결과를 슬라이싱하여 처음 N 개 요소를 가져오는 것입니다.

프로젝트 디렉토리에 top_n_sorted.py라는 새 파일을 생성하고 다음 코드를 추가합니다.

## Finding top N elements using sorted()

## Sample data: Student scores
student_scores = [85, 92, 78, 91, 88, 76, 94, 87, 89, 93]
print("Student scores:", student_scores)

## Find the top 3 scores
top_3_scores = sorted(student_scores, reverse=True)[:3]
print("Top 3 scores:", top_3_scores)

## Find the top 5 scores
top_5_scores = sorted(student_scores, reverse=True)[:5]
print("Top 5 scores:", top_5_scores)

스크립트를 실행하여 상위 N 개 요소를 확인합니다.

python3 top_n_sorted.py

다음과 유사한 출력이 표시되어야 합니다.

Student scores: [85, 92, 78, 91, 88, 76, 94, 87, 89, 93]
Top 3 scores: [94, 93, 92]
Top 5 scores: [94, 93, 92, 91, 89]

복잡한 데이터로 상위 N 개 요소 찾기

더 복잡한 데이터 구조로 작업하도록 예제를 확장해 보겠습니다. 다음 코드를 top_n_sorted.py에 추가합니다.

## Sample data: Product sales data (product name, units sold)
product_sales = [
    ("Product A", 1250),
    ("Product B", 870),
    ("Product C", 1100),
    ("Product D", 750),
    ("Product E", 940),
    ("Product F", 1300),
    ("Product G", 820),
    ("Product H", 980)
]
print("\nProduct sales:", product_sales)

## Find the top 3 best-selling products
## We sort based on the units sold (second element of each tuple)
top_3_products = sorted(product_sales, key=lambda x: x[1], reverse=True)[:3]
print("Top 3 best-selling products:")
for product, sales in top_3_products:
    print(f"  {product}: {sales} units")

## Sample data: Student records (name, scores in different subjects)
student_records = [
    {"name": "Alice", "scores": [92, 88, 95, 85]},
    {"name": "Bob", "scores": [78, 82, 79, 75]},
    {"name": "Charlie", "scores": [85, 90, 88, 92]},
    {"name": "Diana", "scores": [95, 97, 93, 90]},
    {"name": "Eva", "scores": [88, 84, 89, 86]}
]
print("\nStudent records:", student_records)

## Find the top 2 students based on average score
def average_score(student):
    return sum(student["scores"]) / len(student["scores"])

top_2_students = sorted(student_records, key=average_score, reverse=True)[:2]
print("Top 2 students by average score:")
for student in top_2_students:
    avg = average_score(student)
    print(f"  {student['name']}: {avg:.2f} average")

스크립트를 다시 실행합니다.

python3 top_n_sorted.py

추가 출력은 더 복잡한 데이터 구조에서 상위 N 개 요소를 찾는 방법을 보여줍니다.

Product sales: [('Product A', 1250), ('Product B', 870), ('Product C', 1100), ('Product D', 750), ('Product E', 940), ('Product F', 1300), ('Product G', 820), ('Product H', 980)]
Top 3 best-selling products:
  Product F: 1300 units
  Product A: 1250 units
  Product C: 1100 units

Student records: [{'name': 'Alice', 'scores': [92, 88, 95, 85]}, {'name': 'Bob', 'scores': [78, 82, 79, 75]}, {'name': 'Charlie', 'scores': [85, 90, 88, 92]}, {'name': 'Diana', 'scores': [95, 97, 93, 90]}, {'name': 'Eva', 'scores': [88, 84, 89, 86]}]
Top 2 students by average score:
  Diana: 93.75 average
  Alice: 90.00 average

슬라이싱과 함께 sorted() 함수는 리스트에서 상위 N 개 요소를 찾는 다목적 접근 방식입니다. 그러나 대규모 데이터 세트의 경우 더 효율적인 방법이 있으며, 다음 단계에서 살펴보겠습니다.

heapq 를 사용하여 상위 N 개 요소 찾기

sorted() 함수는 대부분의 경우에 잘 작동하지만, Python 의 heapq 모듈은 특히 대규모 데이터 세트의 경우 상위 N 개 요소를 찾는 데 더 효율적인 방법을 제공합니다. heapq 모듈은 힙 큐 알고리즘 (priority queue algorithm) 이라고도 하는 힙 큐 알고리즘을 구현합니다.

프로젝트 디렉토리에 top_n_heapq.py라는 새 파일을 생성하고 다음 코드를 추가합니다.

## Finding top N elements using heapq
import heapq

## Sample data: Student scores
student_scores = [85, 92, 78, 91, 88, 76, 94, 87, 89, 93]
print("Student scores:", student_scores)

## Find the top 3 scores using heapq.nlargest()
top_3_scores = heapq.nlargest(3, student_scores)
print("Top 3 scores (using heapq.nlargest()):", top_3_scores)

## Find the bottom 3 scores using heapq.nsmallest()
bottom_3_scores = heapq.nsmallest(3, student_scores)
print("Bottom 3 scores (using heapq.nsmallest()):", bottom_3_scores)

스크립트를 실행하여 heapq가 어떻게 작동하는지 확인합니다.

python3 top_n_heapq.py

다음과 유사한 출력이 표시되어야 합니다.

Student scores: [85, 92, 78, 91, 88, 76, 94, 87, 89, 93]
Top 3 scores (using heapq.nlargest()): [94, 93, 92]
Bottom 3 scores (using heapq.nsmallest()): [76, 78, 85]

복잡한 데이터로 heapq 사용하기

heapq 모듈은 키 함수를 지정하여 복잡한 데이터 구조에서도 작동할 수 있습니다. 다음 코드를 top_n_heapq.py에 추가합니다.

## Sample data: Product sales data (product name, units sold)
product_sales = [
    ("Product A", 1250),
    ("Product B", 870),
    ("Product C", 1100),
    ("Product D", 750),
    ("Product E", 940),
    ("Product F", 1300),
    ("Product G", 820),
    ("Product H", 980)
]
print("\nProduct sales:", product_sales)

## Find the top 3 best-selling products using heapq.nlargest()
top_3_products = heapq.nlargest(3, product_sales, key=lambda x: x[1])
print("Top 3 best-selling products:")
for product, sales in top_3_products:
    print(f"  {product}: {sales} units")

## Sample data: Student records (name, scores in different subjects)
student_records = [
    {"name": "Alice", "scores": [92, 88, 95, 85]},
    {"name": "Bob", "scores": [78, 82, 79, 75]},
    {"name": "Charlie", "scores": [85, 90, 88, 92]},
    {"name": "Diana", "scores": [95, 97, 93, 90]},
    {"name": "Eva", "scores": [88, 84, 89, 86]}
]
print("\nStudent records:", student_records)

## Find the top 2 students based on average score using heapq.nlargest()
def average_score(student):
    return sum(student["scores"]) / len(student["scores"])

top_2_students = heapq.nlargest(2, student_records, key=average_score)
print("Top 2 students by average score:")
for student in top_2_students:
    avg = average_score(student)
    print(f"  {student['name']}: {avg:.2f} average")

스크립트를 다시 실행합니다.

python3 top_n_heapq.py

추가 출력은 복잡한 데이터 구조에서 heapq를 사용하는 방법을 보여줍니다.

Product sales: [('Product A', 1250), ('Product B', 870), ('Product C', 1100), ('Product D', 750), ('Product E', 940), ('Product F', 1300), ('Product G', 820), ('Product H', 980)]
Top 3 best-selling products:
  Product F: 1300 units
  Product A: 1250 units
  Product C: 1100 units

Student records: [{'name': 'Alice', 'scores': [92, 88, 95, 85]}, {'name': 'Bob', 'scores': [78, 82, 79, 75]}, {'name': 'Charlie', 'scores': [85, 90, 88, 92]}, {'name': 'Diana', 'scores': [95, 97, 93, 90]}, {'name': 'Eva', 'scores': [88, 84, 89, 86]}]
Top 2 students by average score:
  Diana: 93.75 average
  Alice: 90.00 average

성능 비교: sorted() vs heapq

대규모 리스트에서 상위 N 개 요소를 찾는 데 sorted()heapq의 성능을 비교해 보겠습니다. 다음 코드를 top_n_heapq.py에 추가합니다.

import time
import random

## Generate a large list of random numbers
print("\nComparing performance with a large list:")
large_list = [random.randint(1, 1000000) for _ in range(100000)]
print(f"List size: {len(large_list)} elements")

## Time the sorted() approach
start_time = time.time()
top_10_sorted = sorted(large_list, reverse=True)[:10]
sorted_time = time.time() - start_time
print(f"Time taken with sorted(): {sorted_time:.6f} seconds")

## Time the heapq approach
start_time = time.time()
top_10_heapq = heapq.nlargest(10, large_list)
heapq_time = time.time() - start_time
print(f"Time taken with heapq.nlargest(): {heapq_time:.6f} seconds")
print(f"Performance gain: {sorted_time / heapq_time:.2f}x faster")

## Verify both methods give the same result
print("Both methods give the same result:", sorted(top_10_sorted) == sorted(top_10_heapq))

스크립트를 마지막으로 실행합니다.

python3 top_n_heapq.py

추가 출력은 대규모 데이터 세트에 heapq를 사용하는 성능 이점을 보여줍니다.

Comparing performance with a large list:
List size: 100000 elements
Time taken with sorted(): 0.034625 seconds
Time taken with heapq.nlargest(): 0.008976 seconds
Performance gain: 3.86x faster
Both methods give the same result: True

대규모 리스트에서 소수의 상위 N 개 요소를 찾는 경우 heapq.nlargest()가 전체 리스트를 정렬한 다음 슬라이싱하는 것보다 더 효율적입니다. 이는 heapq.nlargest()가 크기 N 의 힙을 유지하는 반면 sorted()는 전체 리스트를 정렬하기 때문입니다.

실제 사용 사례

이제 Python 리스트에서 상위 N 개 요소를 찾는 다양한 기술을 배웠으므로 실제 사용 사례를 살펴보겠습니다. 이 단계에서는 이러한 개념을 실제 시나리오에 적용하는 보다 포괄적인 스크립트를 만들 것입니다.

프로젝트 디렉토리에 practical_applications.py라는 새 파일을 생성하고 다음 코드를 추가합니다.

## Real-world applications of finding top N elements
import heapq
from datetime import datetime

print("PRACTICAL APPLICATIONS OF FINDING TOP N ELEMENTS IN PYTHON LISTS\n")

## Application 1: E-commerce - Analyzing Product Sales
print("APPLICATION 1: E-COMMERCE - ANALYZING PRODUCT SALES")
print("==================================================")

## Sample product sales data (product_id, product_name, units_sold, price)
product_sales = [
    (101, "Smartphone X", 1250, 899.99),
    (102, "Wireless Earbuds", 2100, 129.99),
    (103, "Laptop Pro", 890, 1299.99),
    (104, "Smart Watch", 1450, 249.99),
    (105, "Tablet Mini", 1050, 399.99),
    (106, "Bluetooth Speaker", 1750, 79.99),
    (107, "Gaming Console", 780, 499.99),
    (108, "Digital Camera", 550, 349.99),
    (109, "Power Bank", 1900, 49.99),
    (110, "Fitness Tracker", 1350, 129.99)
]

## Find top 3 products by units sold
top_sold_products = heapq.nlargest(3, product_sales, key=lambda x: x[2])
print("\nTop 3 Best-Selling Products (by units sold):")
for product in top_sold_products:
    print(f"  {product[1]}: {product[2]} units sold at ${product[3]}")

## Find top 3 products by revenue (units_sold * price)
top_revenue_products = heapq.nlargest(3, product_sales, key=lambda x: x[2] * x[3])
print("\nTop 3 Products by Revenue:")
for product in top_revenue_products:
    revenue = product[2] * product[3]
    print(f"  {product[1]}: ${revenue:,.2f} revenue ({product[2]} units at ${product[3]})")

## Application 2: Data Analysis - Temperature Monitoring
print("\n\nAPPLICATION 2: DATA ANALYSIS - TEMPERATURE MONITORING")
print("====================================================")

## Sample temperature data (date, city, temperature)
temperature_data = [
    ("2023-06-15", "New York", 32.5),
    ("2023-06-15", "Los Angeles", 28.3),
    ("2023-06-15", "Chicago", 30.1),
    ("2023-06-15", "Houston", 35.7),
    ("2023-06-15", "Phoenix", 40.2),
    ("2023-06-15", "Miami", 33.8),
    ("2023-06-15", "Denver", 29.6),
    ("2023-06-15", "Seattle", 22.4),
    ("2023-06-15", "Boston", 27.9),
    ("2023-06-15", "Atlanta", 31.5)
]

## Find cities with highest temperatures
hottest_cities = heapq.nlargest(3, temperature_data, key=lambda x: x[2])
print("\nTop 3 Hottest Cities:")
for city_data in hottest_cities:
    print(f"  {city_data[1]}: {city_data[2]}°C")

## Find cities with lowest temperatures
coldest_cities = heapq.nsmallest(3, temperature_data, key=lambda x: x[2])
print("\nTop 3 Coldest Cities:")
for city_data in coldest_cities:
    print(f"  {city_data[1]}: {city_data[2]}°C")

## Application 3: Social Media - User Engagement
print("\n\nAPPLICATION 3: SOCIAL MEDIA - USER ENGAGEMENT")
print("=============================================")

## Sample social media post data (post_id, title, likes, comments, shares, timestamp)
posts = [
    (1001, "Breaking News: Major Announcement", 3500, 420, 1200, datetime(2023, 6, 10, 12, 30)),
    (1002, "Product Review: Latest Gadget", 2200, 380, 900, datetime(2023, 6, 11, 15, 45)),
    (1003, "Tutorial: Python Programming", 1800, 650, 750, datetime(2023, 6, 12, 9, 15)),
    (1004, "Travel Tips for Summer Vacation", 2700, 320, 1100, datetime(2023, 6, 13, 18, 20)),
    (1005, "Recipe: Delicious Desserts", 3100, 450, 1500, datetime(2023, 6, 14, 11, 10)),
    (1006, "Interview with Celebrity", 4200, 580, 2200, datetime(2023, 6, 15, 14, 25)),
    (1007, "Health and Fitness Guide", 1500, 280, 600, datetime(2023, 6, 16, 8, 40)),
    (1008, "Movie Review: Latest Blockbuster", 2900, 410, 950, datetime(2023, 6, 17, 20, 30)),
    (1009, "Tech News: Industry Updates", 2000, 300, 800, datetime(2023, 6, 18, 13, 15)),
    (1010, "DIY Home Improvement Projects", 1700, 520, 700, datetime(2023, 6, 19, 16, 50))
]

## Define a function to calculate engagement score (weighted sum of likes, comments, shares)
def engagement_score(post):
    return post[2] + (post[3] * 2) + (post[4] * 3)  ## likes + (comments * 2) + (shares * 3)

## Find top 3 posts by engagement score
top_engaging_posts = heapq.nlargest(3, posts, key=engagement_score)
print("\nTop 3 Most Engaging Posts:")
for post in top_engaging_posts:
    score = engagement_score(post)
    print(f"  Post ID: {post[0]}")
    print(f"  Title: {post[1]}")
    print(f"  Engagement Score: {score}")
    print(f"  (Likes: {post[2]}, Comments: {post[3]}, Shares: {post[4]})")
    print(f"  Posted on: {post[5].strftime('%Y-%m-%d %H:%M')}")
    print()

## Find top 3 posts by likes
top_liked_posts = heapq.nlargest(3, posts, key=lambda x: x[2])
print("Top 3 Most Liked Posts:")
for post in top_liked_posts:
    print(f"  {post[1]}: {post[2]} likes")

## Find top 3 posts by comments
top_commented_posts = heapq.nlargest(3, posts, key=lambda x: x[3])
print("\nTop 3 Most Commented Posts:")
for post in top_commented_posts:
    print(f"  {post[1]}: {post[3]} comments")

스크립트를 실행하여 이러한 실제 사용 사례를 확인합니다.

python3 practical_applications.py

상위 N 개 요소를 찾는 방법을 실제 시나리오에 적용할 수 있는지 보여주는 자세한 출력이 표시되어야 합니다.

PRACTICAL APPLICATIONS OF FINDING TOP N ELEMENTS IN PYTHON LISTS

APPLICATION 1: E-COMMERCE - ANALYZING PRODUCT SALES
==================================================

Top 3 Best-Selling Products (by units sold):
  Wireless Earbuds: 2100 units sold at $129.99
  Power Bank: 1900 units sold at $49.99
  Bluetooth Speaker: 1750 units sold at $79.99

Top 3 Products by Revenue:
  Smartphone X: $1,124,987.50 revenue (1250 units at $899.99)
  Laptop Pro: $1,156,991.10 revenue (890 units at $1299.99)
  Wireless Earbuds: $272,979.00 revenue (2100 units at $129.99)


APPLICATION 2: DATA ANALYSIS - TEMPERATURE MONITORING
====================================================

Top 3 Hottest Cities:
  Phoenix: 40.2°C
  Houston: 35.7°C
  Miami: 33.8°C

Top 3 Coldest Cities:
  Seattle: 22.4°C
  Boston: 27.9°C
  Los Angeles: 28.3°C


APPLICATION 3: SOCIAL MEDIA - USER ENGAGEMENT
=============================================

Top 3 Most Engaging Posts:
  Post ID: 1006
  Title: Interview with Celebrity
  Engagement Score: 11560
  (Likes: 4200, Comments: 580, Shares: 2200)
  Posted on: 2023-06-15 14:25

  Post ID: 1005
  Title: Recipe: Delicious Desserts
  Engagement Score: 8450
  (Likes: 3100, Comments: 450, Shares: 1500)
  Posted on: 2023-06-14 11:10

  Post ID: 1001
  Title: Breaking News: Major Announcement
  Engagement Score: 8060
  (Likes: 3500, Comments: 420, Shares: 1200)
  Posted on: 2023-06-10 12:30

Top 3 Most Liked Posts:
  Interview with Celebrity: 4200 likes
  Breaking News: Major Announcement: 3500 likes
  Recipe: Delicious Desserts: 3100 likes

Top 3 Most Commented Posts:
  Tutorial: Python Programming: 650 comments
  Interview with Celebrity: 580 comments
  DIY Home Improvement Projects: 520 comments

이러한 예는 전자 상거래 판매 분석, 날씨 데이터 분석 및 소셜 미디어 참여 지표와 같은 실제 시나리오에 배운 기술을 적용할 수 있는 방법을 보여줍니다. 각 경우에 상위 N 개 요소를 효율적으로 찾는 기능은 데이터에서 가치 있는 통찰력을 추출하는 데 매우 중요합니다.

요약

Python 리스트에서 상위 N 개 요소를 찾는 이 랩을 완료하신 것을 축하드립니다. 몇 가지 중요한 기술과 개념을 배웠습니다.

  1. 기본 리스트 연산 (Basic List Operations): Python 의 기본 데이터 구조인 Python 리스트를 생성, 액세스 및 수정하는 방법을 살펴보았습니다.

  2. 정렬 기술 (Sorting Techniques): 오름차순 및 내림차순으로 정렬하는 방법과 사용자 지정 정렬 키를 사용하는 방법을 포함하여 sorted() 함수와 sort() 메서드를 모두 사용하여 리스트를 정렬하는 방법을 배웠습니다.

  3. sorted() 를 사용하여 상위 N 개 요소 찾기: 슬라이싱과 함께 sorted() 함수를 사용하여 리스트에서 상위 N 개 요소를 찾는 방법을 배웠습니다.

  4. heapq 를 사용하여 상위 N 개 요소 찾기: 특히 대규모 데이터 세트의 경우 상위 및 하위 N 개 요소를 찾는 데 더 효율적인 방법 (nlargest()nsmallest()) 을 제공하는 heapq 모듈을 살펴보았습니다.

  5. 실제 사용 사례 (Real-World Applications): 전자 상거래, 데이터 분석 및 소셜 미디어에서 이러한 기술을 실제 시나리오에 적용하여 다재다능함과 유용성을 입증했습니다.

이러한 기술은 데이터 분석 및 처리부터 항목의 우선 순위를 지정하거나 순위를 매겨야 하는 정교한 애플리케이션 구축에 이르기까지 많은 프로그래밍 작업에서 유용할 것입니다. 상위 N 개 요소를 효율적으로 찾는 능력은 Python 프로그래밍 도구 상자에서 강력한 도구입니다.

Python 여정을 계속 진행하면서 이러한 기술이 많은 컨텍스트에서 유용하다는 것을 알게 될 것이며, 이를 기반으로 더 복잡한 문제를 해결할 수 있습니다.