我正在构建应用程序的快速部分,以查看用户的关注者,并突出显示用户关注的人( friend )关注的对象。
我想知道两件事:
代码:
# 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's 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 })
最佳答案
对于问题的第二部分:
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跟随该用户,该用户未关注以及该用户的 friend 关注的关注者。
值=跟随该关注者的该用户的ID列表,该用户未关注
例如,如果用户的关注者的ID为23,并且该用户的两个 friend (用户16和用户28)关注了用户23,则使用键23应该给出以下结果
>>> followers_that_friends_follow[23]
[16,28]
关于python - 显示Python/Django中的 friend 关注哪些Twitter关注者,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5208393/