python满足条件的求和,根据Python中的条件求和嵌套列表

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?

解决方案

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值