0

below is my list of lists;

db_rows = [('a','b','c',4),
           ('a','s','f',6),
           ('a','c','d',6),
           ('a','b','f',2),
           ('a','b','c',6),
           ('a','b','f',8),
           ('a','s','f',6),
           ('a','b','f',7),
           ('a','s','f',5),
           ('a','b','f',2)]

if first three values are same in the inner list then I need to add 4th value to create new list

I need result list like this:

final_list = [('a','b','c',10),
              ('a','s','f',17),
              ('a','c','d',6),
              ('a','b','f',19)]

I have tried the below script (not working):

final_list = []
for row in db_rows:
    temp_flag=False
    temp_list = []
    val = 0
    for ref_row in db_rows:
        if row != ref_row:
            if row[0]==ref_row[0] and row[1]==ref_row[1] and row[2]==ref_row[2]:
                val = val + ref_row[3]
                temp_flag=True
    temp_list=(row[0],row[1],row[2],val)
    if temp_flag==False:
        temp_list=row
    final_list.append(temp_list)

please advice me.

3
  • Please share your attempt to achieve this result. Commented Jun 7, 2015 at 10:42
  • 3
    Try using a dictionary with the first three values as the key. Commented Jun 7, 2015 at 10:43
  • Please correct the desired output. It should be ('a','c','d',6) instead of ('a','b','d',6). Commented Jun 7, 2015 at 11:21

3 Answers 3

5

Use a dictionary as Dov Grobgeld commented, then convert the dictionary back to the list.

from collections import defaultdict

db_rows = [('a','b','c',4),
           ('a','s','f',6),
           ('a','c','d',6),
           ('a','b','f',2),
           ('a','b','c',6),
           ('a','b','f',8),
           ('a','s','f',6),
           ('a','b','f',7),
           ('a','s','f',5),
           ('a','b','f',2)]

sums = defaultdict(int)
for row in db_rows:
    sums[row[:3]] += row[3]

final_list = [key + (value,) for key, value in sums.iteritems()]

Printing final_list outputs:

[('a', 'b', 'c', 10), ('a', 's', 'f', 17), ('a', 'b', 'f', 19), ('a', 'c', 'd', 6)]

See collections.defaultdict.

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

Comments

1
from collections import defaultdict
db_rows = [('a','b','c',4),
           ('a','s','f',6),
           ('a','c','d',6),
           ('a','b','f',2),
           ('a','b','c',6),
           ('a','b','f',8),
           ('a','s','f',6),
           ('a','b','f',7),
           ('a','s','f',5),
           ('a','b','f',2)]

d = defaultdict(int)


for ele in db_rows:
    d[ele[:-1]] += ele[-1]

print([(k +(v,)) for k,v in d.items()])

Comments

0

Here, I am using OrderedDict to preserve the order in which first three values are found.

from collections import OrderedDict

db_rows = [('a','b','c',4),
           ('a','s','f',6),
           ('a','c','d',6),
           ('a','b','f',2),
           ('a','b','c',6),
           ('a','b','f',8),
           ('a','s','f',6),
           ('a','b','f',7),
           ('a','s','f',5),
           ('a','b','f',2)]

temp_dict = OrderedDict()

for x in db_rows:
    temp_dict[x[:3]] = temp_dict.get(x[:3], 0) + x[3]

final_list =  [k +(v,) for k,v in temp_dict.iteritems()]

We can now check value of final_list which gives the desired result in the order as mentioned above.

final_list
[('a', 'b', 'c', 10),
 ('a', 's', 'f', 17),
 ('a', 'c', 'd', 6),
 ('a', 'b', 'f', 19)]

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.