简体   繁体   中英

Sort dictionary based on values in Python

I am trying to find "bots" within Twitter networks. So, I first manually label a Twitter user as an bot and then I want to loop through its friends and followers. I have the following script:

def get_bot_network(target):
    global interactions
    bot_friends = []
    bot_followers = []
    friends = get_friends_ids(target, a)
    followers = get_followers_ids(target, a)

    if target not in interactions:
       interactions[target] = Counter()
       if len(friends) > 0:
          bot_friends = get_bots_from_ids(friends)
             if bot_friends is not None and len(bot_friends) > 0:
                for bot_friend in bot_friends:
                   interactions[target][bot_friend] = 'Friend'

    if len(followers) > 0:
       bot_followers = get_bots_from_ids(followers)
       if bot_followers is not None and len(bot_followers) > 0:
          for bot_follower in bot_followers:
             interactions[target][bot_follower] = 'Follower'

   save_json(interactions, 'bots/interactions.json')
   return bot_friends, bot_followers

where

def save_json(variable, filename):
    with io.open(filename, 'w', encoding='utf-8') as f:
       f.write(str(json.dumps(variable, indent=4, ensure_ascii=False)))

If we take an example where

bot_followers is equal to:

['1103608560468975616', '1103607026645590016', '1103586971706810368', '1103586464942575617', '1103586148004257792', '1103574979868676096', '1103574280392957960', '1103573913122947072', '1103573645140488192']

and bot_friends is equal to

['1103574280392957960', '1103573913122947072', '1103573645140488192', '1103573218227499008', '1103569167226028033']

I would expect the following output: Edit: I see now that the labeling is incorrect. Please focus on the sorting!

 { "1103572731168096258": { "1103573218227499008": "Friend", "1103569167226028033": "Friend", "1103574280392957960": "Follower", "1103573913122947072": "Follower", "1103573645140488192": "Follower", "1103608560468975616": "Follower", "1103607026645590016": "Follower", "1103586971706810368": "Follower", "1103586464942575617": "Follower", "1103586148004257792": "Follower", "1103574979868676096": "Follower" } } 

But the final output is:

 { "1103572731168096258": { "1103574280392957960": "Follower", "1103573913122947072": "Follower", "1103573645140488192": "Follower", "1103573218227499008": "Friend", "1103569167226028033": "Friend", "1103608560468975616": "Follower", "1103607026645590016": "Follower", "1103586971706810368": "Follower", "1103586464942575617": "Follower", "1103586148004257792": "Follower", "1103574979868676096": "Follower" } } 

Q1: What is exactly going wrong in my "default sorting"?

Q2: If there are bot_friends or bot_followers , how can I delete the empty Counter() ?

Would it be an acceptable solution to sort your current output (preferable for me so I don't have to mess with your script that produces said output)? You can do that like:

my_dict = {
  "1103572731168096258": {
    "1103574280392957960": "Follower",
    "1103573913122947072": "Follower",
    "1103573645140488192": "Follower",
    "1103573218227499008": "Friend",
    "1103569167226028033": "Friend",
    "1103608560468975616": "Follower",
    "1103607026645590016": "Follower",
    "1103586971706810368": "Follower",
    "1103586464942575617": "Follower",
    "1103586148004257792": "Follower",
    "1103574979868676096": "Follower"
}}

for key in my_dict.keys():
    sorted_dict = sorted(my_dict[key].items())
    for item in sorted_dict:
        print(item)

Outputs:

('1103569167226028033', 'Friend')
('1103573218227499008', 'Friend')
('1103573645140488192', 'Follower')
('1103573913122947072', 'Follower')
('1103574280392957960', 'Follower')
('1103574979868676096', 'Follower')
('1103586148004257792', 'Follower')
('1103586464942575617', 'Follower')
('1103586971706810368', 'Follower')
('1103607026645590016', 'Follower')
('1103608560468975616', 'Follower')

This is sorting by the first column, but looks very similar to your expected output block.

You want OrderedDict from collections :

from collections import OrderedDict

d = {
    "1103574280392957960": "Follower",
    "1103573913122947072": "Follower",
    "1103573645140488192": "Follower",
    "1103573218227499008": "Friend",
    "1103569167226028033": "Friend",
    "1103608560468975616": "Follower",
    "1103607026645590016": "Follower",
    "1103586971706810368": "Follower",
    "1103586464942575617": "Follower",
    "1103586148004257792": "Follower",
    "1103574979868676096": "Follower"
}

# dictionary sorted by key
dd = OrderedDict(sorted(d.items(), key=lambda t: t[0]))

and output:

OrderedDict([('1103569167226028033', 'Friend'),
             ('1103573218227499008', 'Friend'),
             ('1103573645140488192', 'Follower'),
             ('1103573913122947072', 'Follower'),
             ('1103574280392957960', 'Follower'),
             ('1103574979868676096', 'Follower'),
             ('1103586148004257792', 'Follower'),
             ('1103586464942575617', 'Follower'),
             ('1103586971706810368', 'Follower'),
             ('1103607026645590016', 'Follower'),
             ('1103608560468975616', 'Follower')])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM