如何利用字典将整数元组列表转换为名称元组列表并生成名称映射的好友关系字典
Hey there! Let's break down how to solve each of your problems, plus some cleaner, more efficient ways to approach the whole workflow.
1. Convert friendship_pairs to friendship_pairs_names
Since you already have the converter dict mapping IDs to names, a simple list comprehension will swap out every ID for its corresponding username in one line:
friendship_pairs_names = [(converter[id1], converter[id2]) for id1, id2 in friendship_pairs]
This gives you a list like [("Hero", "Dunn"), ("Hero", "Sue"), ...] exactly as requested.
2. Create a name-based friendship dictionary
You have two solid options to mirror the structure of friendships but use usernames instead of IDs:
Option A: Use the converted name pairs
First initialize an empty dict for all users, then populate mutual friendships by iterating over the name pairs:
# Start with all user names pointing to empty lists friendships_names = {user["name"]: [] for user in users} # Add each two-way friendship for name1, name2 in friendship_pairs_names: friendships_names[name1].append(name2) friendships_names[name2].append(name1)
Option B: Reuse the existing friendships dict (more efficient)
Since you already built the ID-based friendships dict, you can skip reprocessing pairs and directly convert friend IDs to names in a dictionary comprehension:
friendships_names = { user["name"]: [converter[friend_id] for friend_id in friendships[user["id"]]] for user in users }
This leverages the work you already did and avoids an extra loop over the pairs.
3. More concise & efficient implementations
Let's streamline the entire pipeline to cut redundant code and improve efficiency:
Simplify the converter dict
Instead of creating two separate lists and zipping them, use a dictionary comprehension directly from the users list—this is shorter and cleaner:
converter = {user["id"]: user["name"] for user in users}
Skip intermediate steps (if you don't need friendship_pairs_names)
If you don't need the name-based pairs list for anything else, you can build friendships_names directly from the original friendship_pairs without extra steps:
friendships_names = {name: [] for name in converter.values()} for id1, id2 in friendship_pairs: name1, name2 = converter[id1], converter[id2] friendships_names[name1].append(name2) friendships_names[name2].append(name1)
This saves memory by avoiding the intermediate friendship_pairs_names list.
One-shot workflow (if you don't need the ID-based friendships)
If you only care about the name-based friendship structure and don't need the ID version, you can do everything in a few lines:
# Build converter dict converter = {user["id"]: user["name"] for user in users} # Initialize name-based friendships friendships_names = {name: [] for name in converter.values()} # Populate directly from original pairs for id1, id2 in friendship_pairs: friendships_names[converter[id1]].append(converter[id2]) friendships_names[converter[id2]].append(converter[id1])
This eliminates the need to build the ID-based friendships dict entirely if you don't use it elsewhere.
All these approaches prioritize readability while cutting down on unnecessary code and processing. Let me know if you want to dive deeper into any of these!
内容的提问来源于stack exchange,提问作者Sherlly

