根据Python中的值对字典进行排序

时间:2019-03-28 05:41:03

标签: python json

我试图在Twitter网络中找到“机器人”。因此,我首先手动将Twitter用户标记为bot,然后再遍历其朋友和关注者。 我有以下脚本:

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

其中

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)))

如果我们举一个例子

bot_followers等于:

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

bot_friends等于

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

我期望以下输出: 编辑:我现在看到标签不正确。请专注于排序!

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

但是最终输出是:

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

Q1:我的“默认排序”到底出了什么问题?

问题2:如果有bot_friendsbot_followers,如何删除空白的Counter()

2 个答案:

答案 0 :(得分:0)

对您的当前输出进行排序是否是一种可以接受的解决方案(对我来说是更可取的,因此我不必弄乱产生所述输出的脚本)?您可以这样做:

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)

输出:

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

这是按第一列排序的,但看起来与预期的输出块非常相似。

答案 1 :(得分:0)

您想要OrderedDict中的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]))

并输出:

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