Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how can I make it that the list updates itself?

In every "for" loop, two items (user and likes) are taken from the "Data" list and are added to a separate list (scoreboard). I am trying to detect if "user" already exists in the scoreboard list. If it does exist, then I try to take the item that comes after "user" in the scoreboard list (which would be the previous "likes" item) and add the new "likes" item to it, after that I try to set the new "likes" item to the new value. but i couldn’t make it work.

   def likecount():
        scoreboard = []
        for t in range(0,len(Data),3):
            user = Data[t]
            likes = Data[t + 2]
            if user not in scoreboard:
                scoreboard.append(user), scoreboard.append(likes)
            else:
                scoreboard[(scoreboard.index(user) + 1)] + likes == scoreboard[(scoreboard.index(user) + 1)]
                
                    
        for i in range(0,len(scoreboard),2):
            print (scoreboard[i],scoreboard[i+1], end= "\n")

#Data list:

['Rhett_andEmma77', 'Los Angeles is soooo hot', '0', 'Tato', 'Glitch', '1', 'Aurio', 'April fools tomorrow', '4', 'nap', 'The bully**', '3', 'NICKSION', 'Oops', '6', 'stupidfuck', 'hes a little guy', '5', 'database', 'Ty', '1', 'stupidfuck', 'possible object show (objects needed)', '3', 'NightSkyMusic', 'nicotine takes 10 seconds to reach your brain', '27', 'stupidfuck', '@BFBleafyfan99 be   anii e', '4', 'Odminey', 'Aliveness', '26', 'stupidfuck', '@techness2011 the', '5', 'techness2011', 'Boomerang', '1', 'saltyipaint', 'April is r slur month', '5', 'HENRYFOLIO', 'flip and dunk', '4', 'SpenceAnimation', 'Got Any grapes 🍇', '2', 'RainyFox2', 'Draw me in your style****', '1', 'hecksacontagon', 'funky cat guy impresses you with his fish corpse', '11', 'HENRYFOLIO', 'flip and dunk @bruhkeko version', '4', 'nairb', 'Spoderman turns green', '5', 'SpenceAnimation', 'Jellybean', '1', 'SpenceAnimation', '@FussiArt', '3']

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Dictiionary is precisely the thing you are looking for instead of keeping a list of items. So whenever a user is considered, you will check if he/she exists

  1. If exists, then add the new likes to the old like count.
  2. If it doesn’t, create an entry with the like count.

That is the natural solution.

data = ['Rhett_andEmma77', 'Los Angeles is soooo hot', '0', 'Tato', 'Glitch', '1', 'Aurio', 'April fools tomorrow', '4', 'nap', 'The bully**', '3', 'NICKSION', 'Oops', '6', 'stupidfuck', 'hes a little guy', '5', 'database', 'Ty', '1', 'stupidfuck', 'possible object show (objects needed)', '3', 'NightSkyMusic', 'nicotine takes 10 seconds to reach your brain', '27', 'stupidfuck', '@BFBleafyfan99 be   anii e', '4', 'Odminey', 'Aliveness', '26', 'stupidfuck', '@techness2011 the', '5', 'techness2011', 'Boomerang', '1', 'saltyipaint', 'April is r slur month', '5', 'HENRYFOLIO', 'flip and dunk', '4', 'SpenceAnimation', 'Got Any grapes 🍇', '2', 'RainyFox2', 'Draw me in your style****', '1', 'hecksacontagon', 'funky cat guy impresses you with his fish corpse', '11', 'HENRYFOLIO', 'flip and dunk @bruhkeko version', '4', 'nairb', 'Spoderman turns green', '5', 'SpenceAnimation', 'Jellybean', '1', 'SpenceAnimation', '@FussiArt', '3']
assert(len(data)%3 == 0)
scoreboard = {}
for i in range(0, len(data), 3):
    if data[i] in scoreboard.keys():
        scoreboard[data[i]]+= int(data[i+2])
    else:
        scoreboard[data[i]] = int(data[i+2])

print(scoreboard)

Output

    {'Rhett_andEmma77': 0, 'Tato': 1, 'Aurio': 4, 'nap': 3, 'NICKSION': 6, 'stupidfuck': 17, 'database': 1, 'NightSkyMusic': 27, 'Odminey': 26, 'techness2011': 1, 'saltyipaint': 5, 'HENRYFOLIO': 8, 'SpenceAnimation': 6, 'RainyFox2': 1, 'hecksacontagon': 11, 'nairb': 5}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading