sumifs function in python

前端 未结 2 1806
刺人心
刺人心 2021-01-29 09:03

I have a list of lists that looks like:

[[\'chr1\', \'3088\', \'1\', 744, \'L1MCc_dup1\']
[\'chr1\', \'3089\', \'1\', 744, \'L1MCc_dup1\']
[\'chr1\', \'3090\', \         


        
2条回答
  •  梦谈多话
    2021-01-29 09:42

    Use a dictionary:

    d = {}
    for row in my_list:
        key = row[4]
        value = int(row[2])
        d[key] = d.get(key, 0) + value
    

    After this loop, d will map the key values in the last column to the desired sums.

    You could also use collections.defaultdict instead of a normal dictionary.

提交回复
热议问题