简体   繁体   English

Python - 将元组列表转换为形状奇怪的其他元组列表

[英]Python - List of tuples into oddly shaped other list of tuples

Confused as to how to create this final part of my function.对如何创建我的函数的最后一部分感到困惑。

The function below needs to read in data, start year and end year.下面的函数需要读入数据,开始年份和结束年份。

Consider the following:考虑以下:

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

The part I am having trouble with is:我遇到问题的部分是:

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

This is the expected output I need for casualties_by_year[]:这是我需要的 cavalities_by_year[] 的预期输出:

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

So what I'm trying to do is construct a list of tuples, in the arrangement shown above, for the final list going into L. it's tuple[0] for year, tuple[6] for deaths, tuple[7] for missing, tuple[8] for injured.所以我想要做的是构建一个元组列表,按照上面显示的排列,对于进入 L 的最终列表。它是元组 [0] 表示年份,元组 [6] 表示死亡,元组 [7] 表示缺失, tuple[8] 表示受伤。

How can I correct that final if/else to get the list of tuples I'm looking for?我如何更正最终的 if/else 以获得我正在寻找的元组列表?

List comprehension?列表理解?

EDIT- This is what "data" looks as it's input into the function:编辑-这是“数据”在输入到函数时的样子:

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

If you want a tuple, make a tuple.如果你想要一个元组,就创建一个元组。 It's that simple.就这么简单。 You can still use parentheses notation normally, but sometimes you need the constructor.您仍然可以正常使用括号表示法,但有时您需要构造函数。

But first, never name a variable the same as some important python type.但首先,永远不要将变量命名为某些重要的 Python 类型。 You want to create an instance of type tuple, but you have a variable named tuple, so now "tuple" means that local variable, and you can't access the tuple constructor, because you've hidden its name.您想创建一个类型为 tuple 的实例,但是您有一个名为 tuple 的变量,所以现在“tuple”表示该局部变量,并且您无法访问 tuple 构造函数,因为您隐藏了它的名称。 I renamed it to "year_data"我将其重命名为“year_data”

Now, when you add the tuples together, that gets slightly more interesting.现在,当您将元组加在一起时,这会变得稍微有趣一些。 See Python element-wise tuple operations like sum请参阅Python 元素元元组操作,如 sum

The rest is already there.其余的已经在那里了。 You just have to use it.你只需要使用它。 I deleted some irrelevant stuff.我删除了一些无关的东西。

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:
            # https://stackoverflow.com/a/497894/1766544
            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))] [(2020, (53, 0, 1689))]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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