2017-08-09 53 views
0

我的問題的目的是在一個線程後閱讀像這樣的更新列表: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 /難以閱讀。抱歉給你帶來不便。

謝謝!

+1

您是否想總結_author_的upvotes? – wwii

+1

對不起,這個東西只是無法讀取,它不是「略微不pythonic」這是魔鬼的工作。你不能給出有意義的名字嗎?在嘗試幫助時讓我們的生活更輕鬆? – droravr

+0

@droravr對不起!我將變量更改爲更易理解的名稱。 –

回答

0

我不明白你在問什麼,因爲邏輯不是很清楚。

然而,聚合可以做到像這樣:

some_pages = [ 
     { 
      '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, 
       }, 
      ], 
     } 
    ] 

author_aggregation = {} 
for pages in some_pages: 
    for post in pages.get('posts', []): 
     a = post.get('author') 
     v = post.get('upvotes') 
     c = post.get('content') 
     if a in author_aggregation: 
      author_aggregation.update({a: {'upvotes': author_aggregation[a]['upvotes'] += v, 'content': author_aggregation[a]['content'].append(c)}}) 
     else: 
      author_aggregation[a] = {'upvotes': v, 'content': [c]} 

相關:

0

這可能會實現,它忽略內容(可加入,以及如果需要),只需要注意和作者。它還採用了字典,而不是一個列表:

authors = dict() 

for post in x[0]['posts']: 
    try: 
     authors[post['author']] += post['upvotes'] 

    except KeyError: 
     authors[post['author']] = post['upvotes'] 

for k, upvotes in authors.iteritems(): 
    if upvotes == 0: 
     authors[k] = (upvotes, "Insignificantly Evil") 

    elif upvotes < 20: 
     authors[k] = (upvotes, "Cautioiusly Evil") 

    elif upvotes < 100: 
     authors[k] = (upvotes, "Justifiably Evil") 

    elif upvotes <= 500: 
     authors[k] = (upvotes, "Wickedly Evil") 

    elif upvotes > 500: 
     authors[k] = (upvotes, "Diabolically Evil") 

print authors 

輸出:

{'Mr. Sinister': (2, 'Cautioiusly Evil')} 

和:

{'Mr. Sinister': (4, 'Cautioiusly Evil'), 'Loki': (22, 'Justifiably Evil')} 

對於第二個例子。

0

此代碼的工作,希望這是足夠的可讀性,所以你能適應它

x = in[0] # returns a dict from your input 

for post in x.get('posts'): 
     author = post.get('author') 
     if author not in d.keys(): 
      d[author] = post 
     else: 
      d.get('author')['upvotes'] += post.get('upvotes') 

這將返回一個字典沒有重複的作者,如果它已經存在,將不會更新比分。

我試了一下你的數據和它的工作

d {'先生陰險':{'content':'我在想下午9點?','upvotes':2,'作者':'先生。 Sinister'}}

+0

您的代碼不包括同一張海報的所有投票。它只會從每位作者的第一篇文章中獲得選票。 – droravr

+0

我編輯了答案,謝謝 – Vinny

相關問題