Python - 元组列表变成奇形怪状的其他元组列表

Python - List of tuples into oddly shaped other list of tuples

对如何创建函数的最后一部分感到困惑。

下面的函数需要读入数据,开始年份和结束年份。

考虑以下几点:

def summary_statistics(data, year_start, year_end):
    earthquake_count_by_year = []
    total_damages_by_year = []
    casualties_by_year = []
    year_count = {}
    dmg_sum = {}
    casualties = {}
    
    years = []
    year_start = int(year_start)
    year_end = int(year_end)
    
    if year_end >= year_start:
        
        #store year range into list
        years = list(range(year_start, year_end+1))
        
        for index, tuple in enumerate(data):
            #values to be summed for each year in dmg_sum
            yr = tuple[0]
            dmgs = tuple[9]
            deaths = tuple[6]
            missing = tuple[7]
            injured = tuple[8]
            
            #if year in range of years (year_start - year_end)
            if tuple[0] in years:
                #if year does not exist in dict, set it to 0
                if tuple[0] not in year_count:
                    year_count[tuple[0]] = 0
                #count number of occurences for each year
                year_count[tuple[0]] += 1
                
                if tuple[0] not in dmg_sum:
                    dmg_sum[tuple[0]] = dmgs
                else:
                    dmg_sum[tuple[0]] += dmgs
                    
                if tuple[0] not in casualties:
                    casualties[tuple[0]] = list(deaths + ',' + missing + ',' + injured)
                else:
                    casualties[tuple[0]] += list(deaths + ',' + missing + ',' + injured)

        earthquake_count_by_year = list(year_count.items())
        total_damages_by_year = list(dmg_sum.items())
        casualties_by_year = list(casualties.items())

    
    L = [[earthquake_count_by_year], [total_damages_by_year], [casualties_by_year]]
    print(L)
    return L

我遇到问题的部分是:

                if tuple[0] not in casualties:
                    casualties[tuple[0]] = list(deaths + ',' + missing + ',' + injured)
                else:
                    casualties[tuple[0]] += list(deaths + ',' + missing + ',' + injured)

这是我需要的预期输出 casualties_by_year[]:

[(2020, (deaths, missing, injured)), (2019, (deaths, missing, injured)), (2018, (deaths, missing, injured))]

所以我想做的是构建一个元组列表,按照上面显示的排列,最后一个列表进入 L。它是年份的元组 [0],死亡的元组 [6],元组[7] 表示失踪,tuple[8] 表示受伤。

如何更正最后的 if/else 以获得我正在寻找的元组列表?

列表理解?

编辑- 这就是“数据”输入函数时的样子:

[(2020, 1, 6.0, 'CHINA:  XINJIANG PROVINCE', 39.831, 77.106, 1, 0, 2, 0), (2020, 1, 6.7, 'TURKEY:  ELAZIG AND MALATYA PROVINCES', 38.39, 39.081, 41, 0, 1600, 0), (2020, 1, 7.7, 'CUBA: GRANMA;  CAYMAN IS;  JAMAICA', 19.44, -78.755, 0, 0, 0, 0), (2020, 2, 6.0, 'TURKEY: VAN;  IRAN', 38.482, 44.367, 10, 0, 60, 0), (2020, 3, 5.4, 'BALKANS NW:  CROATIA:  ZAGREB', 45.897, 15.966, 1, 0, 27, 6000.0)]

如果你想要一个元组,就制作一个元组。就这么简单。你仍然可以正常使用括号表示法,但有时你需要构造函数。

但首先,永远不要将变量命名为与某些重要 python 类型相同的变量。你想创建一个元组类型的实例,但你有一个名为元组的变量,所以现在“元组”表示局部变量,你不能访问元组构造函数,因为你隐藏了它的名字。我将其重命名为“year_data”

现在,当您将元组加在一起时,会变得更有趣。参见 Python element-wise tuple operations like sum

其余的已经在那里了。你只需要使用它。我删除了一些不相关的东西。

import operator

data = [
    (2020, 1, 6.0, 'CHINA:  XINJIANG PROVINCE', 39.831, 77.106, 1, 0, 2, 0), 
    (2020, 1, 6.7, 'TURKEY:  ELAZIG AND MALATYA PROVINCES', 38.39, 39.081, 41, 0, 1600, 0), 
    (2020, 1, 7.7, 'CUBA: GRANMA;  CAYMAN IS;  JAMAICA', 19.44, -78.755, 0, 0, 0, 0), 
    (2020, 2, 6.0, 'TURKEY: VAN;  IRAN', 38.482, 44.367, 10, 0, 60, 0), 
    (2020, 3, 5.4, 'BALKANS NW:  CROATIA:  ZAGREB', 45.897, 15.966, 1, 0, 27, 6000.0)
]

def summary_statistics(data):
    casualties = {}
    for index, year_data in enumerate(data):
        year = year_data[0]
        deaths = year_data[6]
        missing = year_data[7]
        injured = year_data[8]
        casualties_data = (deaths, missing, injured)

        if year not in casualties:
            casualties[year] = casualties_data
        else:
            # 
            casualties[year] = tuple(map(operator.add, casualties[year], (deaths, missing, injured)))

    casualties_by_year = list(casualties.items())
    return casualties_by_year
    
result = summary_statistics(data)
print(result)

[(2020, (53, 0, 1689))]