You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python元组列表合并排序高效实现及代码复杂度优化咨询

Optimizing consolidate_and_sort_interests for Time & Space Efficiency

Great question—let's dive into refining this method to be faster and more memory-efficient. First, let's break down where the current implementation has room to improve:

Current Implementation Bottlenecks

  • Time Complexity: Using bisect.insort_right to maintain a sorted list as you add each interest count means every insertion takes O(k) time (where k is the current length of the sorted list). With m unique interests, this adds up to O(m²) time—this will get slow quickly as the number of unique interests grows.
  • Space Complexity: Storing all m unique interest counts in a fully sorted list uses O(m) space. While this works, we can cut down on memory usage if n (the number of top interests we need) is much smaller than m.

Optimized Approach: Min-Heap for Top-N Tracking

The biggest win comes from using a min-heap (via Python's built-in heapq module) instead of sorting all interests. Here's why this works:

  • Instead of sorting every single unique interest, we only track the top n values. A min-heap of size n lets us do this in O(m log n) time—way faster than the O(m log m) of full sorting when n is small (like fetching the top 10 interests from thousands of unique ones).
  • The heap only uses O(n) space, which is a huge saving when dealing with large numbers of unique interests.

The counting step with defaultdict is already optimal (O(N) time, where N is the total number of interests across all profiles)—we'll keep that part intact.

Optimized Code

Here's the revised method, plus a small fix for a bug in get_profiles (note: ('3') is a string, not a tuple—you need ('3',) to create a single-element tuple):

import heapq
from collections import defaultdict
from dataclasses import dataclass
from typing import Tuple

@dataclass
class Profile(object):
    uid: str
    interests: Tuple[str]
    following: Tuple[str]

def get_sorted_interests(n):
    """ Get the most 'n' number of interests """
    return consolidate_and_sort_interests(n)

def get_profiles():
    # Fixed single-element tuples for 'following'
    p1 = Profile('1', ('film', 'music'), ('3',))
    p2 = Profile('2', ('football', 'film'), ('1',))
    p3 = Profile('3', ('music', 'film'), ('1', '2'))
    yield p1
    yield p2
    yield p3

def consolidate_and_sort_interests(n):
    """ Should return for eg: if n = 2 [ ('film', 3), ('music', 2) ] """
    # Count interest frequencies (optimal O(N) time)
    interest_counts = defaultdict(int)
    for p in get_profiles():
        for interest in p.interests:
            interest_counts[interest] += 1
    
    # Use min-heap to track top n interests
    min_heap = []
    for interest, count in interest_counts.items():
        heapq.heappush(min_heap, (count, interest))
        # Keep heap size limited to n by removing the smallest element when overflowing
        if len(min_heap) > n:
            heapq.heappop(min_heap)
    
    # Reverse to get descending order, reformat to (interest, count)
    top_interests = []
    while min_heap:
        count, interest = heapq.heappop(min_heap)
        top_interests.append((interest, count))
    return top_interests[::-1]

Key Improvements

  1. Time Complexity: Reduced from O(m²) to O(N + m log n) (N = total interests, m = unique interests)—a massive speedup for large datasets.
  2. Space Complexity: Cut from O(m) to O(m + n)—the heap only uses O(n) space instead of O(m) for the full sorted list.
  3. Edge Case Handling: Gracefully returns all interests sorted by count if n is larger than the number of unique interests.

Bonus: Concise Alternative with collections.Counter

If you prefer to lean on Python's optimized built-ins, collections.Counter has a most_common(n) method that does exactly what we need. It uses a heap under the hood, so it's just as efficient:

from collections import Counter

def consolidate_and_sort_interests(n):
    all_interests = []
    for p in get_profiles():
        all_interests.extend(p.interests)
    return Counter(all_interests).most_common(n)

This is far more concise and leverages Python's optimized C-backed code—perfect unless you need to implement the logic manually.

内容的提问来源于stack exchange,提问作者RampantSoze

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 19:08:14