Sum nested lists based on condition in Python

后端 未结 6 1609
感情败类
感情败类 2021-01-29 01:46

I have a nested list looking like this:

[[\'Vienna\',\'2012\', 890,503,70],[\'London\',\'2014\', 5400, 879,78],
 [\'London\',\'2014\',4800,70,90],[\'Bern\',\'201         


        
6条回答
  •  离开以前
    2021-01-29 02:42

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

提交回复
热议问题