我的問題的目的是在一個線程後閱讀像這樣的更新列表:Python的 - 檢查重複項列表,並添加副本連同加值
([
{
'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,
},
],
}
))
,並創建一個定義輸出這樣的:
[('Mr. Sinister', '2', 'Cautioiusly Evil'), ('Magneto', '0', 'Insignificantly Evil'), ('Mystique', '0', 'Insignificantly Evil')]
凡列表從最高upvotes排序,以最低的upvotes,與領帶打破按字母順序排列。
然而,當我得到這個線程:
([
{
'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,
},
],
}
))
如作者的帖子多次,我的計劃產出:
[('Mr. Sinister', '2', 'Cautioiusly Evil'), ('Mr. Sinister', '0', 'Insignificantly Evil'), ('Mr. Sinister', '0', 'Insignificantly Evil')]
我的程序打印每一個人而不是結合如下結果:
[('Mr. Sinister', 2, 'Cautiously Evil')]
只是爲了澄清,如果線程是:
([
{
'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,
},
],
}
))
它應該輸入:
[('Loki', 22, 'Justifiably Evil'), ('Mr. Sinister', 4, 'Cautiously Evil')]
我對這個代碼是在這裏:
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])
我知道我的代碼稍微不pythonic /難以閱讀。抱歉給你帶來不便。
謝謝!
您是否想總結_author_的upvotes? – wwii
對不起,這個東西只是無法讀取,它不是「略微不pythonic」這是魔鬼的工作。你不能給出有意義的名字嗎?在嘗試幫助時讓我們的生活更輕鬆? – droravr
@droravr對不起!我將變量更改爲更易理解的名稱。 –