如何在python中创建词典列表

时间:2018-07-03 12:42:18

标签: python python-3.x list dictionary tuples

我有一本字典和友谊元组。我需要创建一个词典列表,每个键的人和值是他的朋友

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


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


friend={}
friends=[]
for i in users:
    for j,k in friendships:
        if i['id']==j:
            friend[i['name']]=k
            friends.append(friend)

4 个答案:

答案 0 :(得分:1)

您可以使用:

[{[i for i in users if i['id'] == x][0]['name'],
  [i for i in users if i['id'] == y][0]['name']} for x,y in friendships]

输出:

[{'Dunn', 'hero'},
 {'Sue', 'hero'},
 {'Dunn', 'Sue'},
 {'Chie', 'Dunn'},
 {'Chie', 'Sue'},
 {'Chie', 'Thor'},
 {'Clive', 'Thor'},
 {'Clive', 'Hicks'},
 {'Clive', 'Devin'},
 {'Hicks', 'Kate'},
 {'Devin', 'Kate'},
 {'Kate', 'kelin'}]

答案 1 :(得分:1)

即使我不确定您的要求,也请尝试为您提供易于阅读的解决方案。它为您提供了具有以下内容的字典:
 -键=用户名,
 -值=该用户的朋友列表:

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

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

names = {d["id"]:d["name"] for d in users}
friends = {d["name"]:[] for d in users}

for t in friendships:
    friends[names[t[0]]].append(names[t[1]])
    friends[names[t[1]]].append(names[t[0]])

friends = [{k:v} for k,v in friends.items()]

print(friends)
# [{'hero': ['Dunn', 'Sue']}, {'Dunn': ['hero', 'Sue', 'Chie']}, {'Sue': ['hero', 'Dunn', 'Chie']},
#  {'Chie': ['Dunn', 'Sue', 'Thor']}, {'Thor': ['Chie', 'Clive']}, {'Clive': ['Thor', 'Hicks', 'Devin']},
#  {'Hicks': ['Clive', 'Kate']}, {'Devin': ['Clive', 'Kate']}, {'Kate': ['Hicks', 'Devin', 'kelin']},
#  {'kelin': ['Kate']}]

答案 2 :(得分:0)

您的数据:

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


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

现在:

friendsdictList = []


for friendPairs in friendships:
    friends = {}
    left = friendPairs[0]
    right = friendPairs[1]

    print(left, right)
    for ids in users:




        if ids['id'] == left:


            leftkey = ids["name"]
            print(leftkey)

        if ids['id'] == right:

            rightvalue = ids["name"]
            print(rightvalue)

    friends[leftkey] = rightvalue
    friendsdictList.append(friends)
    print("\n")


print(friendsdictList)            

会给你

[{'hero': 'Dunn'}, {'hero': 'Sue'}, {'Dunn': 'Sue'}, {'Dunn': 'Chie'}, {'Sue': 'Chie'}, 
 {'Chie': 'Thor'}, {'Thor': 'Clive'}, {'Clive': 'Hicks'}, {'Clive': 'Devin'}, {'Hicks': 'Kate'}, 
 {'Devin': 'Kate'}, {'Kate': 'kelin'}]

我猜这就是你想要的。

答案 3 :(得分:0)

创建一个dict,其中包含用户ID和用户名之间的映射。那可以帮助您轻松解决问题

users = [{'id': 0, 'name': 'hero'}, {'id': 1, 'name': 'Dunn'}, {'id': 2, 'name': 'Sue'}, {'id': 3, 'name': 'Chie'}, {'id': 4, 'name': 'Thor'}, {'id': 5, 'name': 'Clive'}, {'id': 6, 'name': 'Hicks'}, {'id': 7, 'name': 'Devin'}, {'id': 8, 'name': 'Kate'}, {'id': 9, 'name': 'kelin'}]
friendships = [(0, 1), (0, 2), (1, 2), (1, 3), (2, 3), (3, 4), (4, 5), (5, 6), (5, 7), (6, 8), (7, 8), (8, 9)]

users_dict = {d['id']: d['name'] for d in users}
friends = [{users_dict[k]: users_dict[v]} for k,v in friendships]
print (friends)
# [{'hero': 'Dunn'}, {'hero': 'Sue'}, {'Dunn': 'Sue'}, {'Dunn': 'Chie'}, {'Sue': 'Chie'}, {'Chie': 'Thor'}, {'Thor': 'Clive'}, {'Clive': 'Hicks'}, {'Clive': 'Devin'}, {'Hicks': 'Kate'}, {'Devin': 'Kate'}, {'Kate': 'kelin'}]