The objective of my problem is to read in a thread post such as this:
([
{
'title': 'Invade Manhatten, anyone?',
'tags': ['world-domination', 'hangout'],
'posts': [
{
'author': 'Mr. Sinister',
'content': "I'm thinking 9 pm?",
'upvotes': 2,
},
{
'author': 'Mystique',
'content': "Sounds fun!",
'upvotes': 0,
},
{
'author': 'Magneto',
'content': "I'm in!",
'upvotes': 0,
},
],
}
]))
And create a definition to output this:
[('Mr. Sinister', '2', 'Cautioiusly Evil'), ('Magneto', '0', 'Insignificantly Evil'), ('Mystique', '0', 'Insignificantly Evil')]
Where the list is sorted from highest upvotes to lowest upvotes, with ties broken alphabetically.
However, when I was given this thread:
([
{
'title': 'Invade Manhatten, anyone?',
'tags': ['world-domination', 'hangout'],
'posts': [
{
'author': 'Mr. Sinister',
'content': "I'm thinking 9 pm?",
'upvotes': 2,
},
{
'author': 'Mr. Sinister',
'content': "Sounds fun!",
'upvotes': 0,
},
{
'author': 'Mr. Sinister',
'content': "I'm in!",
'upvotes': 0,
},
],
}
]))
Where the author posts multiple times, my program outputs:
[('Mr. Sinister', '2', 'Cautioiusly Evil'), ('Mr. Sinister', '0', 'Insignificantly Evil'), ('Mr. Sinister', '0', 'Insignificantly Evil')]
My program prints each individual post rather than combining the results like this:
[('Mr. Sinister', 2, 'Cautiously Evil')]
Just to clarify, if the thread was:
([
{
'title': 'Invade Manhatten, anyone?',
'tags': ['world-domination', 'hangout'],
'posts': [
{
'author': 'Mr. Sinister',
'content': "I'm thinking 9 pm?",
'upvotes': 2,
},
{
'author': 'Loki',
'content': "Sounds fun!",
'upvotes': 2,
},
{
'author': 'Mr. Sinister',
'content': "I'm in!",
'upvotes': 2,
},
{
'author': 'Loki',
'content': "I'm in it!",
'upvotes': 20,
},
],
}
]))
It should input:
[('Loki', 22, 'Justifiably Evil'), ('Mr. Sinister', 4, 'Cautiously Evil')]
My code for this is here:
def author_rankings(thread_list):
# TODO: Determine (author, upvotes, ranking) over all threads.
counterA = 0
counterB=2
listA = []
Final = []
Double = {}
for i in thread_list[0]['posts']:
for ii in i:
if ii == 'content':
null = 1
else:
b = str(i[ii])
if b in Double:
Double[b]
a = b
if a.isdigit():
a = int(a)
listA.append(a)
bel=[]
for qq in listA:
if counterA == counterB:
bel = []
counterB+=2
if counterA%2 ==0:
bel.append(qq)
counterA+=1
else:
bel.append(qq)
qq = int(qq)
if qq == 0:
bel.append('Insignificantly Evil')
elif qq < 20:
bel.append('Cautiously Evil')
elif qq < 100:
bel.append('Justifiably Evil')
elif qq < 500:
bel.append('Wickedly Evil')
elif qq >= 500:
bel.append('Diabolically Evil')
counterA+=1
tuuple = tuple(bel)
Final.append(tuuple)
Final.sort()
Final.sort(key=lambda tup: -tup[1])
I know I code slightly un-pythonic/ hard to read. Sorry for the inconvenience.
Thank you!