6

I have a list of lists looking like this:

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

and so on..

I need just one list per user with time, cpus and mem added together. I've tried a few things but i cant make it work.

2
  • 2
    Please elaborate more. Are you trying to remove duplicates, sum up the relevant fields for a user? How do you want to handle the data Commented Apr 23, 2018 at 7:32
  • 2
    Use a dict with users as keys ? Commented Apr 23, 2018 at 7:32

4 Answers 4

6

A sweet, Pythonic and concise way to do that would be:

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])]

Output:

In : merged
Out: 
defaultdict(<function __main__.<lambda>>,
        {'user1': [1, 1, 1], 'user2': [2, 2, 2], 'user3': [3, 3, 3]})

That uses a defaultdict with a list of length 3 as its default value. The relevant user's values are updated for each element in the list.

Sign up to request clarification or add additional context in comments.

Comments

3

You can do this using a dictionary in order to group list items by user.

Then just use zip function to find out the sum for every feature from time, cpus, mem list.

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])]

Output

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

1 Comment

Note that you should not use dict as a variable name since it is a built-in function docs.python.org/3/library/functions.html .
1

What about putting the list of lists into a pandas.DataFrame. Then you could do grouping by users and apply aggregate functions like 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

1 Comment

Agreed - but why? Because of not wanting to use it or because of not knowing about it?
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

ll will be

[
 [[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]]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.