列表中元素的总和

Sum of elements in the lists

我的 csv 数据如下所示:

ID, DATE, mm

251691,01/01/2016,16.6
251691,02/01/2016,4.4
251691,08/01/2016,3.7
120530,07/01/2019,55.5
120530,22/04/2019,1.8

我想对每年的所有“mm”和ID求和

我想要得到的结果是这样的:

('251691', '2016')  :  (sum_of_mm_for_each_station_in_that_year, number_of_total_rain_days)

那它:

('251691', '2016')  :  (24.7, 3)

这是我的代码:

answer = {}
with open(filename) as f:
        header = line_to_list(f.readline())
        if header[0] != 'ID':
            raise Exception("Bad")
        for line in f.readlines():
            row = line_to_list(line)
            date = row[1]
            mm = row[2]
            year = date [-4:]
            answer[row[0],year] = (mm, date)

函数line_to_list:

def line_to_list(line):
    ''' converts a csv line (string) to a list of items '''
    line = line.rstrip('\n')
    return [s for s in line.split(',')]

我无法对元素求和。 我想每年按 ID 分组并对元素求和,但我不能使用 pandas 等任何模块

有人可以帮助我吗? 谢谢!

我已对代码进行了一些更改以适应您的更改:-

answer = {}
def line_to_list(line):
    ''' converts a csv line (string) to a list of items '''
    line = line.rstrip('\n')
    return [s for s in line.split(',')]
with open("file.txt") as f:
    header = line_to_list(f.readline())
    if header[0] != 'ID':
        raise Exception("Bad")
    for line in f.readlines():
        row = line_to_list(line)
        date = row[1]
        mm = float(row[2])
        year = date [-4:]
        count = 1
        if (row[0], year) in answer:
            mm += answer[(row[0],year)][0]
            count += answer[(row[0],year)][1]
        answer[(row[0],year)] = (mm, count)
print(answer)

结果:-

{('251691', '2016'): (24.7, 3), ('120530', '2019'): (57.3, 2)}

解释:-

由于列表是可变的和可哈希的,我们不能直接使用它来引用它作为字典的键,因为键需要被哈希。所以我明确地把它转换成元组,然后使用它。

代码部分 if (row[0], year) in answer: 将检查元组是否已存在于字典中,如果存在,它将附加其值并使用 mm += answer[(row[0],year)][0] 和 [=14= 行增加计数].我认为代码的另一部分很清楚。