0

Suppose I have a example Twitter account whose username is @testaccount and I want to grab the followers of the account @testaccount and with that also want to grab the followers of the users who follow @testaccount.

My problem is I don't know how to store this and map this into a database or into dict/list. If yes then how can I do it? Because I have to work with all data including the followers of followers.

For just an example say I have some data that looks like:

I want to store all these data in a way so that I can access each of them under each followers.

0xdeadbeef
  • 21
  • 4
  • 1
    What's your use case? If you simply, as you stated, store these details and access it without worrying too much about the time and space complexity. Then the simplest approach is to have a table/collection with just two columns: userId and followerId. It will satisfy your requirement. – matcheek Aug 27 '19 at 10:09
  • Also, you can watch this amazing NDC talk by Rob Conery how he overpromised something the was clearly beyond computational reach - problem similar to yours. https://www.youtube.com/watch?v=rYlwiJ0vr_4 – matcheek Aug 27 '19 at 10:14
  • looks similar to this https://stackoverflow.com/questions/19734154/followers-following-database-structure – xeon zolt Aug 27 '19 at 10:19
  • Possible duplicate of [Followers/following database structure](https://stackoverflow.com/questions/19734154/followers-following-database-structure) – xeon zolt Aug 27 '19 at 10:19

2 Answers2

0

I would make a custom class, TwitterAccount:

class TwitterAccount:

    def __init__(self, name):
        this.name = name
        this.followers = []

    def addFollowers(self, followers):
        this.followers.extend(followers)

    # Will return a list of the followers.
    def getFollowers(self):
        return this.followers

    # more functionality can be added

And you can use it like this:

acc0 = TwitterAccount("BradPitt")
acc1 = TwitterAccount("BradPittFan1")
acc2 = TwitterAccount("BradPittFan2")
acc3 = TwitterAccount("BradPittFan1Mother")

acc0.addFollowers([acc1,acc2])
acc1.addFollowers([acc3])

# more code

More on classes:

hajduzs
  • 83
  • 1
  • 7
0

Miguel Grinberg has a very good tutorial on flask, where he also goes into detail on the follower/followed structure using SQL databases: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-viii-followers

His solution is construct two tables, a user table which contains the user ID and posts, etc., and a auxiliary table, which maps user IDs (followed) to other user IDs (followers):

https://blog.miguelgrinberg.com/static/images/mega-tutorial/ch08-followers-schema.png

Image from https://blog.miguelgrinberg.com/static/images/mega-tutorial/ch08-followers-schema.png

Tankred
  • 196
  • 1
  • 9