Showing that Twitter Followers Follow Friends in Python / Django

I create a quick part of my application that looks at the followers of users, and highlights who are followed by people who follow the user (friends).

I am interested in two things:

  • Is there a more efficient way to do this? It looks like this will launch the Twitter API restrictions, because I need to check the friends of each of the user's friends.

  • This creates a list of dicts containing the identifier of the friend and the followers that they follow. Instead, a dict would be better as an identifier for a follower and then friends who follow them. Advice?

the code:

# Get followers and friends
followers = api.GetFollowerIDs()['ids']
friends = api.GetFriendIDs()['ids']

# Create list of followers user is not following
followers_not_friends = set(followers).difference(friends)

# Create list of which of user followers are followed by which friends
followers_that_friends_follow = []
for f in friends:
    ff = api.GetFriendIDs(f)['ids']
    users = followers_not_friends.intersection(ff)
    followers_that_friends_follow.append({'friend': f, 'users': users })
+5
source share
1 answer

:

import collections

followers_that_friends_follow = collections.defaultdict(list)
for f in friends:
    ff = api.GetFriendsIDs(f)['ids']
    users = followers_not_friends.intersection(ff)
    for user in users:
        followers_that_friends_follow[user].append(f)

keys = ids , , , .

values ​​= , ,

, 23, - ( 16 28) 23, 23,

>>> followers_that_friends_follow[23]
[16,28]
+1

All Articles