Python 如何使用字典将这些整数元组列表转换为名称元组列表?

Python 如何使用字典将这些整数元组列表转换为名称元组列表?,python,data-science,python-3.8,Python,Data Science,Python 3.8,我希望能够获取Friendly_pairs的输入,并返回一个具有相同信息的变量,其中来自用户的ids值将替换为来自用户的名称值,以便我可以使用名称在ids中显示相同的字典。欢迎任何关于更快或更干净的方法的建议。您可以试试这个 users = [ {'id': 0, "name": "Hero"}, {'id': 1, "name": "Dunn"}, {'id': 2, "name&

我希望能够获取Friendly_pairs的输入,并返回一个具有相同信息的变量,其中来自用户的ids值将替换为来自用户的名称值,以便我可以使用名称在ids中显示相同的字典。欢迎任何关于更快或更干净的方法的建议。

您可以试试这个

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
输出

group= [(list(users[x].values())[1],list(users[y].values())[1] )for x,y in friendship_pairs]


编写一个将id映射到名称的词典。像这样:

[('Hero', 'Dunn'),
 ('Hero', 'Sue'),
 ('Dunn', 'Sue'),
 ('Dunn', 'Chi'),
 ('Sue', 'Chi'),
 ('Chi', 'Thor'),
 ('Thor', 'Clive'),
 ('Clive', 'Hicks'),
 ('Clive', 'Devin'),
 ('Hicks', 'Kate'),
 ('Devin', 'Kate'),
 ('Kate', 'Klein')]
然后,您可以按照为id创建字典的相同方式创建字典,但使用名称[id]而不是id,如下所示:

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

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)
您也可以使用
setdefault
作为不需要
{user['name']:[]作为用户中的用户}
字典理解,而是执行以下操作:

{
 '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']
}
friendships = {}
for i, j in friendship_pairs:
    friendships.setdefault(names[i], []).append(names[j])
    friendships.setdefault(names[j], []).append(names[i])