1

I have a nested list looking like this:

[['Vienna','2012', 890,503,70],['London','2014', 5400, 879,78],
 ['London','2014',4800,70,90],['Bern','2013',300,450,678], 
 ['Vienna','2013', 700,850,90], ['Bern','2013',500,700,90]]

What I want to do is summing every integervalue in the sublist with another sublist if city and year are equal. I first thought of a dictionary with city and year as key, but it caused problems sorting it.

Then I had: {('Vienna','2012'):[890,503,70],('Bern','2013'):[800,1150,768],...}

I also tried something like this:

[sum(x) for x in zip(*list) if x[0] == x[0]] but of course it did not work.

Can I do something like this with a nested list to so sorting it by city and year would be easier?

Krish Munot
  • 1,093
  • 2
  • 18
  • 29
IamnotaRabbit
  • 173
  • 1
  • 9

6 Answers6

3

You could construct a result dict where key is tuple of first two items in the original lists and value is list of numbers. Every time you add value to dict you could use get to either return existing element or given default value, in this case empty list.

Once you have the existing list and list to add you can use zip_longest with fillvalue to get numbers to sum from both lists. zip_longest returns tuples of length 2 containing one number from each list. In case one list is longer than other fillvalue is used as default so this will also work in case lists have different lengths. Finally list comprehension could used to sum each item for a new value:

from itertools import zip_longest

l = [
    ['Vienna','2012', 890,503,70],['London','2014', 5400, 879,78],
    ['London','2014',4800,70,90],['Bern','2013',300,450,678],
    ['Vienna','2013', 700,850,90], ['Bern','2013',500,700,90]
]

res = {}
for x in l:
    key = tuple(x[:2])
    res[key] = [i + j for i, j in zip_longest(res.get(key, []), x[2:], fillvalue=0)]

print(res)

Output:

{('Vienna', '2013'): [700, 850, 90], ('London', '2014'): [10200, 949, 168], 
 ('Vienna', '2012'): [890, 503, 70], ('Bern', '2013'): [800, 1150, 768]}  

If you want to sort the cities alphabetically and years latest first you could pass custom key to sorted:

for item in sorted(res.items(), key=lambda x: (x[0][0], -int(x[0][1]))):
    print(item)

Output:

(('Bern', '2013'), [800, 1150, 768])
(('London', '2014'), [10200, 949, 168])
(('Vienna', '2013'), [700, 850, 90])
(('Vienna', '2012'), [890, 503, 70])
niemmi
  • 17,113
  • 7
  • 35
  • 42
  • This looks very good. Use `res = collections.OrderedDict()` to improve. – Gribouillis Dec 10 '16 at 10:26
  • Also with `defaultdict(list)` you can just write `res[key]` instead of `res.get(key, [])` – Poloq Dec 10 '16 at 10:39
  • @Gribouillis: This depends entirely on the ordering of expected output. If items are needed in the same order as in original list then it would be useful. – niemmi Dec 10 '16 at 10:43
2

You can achieve the result you want by simply using a dictionary store all the country names and years as one value. Each key in the dictionary is a tuple of the country name and the corresponding year.

Ex: key = (country,year).

This allows us to have the unique values that we need to group them by.

L = [
        ['Vienna','2012', 890,503,70],['London','2014', 5400, 879,78],
        ['London','2014',4800,70,90],['Bern','2013',300,450,678],
        ['Vienna','2013', 700,850,90], ['Bern','2013',500,700,90]
    ]

    countries = {}

    for list in L:
        key = tuple(list[0:2])
        values = list[2:]
        if key in countries:
            countries[key] = [sum(v) for v in zip(countries[key],values)]
        else:
            countries[key] = values

    print(countries)

out:

 {
     ('Vienna', '2012'): [890, 503, 70],
     ('London', '2014'): [10200, 949, 168],
     ('Bern', '2013'): [800, 1150, 768],
     ('Vienna', '2013'): [700, 850, 90]
}
GameO7er
  • 2,028
  • 1
  • 18
  • 33
A Merii
  • 574
  • 9
  • 21
0

You should maintain a dictionary as you have outlined in the question. Something like this will help,

cities = {}
for a in list:
    city_key = a[:1]
    if city_key in cities:
        cities[city_key] = [a + b for a, b in zip(a[2:], cities[city_key])]
    else:
        cities[city_tuple] = a[2:]
martianwars
  • 6,380
  • 5
  • 35
  • 44
  • Ok i already did it nearly the way you did. the Problem is that I need to sort the output. It should look like this: Vienna 2014 .... 2013 .... what would the easiest way to do this? – IamnotaRabbit Dec 10 '16 at 10:28
  • You can easily sort the keys http://stackoverflow.com/questions/9001509/how-can-i-sort-a-dictionary-by-key – martianwars Dec 10 '16 at 10:30
0

One way is to split the list of lists into a dict by the key you want (the city and year). Also the defaultdict helps squashing all distances into a flat list

>>> from collections import defaultdict
>>> dct = defaultdict(list)
>>> for item in lst:
...    dct[(item[0], item[1])].extend(item[2:])

Now dct has the integers grouped by the city and year:

>>> dct
defaultdict(<type 'list'>, {('Vienna', '2013'): [700, 850, 90], ('London', '2014'): [5400, 879, 78, 4800, 70, 90], ('Vienna', '2012'): [890, 503, 70], ('Bern', '2013'): [300, 450, 678, 500, 700, 90]})

And you can just sum them:

>>> for key in dct:
...    print(key, sum(dct[key]))
... 
(('Vienna', '2013'), 1640)
(('London', '2014'), 11317)
(('Vienna', '2012'), 1463)
(('Bern', '2013'), 2718)
ev-br
  • 24,968
  • 9
  • 65
  • 78
0
nl = [['Vienna','2012', 890,503,70],['London','2014', 5400, 879,78],
      ['London','2014',4800,70,90],['Bern','2013',300,450,678],
      ['Vienna','2013', 700,850,90], ['Bern','2013',500,700,90]]
d = {}
for l in nl:
    key = l[0] , l[1]
    value = l[2:]
    if key not in d:
        d[key] = value
    else:
        d[key] = [sum(i)for i in zip(d[key], value)]
print(d)

out:

{('Vienna', '2012'): [890, 503, 70], ('London', '2014'): [10200, 949, 168], ('Bern', '2013'): [800, 1150, 768], ('Vienna', '2013'): [700, 850, 90]}
宏杰李
  • 11,820
  • 2
  • 28
  • 35
0

The solution using itertools.groupby and operator.itemgetter functions:

import itertools, operator

l = [['Vienna','2012', 890,503,70],['London','2014', 5400, 879,78],
 ['London','2014',4800,70,90],['Bern','2013',300,450,678],
 ['Vienna','2013', 700,850,90], ['Bern','2013',500,700,90]]

getter = operator.itemgetter(0, 1)  # the sequence to be grouped(first two items)
summed = [[k[0],k[1],sum(sum(d[2:]) for d in list(group))]
          for k, group in itertools.groupby(sorted(l, key=getter), getter)]

print(summed)

The output:

[['Bern', '2013', 2718], ['London', '2014', 11317], ['Vienna', '2012', 1463], ['Vienna', '2013', 1640]]
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105