简体   繁体   中英

Incorporate duplicates in a list of tuples into a dictionary summing the values

Hi I wish to convert this list of tuples into dictionary. As I am new to python, I am figuring out ways to convert into dictionary. I could only convert into dictionary if there is only one value. In my case, there are two values in it.I will demonstrate with more details below:

    `List of tuples: [('Samsung', 'Handphone',10), ('Samsung', 'Handphone',-1),
('Samsung','Tablet',10),('Sony','Handphone',100)]`

As you can see above, I wish to identify 'Samsung' as the key and 'Handphone' and '10' as the values with respect to the key.

My desired output would be:

  `Output: {'Sony': ['Handphone',100], 'Samsung': ['Tablet',10,'Handphone', 9]}`

As you can see above, the item 'handphone' and 'tablet' are group according to the key values which in my case is Sony and Samsung. The quantity of the item are are added / subtracted if they belong to the same item and same key (Samsung or Sony).

I would really appreciate any suggestions and ideas that you guys have in order to achieve the above output. I really ran out of ideas. Thank you.

Good opportunity for defaultdict

from collections import defaultdict

the_list = [
    ('Samsung', 'Handphone', 10), 
    ('Samsung', 'Handphone', -1), 
    ('Samsung', 'Tablet', 10),
    ('Sony', 'Handphone', 100)
]

d = defaultdict(lambda: defaultdict(int))

for brand, thing, quantity in the_list:
    d[brand][thing] += quantity

Result will be

{
    'Samsung': {
        'Handphone': 9, 
        'Tablet': 10
    },
    'Sony': {
        'Handphone': 100
    }
}

You can do this with a dictionary comprehension

What you really want are tuples for keys, which will be the company and the device.

tuples = [('Samsung', 'Handphone',10), 
          ('Samsung', 'Handphone',-1),
          ('Samsung','Tablet',10),
          ('Sony','Handphone',100)]

d = {}
for company, device, n in tuples:
    key = (company, device)
    d[key] = d.get(key, 0) + n

Your output has a problem, that can be seen with proper identation:

{
    'Sony': ['Handphone',100], 
    'Samsung': ['Tablet',10],
    ['Handphone', 9]
}

Handphone isn't a part of 'Samsung', you can do a list of lists to get:

{
    'Sony': [
        ['Handphone',100]
    ],
    'Samsung': [
        ['Tablet',10],
        ['Handphone', 9]
    ]
}

With:

my_list = [('Samsung', 'Handphone',10), ('Samsung', 'Handphone',-1), ('Samsung','Tablet',10),('Sony','Handphone',100)]

result = {}
for brand, device, value in my_list:
    # first verify if key is present to add list for the first time
    if not brand in result:
        result[brand] = []
    # then add new list to already existent list
    result[brand] += [[device, value]]

But I think the best format would be a dict:

{
    'Sony': {
        'Handphone': 100
    },
    'Samsung': {
        'Tablet': 10,
        'Handphone': 9
    }
}

That would be like:

my_list = [('Samsung', 'Handphone',10), ('Samsung', 'Handphone',-1), ('Samsung','Tablet',10),('Sony','Handphone',100)]

result = {}
for brand, device, value in my_list:
    # first verify if key is present to add dict for the first time
    if not brand in result:
        result[brand] = {}
    # then add new key/value to already existent dict
    result[brand][device] = value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM