Home > Software engineering >  Loop within dictionary
Loop within dictionary

Time:10-14

This is the dictionary I am referring to:

members = {
    1: {"name": "Farly Waller", "country": "Russia", "following": [11, 18, 17, 20]},
    2: {"name": "Jania Hardistry", "country": "Indonesia", "following": [3, 8, 15, 20]},
    3: {"name": "Yale Shortan", "country": "Brazil", "following": [2, 4, 8]},
    4: {"name": "Redford Melloi", "country": "Philippines", "following": [1, 3, 13, 14]},
    5: {"name": "Lisbeth Cello", "country": "Pakistan", "following": [4, 7, 10]},
    6: {"name": "Andrew Flaonier", "country": "Pakistan", "following": []},
    7: {"name": "Pris Pilmore", "country": "Japan", "following": [5, 6, 9, 1, 15, 16]},
    8: {"name": "Starla Buffey", "country": "Colombia", "following": [10, 2, 3]},
    9: {"name": "Clark Goymer", "country": "Thailand", "following": [7, 11]},
    10: {"name": "Gayle Testin", "country": "Brazil", "following": [8, 5, 15, 19, 13]},
    11: {"name": "Serena Brombell", "country": "China", "following": [1, 17, 18]},
    12: {"name": "Wendy Toke", "country": "Peru", "following": []},
    13: {"name": "Clarice Shelmardine", "country": "Portugal", "following": [10, 1, 14, 15, 16, 19]},
    14: {"name": "Julie Corradeschi", "country": "Peru", "following": [13, 1, 2, 4]},
    15: {"name": "Constantin Sebire", "country": "China", "following": [3, 10, 13, 16, 18]},
    16: {"name": "Annadiane Mum", "country": "Japan", "following": [7, 13, 6, 18]},
    17: {"name": "Lovell Lubbock", "country": "China", "following": [1, 11, 20]},
    18: {"name": "Gwynne Toplis", "country": "China", "following": [1, 11, 16, 17]},
    19: {"name": "Pammy Urey", "country": "Yemen", "following": [10, 6, 13, 20]},
    20: {"name": "Celestina Phillipps", "country": "Canada", "following": [3, 17]},

I am struggling with this code:

    followers = {}
for user_id in members:
    followers[user_id]=[]
    for user1 in members[user_id]["following"]:
        if user1 in members[user_id]["following"]: 
            
            followers[user_id].append(user1)
        
print(followers)

currently for each user_id the people this person follows are displayed, but I actually want to see from each person the followers, so e.g., For user 5, the value should be the list [7, 10]. For user 1, the value should be the list [4, 7, 11, 13, 14, 17, 18].

Does anyone know how to make the switch?

Best, Emma

CodePudding user response:

You can do this,

d = {}
for k, v in members.items():
    for i in v.get('following'):
        d.setdefault(i, []).append(k)

Output:

{11: [1, 9, 17, 18],
 18: [1, 11, 15, 16],
 17: [1, 11, 18, 20],
 20: [1, 2, 17, 19],
 3: [2, 4, 8, 15, 20],
 8: [2, 3, 10],
 15: [2, 7, 10, 13],
 2: [3, 8, 14],
 4: [3, 5, 14],
 1: [4, 7, 11, 13, 14, 17, 18],
 13: [4, 10, 14, 15, 16, 19],
 14: [4, 13],
 7: [5, 9, 16],
 10: [5, 8, 13, 15, 19],
 5: [7, 10],
 6: [7, 16, 19],
 9: [7],
 16: [7, 13, 15, 18],
 19: [10, 13]}

CodePudding user response:

I think you can do it like this :

followers = {}

for person in members.keys():
    person_followers = []
    for user, detail in members.items():
        if person != user and person in detail["following"]:
            person_followers.append(user)
    followers[person] = person_followers

followers

Result :

{1: [4, 7, 11, 13, 14, 17, 18],
 2: [3, 8, 14],
 3: [2, 4, 8, 15, 20],
 4: [3, 5, 14],
 5: [7, 10],
 6: [7, 16, 19],
 7: [5, 9, 16],
 8: [2, 3, 10], ... }
  • Related