Python社区  »  Python

Python列表操作

Craig Ellis • 2 周前 • 42 次点击  

我有一个Python问题,可以用多个嵌套 for 循环,但我想知道是否有一个更简单的方法来解决这个问题,也许通过添加列表项和删除重复项。

main_list = [["user1@test.com", "Administration", "100"],
             ["user2@test.com", "Testing", "30"],
             ["user2@test.com", "Development", "45"],
             ["user2@test.com", "Development", "90"],
             ["user2@test.com", "Development", "35"],
             ["user3@test.com", "Development", "400"],
             ["user3@test.com", "Administration", "95"],
             ["user3@test.com", "Testing", "200"]]

我需要合并电子邮件地址和类别(前两个列表元素)并将重复的第三个条目添加到一起。

所以[user2,development]来自:

["user2@test.com", "Development", "45"],
["user2@test.com", "Development", "90"],
["user2@test.com", "Development", "35"],

致:

["user2@test.com", "Development", "170"]

谢谢您!

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/54288
 
42 次点击  
分享到微博
文章 [ 4 ]  |  最新文章 2 周前
VegaS
Reply   •   1 楼
VegaS    3 月前

main_dict = {}
for email, category, value in main_list:
    token = (email, category)
    if token in main_dict:
        main_dict[token] += int(value)
    else:
        main_dict[token] = int(value)

main_list_converted = []
for k, v in main_dict.iteritems():
    main_list_converted.append(list(k) + [v])

main_list_converted.sort()

"""
for item in main_list_converted:
    print (item)

[['user1@test.com', 'Administration', 100]
['user2@test.com', 'Development', 170]
['user2@test.com', 'Testing', 30]
['user3@test.com', 'Administration', 95]
['user3@test.com', 'Development', 400]
['user3@test.com', 'Testing', 200]]
"""
Alexey
Reply   •   2 楼
Alexey    3 月前

熊猫模块:

import pandas as pd
out_d = (pd.DataFrame(main_list).set_index([0,1])[2].astype(int).groupby(level=[0,1]).sum()).to_dict()
out_d

Out[1]:
{('user1@test.com', 'Administration'): 100,
 ('user2@test.com', 'Development'): 170,
 ('user2@test.com', 'Testing'): 30,
 ('user3@test.com', 'Administration'): 95,
 ('user3@test.com', 'Development'): 400,
 ('user3@test.com', 'Testing'): 200}

#for list
[[u[0],u[1],v] for u,v in out_d.items()]

Out[2]:
[['user1@test.com', 'Administration', 100],
 ['user2@test.com', 'Development', 170],
 ['user2@test.com', 'Testing', 30],
 ['user3@test.com', 'Administration', 95],
 ['user3@test.com', 'Development', 400],
 ['user3@test.com', 'Testing', 200]]
Mykola Zotko
Reply   •   3 楼
Mykola Zotko    3 月前

你可以使用这个函数 groupby() :

from itertools import groupby
from operator import itemgetter

iget = itemgetter(0, 1)
[[*k, sum(int(i[2]) for i in g)] for k, g in groupby(sorted(main_list), key=iget)]

[['user1@test.com', 'Administration', 100],
 ['user2@test.com', 'Development', 170],
 ['user2@test.com', 'Testing', 30],
 ['user3@test.com', 'Administration', 95],
 ['user3@test.com', 'Development', 400],
 ['user3@test.com', 'Testing', 200]]
Rakesh
Reply   •   4 楼
Rakesh    3 月前

使用 collections.defaultdict

from collections import defaultdict


main_list = [["user1@test.com", "Administration", "100"],
             ["user2@test.com", "Testing", "30"],
             ["user2@test.com", "Development", "45"],
             ["user2@test.com", "Development", "90"],
             ["user2@test.com", "Development", "35"],
             ["user3@test.com", "Development", "400"],
             ["user3@test.com", "Administration", "95"],
             ["user3@test.com", "Testing", "200"]]
result = defaultdict(int)
for k, v, n in main_list:
    result[(k, v)] += int(n)
result = [list(k) + [v] for k, v in result.items()]
print(result)

输出:

[['user1@test.com', 'Administration', 100],
 ['user2@test.com', 'Testing', 30],
 ['user2@test.com', 'Development', 170],
 ['user3@test.com', 'Development', 400],
 ['user3@test.com', 'Administration', 95],
 ['user3@test.com', 'Testing', 200]]