如何使用字典将这些整数元组列表翻译成名称元组列表?
How can I translate these list of integer tuples into a list of name tuples using a dictionary?
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
我希望能够输入 friendship_pairs 和 return 一个具有相同信息的变量,其中用户的 ids 值被用户的名称值替换,这样我可以使相同的字典显示在 ids 中,带有名称。欢迎任何关于更快或更清洁方法的建议。
你可以试试这个
group= [(list(users[x].values())[1],list(users[y].values())[1] )for x,y in friendship_pairs]
产出
[('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 映射到名称的字典。像这样:
names = {user['id']:user['name'] for user in users}
然后您可以使用与为 id 创建字典相同的方式创建字典,但使用 names[id] 而不是 id,如下所示:
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)
输出:
{
'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']
}
你也可以使用 setdefault
不需要 {user['name']: [] for user in users}
字典理解,而是这样做:
friendships = {}
for i, j in friendship_pairs:
friendships.setdefault(names[i], []).append(names[j])
friendships.setdefault(names[j], []).append(names[i])
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
我希望能够输入 friendship_pairs 和 return 一个具有相同信息的变量,其中用户的 ids 值被用户的名称值替换,这样我可以使相同的字典显示在 ids 中,带有名称。欢迎任何关于更快或更清洁方法的建议。
你可以试试这个
group= [(list(users[x].values())[1],list(users[y].values())[1] )for x,y in friendship_pairs]
产出
[('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 映射到名称的字典。像这样:
names = {user['id']:user['name'] for user in users}
然后您可以使用与为 id 创建字典相同的方式创建字典,但使用 names[id] 而不是 id,如下所示:
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)
输出:
{
'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']
}
你也可以使用 setdefault
不需要 {user['name']: [] for user in users}
字典理解,而是这样做:
friendships = {}
for i, j in friendship_pairs:
friendships.setdefault(names[i], []).append(names[j])
friendships.setdefault(names[j], []).append(names[i])