【问题标题】:How can i create a list of dictionaries in python如何在 python 中创建字典列表
【发布时间】:2018-07-03 12:42:17
【问题描述】:

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

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)

【问题讨论】:

  • 你的问题是什么?
  • 预期的输出是什么?
  • 您能否详细说明一下如何将用户与朋友配对?
  • 你能说出你的预期输出吗?

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


【解决方案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'}]

【讨论】:

    【解决方案2】:

    让我们尝试为您提供一个易于阅读的解决方案,即使我不确定您的要求。它为您提供了一本字典:
    - 键 = 用户名,
    - value = 该用户的好友列表:

    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']}]
    

    【讨论】:

      【解决方案3】:

      您的数据:

      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'}]
      

      我猜这就是你想要的。

      【讨论】:

      • 不过,这有很多猜测。
      • I need to create a list of dictionaries that each key the person and the values are his friends is what he asked. 我尽量保持谦虚。
      【解决方案4】:

      创建一个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'}]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-11-18
        • 2011-01-24
        • 2020-03-07
        • 2023-02-07
        • 2017-05-05
        • 1970-01-01
        • 2018-09-17
        相关资源
        最近更新 更多