如何在Python中对列表列表进行分组?

时间:2018-04-23 07:23:40

标签: python list

我有一个列表如下:

['user1', time, cpus, mem]  
['user1', time, cpus, mem]
['user2', time, cpus, mem]
['user3', time, cpus, mem]

依旧......

我需要每个用户只有一个列表,时间,cpus和mem加在一起。我尝试了一些事情,但我无法让它发挥作用。

4 个答案:

答案 0 :(得分:1)

您可以使用dictionary执行此操作,以便按user对列表项进行分组。

然后只需使用zip函数查找time, cpus, mem列表中每个要素的总和。

mylist = [['user1', 1, 6, 8], 
['user1', 2, 7, 9],
['user2', 3, 7, 5],
['user3', 4, 7, 3]]

dict = {}
for elem in mylist:
  if elem[0] not in dict:
    dict[elem[0]] = []
  dict[elem[0]].append(elem[1:])

for key in dict:
  dict[key] = [sum(i) for i in zip(*dict[key])]

输出

In[1]: dict
Out[1]: {'user1': [3, 13, 17], 'user2': [3, 7, 5], 'user3': [4, 7, 3]}

答案 1 :(得分:1)

这样做的甜蜜,Pythonic和简洁的方法是:

1

输出:

from collections import defaultdict

l = [                  
    ['user1', 0, 1, 0],                                                    
    ['user2', 2, 2, 2],      
    ['user3', 2, 2, 1],
    ['user3', 1, 1, 2],
    ['user1', 1, 0, 1],
]


merged = defaultdict(lambda: [0, 0, 0])

for user, *values in l:               
     merged[user] = [sum(i) for i in zip(values, merged[user])]

使用长度为In : merged Out: defaultdict(<function __main__.<lambda>>, {'user1': [1, 1, 1], 'user2': [2, 2, 2], 'user3': [3, 3, 3]}) 的{​​{1}}作为默认值。为列表中的每个元素更新相关用户的值。

答案 2 :(得分:0)

如何将列表列表放入pandas.DataFrame。 然后,您可以按用户进行分组并应用sum()等集合函数。

df = pd.DataFrame()
df['users'] = ['user1', 'user1','user2', 'user3']
df['time'] = [1,2,3,4]
df['mem'] = [128, 256, 1024, 2048]
df['cpu'] = [4, 2, 16, 32]

In: df
out: 
       users  time  cpu   mem
0  user1     1    4   128
1  user1     2    2   256
2  user2     3   16  1024
3  user3     4   32  2048

In: df.groupby('users').sum()
Out:
       time  cpu   mem
users                 
user1     3    6   384
user2     3   16  1024
user3     4   32  2048

答案 3 :(得分:0)

def gropulistsinlist(data,columns):
    ll=[[]]
    #ll.append([[3,4,5,6,7,7]])
    #columns=[4,5]
    #data=[[3,4,5,6,7,8],[3,4,5,6,7,7],[3,4,5,6,7,9],[3,4,5,6,7,8]]
    #data=[[3,4,5,6,7,7],[3,4,5,6,7,8],[3,4,5,6,7,7],[3,4,5,6,7,9],[3,4,5,6,7,8]]
    for d in data:
        addnew=0
        addwhat=0
        addwhere=0
        for index,l in enumerate(ll):
            if len(l)and([l[0][ii] for ii in columns]== [d[iii] for iii in columns]):
                addwhere=index
                addwhat=d
                addnew=0
                #print(ll)
                #print()
                break
            addnew=1
            addwhat=d
        if(addnew):
            ll.append([addwhat])
        else:
            ll[index].append(addwhat)
   ll.pop(0)
   return ll

将为

[
 [[3, 4, 5, 6, 7, 7], [3, 4, 5, 6, 7, 7]],
 [[3, 4, 5, 6, 7, 8],[3, 4, 5, 6, 7, 8]],
 [[3, 4, 5, 6, 7, 9]]]