如何先按id再按发布日期对Python列表分组(MongoDB游戏发布场景)
Got it, let's break this down. You've got a Python list of game release records from your MongoDB-backed /releases/ endpoint—where the same game ID shows up multiple times for different platforms—and you need to group them first by game ID, then by release date. Here are two straightforward, efficient approaches using Python's standard libraries:
Approach 1: Using itertools.groupby (Ordered Grouping)
This method is great if you need your groups to be ordered by game ID and date. Just remember: groupby only groups consecutive items with the same key, so we need to sort the data first.
from itertools import groupby from operator import itemgetter # Example release data (replace with your actual list) release_data = [ {"date": 1524528000000, "game": 253, "id": 1, "platform": 2}, {"date": 1524528000000, "game": 253, "id": 2, "platform": 6}, {"date": 1943308800000, "game": 253, "id": 3, "platform": 2}, {"date": 1524528000000, "game": 101, "id": 4, "platform": 2} ] # Step 1: Sort the data first by game ID, then by release date sorted_releases = sorted(release_data, key=itemgetter("game", "date")) # Step 2: Group by game ID first, then by date grouped_results = {} for game_id, game_records in groupby(sorted_releases, key=itemgetter("game")): date_groups = {} for release_date, date_records in groupby(game_records, key=itemgetter("date")): # Convert groupby's iterator to a list so we can reuse the data date_groups[release_date] = list(date_records) grouped_results[game_id] = date_groups # Print the output to verify for game_id, date_groups in grouped_results.items(): print(f"Game ID: {game_id}") for date, records in date_groups.items(): print(f" Release Timestamp: {date}") for record in records: print(f" {record}")
Approach 2: Using collections.defaultdict (Flexible, No Pre-Sorting)
This method is more intuitive and doesn't require pre-sorting the data. It's perfect if you don't need strict ordering upfront (though you can sort the groups later if needed).
from collections import defaultdict # Use the same release_data list as above grouped_data = defaultdict(lambda: defaultdict(list)) # Iterate through each record and nest it under the correct game ID > date for record in release_data: game_id = record["game"] release_date = record["date"] grouped_data[game_id][release_date].append(record) # Optional: Convert to a regular dict if you don't need defaultdict behavior grouped_data = dict(grouped_data) # Optional: Sort the date groups for each game (ascending order) for game_id in grouped_data: sorted_date_groups = sorted(grouped_data[game_id].items(), key=lambda x: x[0]) grouped_data[game_id] = dict(sorted_date_groups) # Verify the output for game_id, date_groups in grouped_data.items(): print(f"Game ID: {game_id}") for date, records in date_groups.items(): print(f" Release Timestamp: {date}") for record in records: print(f" {record}")
Quick Note
If you actually meant grouping by the release id (the unique id field in each record) first instead of game ID, just swap the key from "game" to "id" in either approach. But based on your description of duplicate game IDs across platforms, I assumed game ID was the primary grouping key.
内容的提问来源于stack exchange,提问作者user9313865

