Created
September 13, 2015 03:14
-
-
Save seanlinehan/54eb0a122ac4c4308a95 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from collections import defaultdict | |
# Convenience method | |
def get_intersection(users): | |
# pull out the users' following list | |
list_of_follower_lists = map(lambda x: x[1], users) | |
# return a list containing the common followers amongst them all | |
return set(list_of_follower_lists[0]).intersection(*list_of_follower_lists) | |
# This is the file containing all the data fetched from Twitter | |
with open('twitter_ids.txt') as fo: | |
ids = fo.read().splitlines() | |
# Convenient data structure to aggregate each person's follower set | |
follower_sets = defaultdict(list) | |
for pair in ids: | |
followed, follower = pair.split(',') | |
follower_sets[followed].append(follower) | |
# Find the 10-top most followed people | |
most_followers = sorted(follower_sets.iteritems(), key=lambda i: len(i[1]), reverse=True)[:10] | |
# Find the people that follow all 10 | |
common_followers = get_intersection(most_followers) | |
# Count them | |
num_common_followers = len(common_followers) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment