How can I translate these list of integer tuples into a list of name tuples using a dictionary?

Sherlly
users = [
    {'id': 0, "name": "Hero"},
    {'id': 1, "name": "Dunn"},
    {'id': 2, "name": "Sue"},
    {'id': 3, "name": "Chi"},
    {'id': 4, "name": "Thor"},
    {'id': 5, "name": "Clive"},
    {'id': 6, "name": "Hicks"},
    {'id': 7, "name": "Devin"},
    {'id': 8, "name": "Kate"},
    {'id': 9, "name": "Klein"},
]
# list of users

friendship_pairs = [(0, 1), (0, 2), (1, 2), (1, 3), (2, 3), (3, 4), (4, 5),
                    (5, 6), (5, 7), (6, 8), (7, 8),
                    (8, 9)]  # list of friendship pairs

# it is easy to make a dictionary list of friendships for the IDs as the friendship_pairs and written in terms of IDs
# what if I wanted this dict to show for example "Hero": ["dunn","Sue"] instead of 0:[1,2]
friendships = {user['id']: [] for user in users}

for i, j in friendship_pairs:
    friendships[i].append(j)
    friendships[j].append(i)

display(friendships)

# I made a dictonary of intergers:names

user_Ids = list(user['id'] for user in users)
user_Names = list(user['name'] for user in users)

converter = dict(zip(user_IDS, user_Names))

# How can I use this to turn 'friendship_pairs' into something like 'friendship_pairs_names' 
# which has the same information has friendship_pairs but uses their names

I would like to be able to take the input of friendship_pairs and return a variable that has the same information where the ids values from users is replaced with name values from users so that I can make the same dictionary displayed in ids, with names. Open to any suggestion on faster or cleaner methods.

Martin Gustafsson

Write a dictionary that maps an id to a name. like this:

names = {user['id']:user['name'] for user in users}

Then you can create a dictionary in the same way that you created one for the ids but with names[id] instead of id, like so:

users =[
    {'id':0, "name":"Hero"},
    {'id':1, "name":"Dunn"},
    {'id':2, "name":"Sue"},
    {'id':3, "name":"Chi"},
    {'id':4, "name":"Thor"},
    {'id':5, "name":"Clive"},
    {'id':6, "name":"Hicks"},
    {'id':7, "name":"Devin"},
    {'id':8, "name":"Kate"},
    {'id':9, "name":"Klein"},
]

names = {user['id']:user['name'] for user in users}

friendship_pairs = [(0,1),(0,2),(1,2),(1,3),(2,3),(3,4),(4,5),(5,6),(5,7),(6,8),(7,8),(8,9)]

friendships = {user['name']: [] for user in users}

for i, j in friendship_pairs:
    friendships[names[i]].append(names[j])
    friendships[names[j]].append(names[i])

print(friendships)

output:

{
 'Hero': ['Dunn', 'Sue'],
 'Dunn': ['Hero', 'Sue', 'Chi'],
 'Sue': ['Hero', 'Dunn', 'Chi'],
 'Chi': ['Dunn', 'Sue', 'Thor'],
 'Thor': ['Chi', 'Clive'],
 'Clive': ['Thor', 'Hicks', 'Devin'],
 'Hicks': ['Clive', 'Kate'],
 'Devin': ['Clive', 'Kate'],
 'Kate': ['Hicks', 'Devin', 'Klein'],
 'Klein': ['Kate']
}

You could also use setdefault as to not need the the {user['name']: [] for user in users} dictionary comprehension and instead do this:

friendships = {}
for i, j in friendship_pairs:
    friendships.setdefault(names[i], []).append(names[j])
    friendships.setdefault(names[j], []).append(names[i])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I convert a dictionary into a list of tuples?

Python: How can I turn a list of tuples into dictionary keys?

How can I count a list of tuples from a list of tuples?

How do I convert a list of tuples to a dictionary

How to convert this dictionary into a list of tuples?

How can I sum a list of tuples in Haskell?

How can I printf a list of tuples?

How can I convert a list of tuples into a mask?

How can I split a list of tuples scala

How can I subtract tuples in a list?

In Python, how can I combine a set of tuples from a list of given tuples without using itertools?

How to create a dictionary of dictionaries using a list of lists and a list of tuples

Arrange a list of tuples by integer

Convert tuples to list of dictionary

Dictionary with list of tuples Python

list of tuples to dictionary of dictionaries

List of tuples to dictionary

sort list of tuples in a dictionary

Convert a list of tuples to a dictionary

Convert Dictionary to a list of Tuples

Convert list of tuples into a dictionary with list of tuples

How do I group two items in a list of tuples as a dictionary?

How would I make a dictionary from a list of tuples?

How to I factorize a list of tuples?

How can I create a dictionary from a list of k:v tuples returned from a SQL query?

How can I access my orders in the dictionary structure within the tuples in the list?

How can I evaluate a list of strings as a list of tuples in Python?

How to sum tuples in a list of tuples?

How to combine list with integer in an array of tuples?