Python - 检查列表中的重复项并将重复项一起添加以使用求和值更新列表

时间:2017-08-09 22:13:06

标签: python python-3.x

我的问题的目标是阅读如下的帖子:

([
{
    '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 /难以阅读。很抱歉给您带来不便。

谢谢!

3 个答案:

答案 0 :(得分: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]}

相关:

答案 1 :(得分: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')}

对于第二个例子。

答案 2 :(得分: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   {'先生。 Sinister':{'content':“我想晚上9点?”,'upvotes':2,'作者':'先生险恶'}}