python根据日期将列表列表中的列表元素汇总为唯一 [英] python sum up list elements in list of lists based on date as unique

查看:65
本文介绍了python根据日期将列表列表中的列表元素汇总为唯一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表列表,如下所示:

i have a list of lists like below :

asd = [['14.00', '10.08', '2017-12-14'], ['14.87', '11.32', '2017-12-16'], ['10.00', '7.50', '2017-12-17'], ['15.38', '11.53', '2017-12-14'], ['13.43', '10.07', '2017-12-17'], ['12.23', '17.71', '2017-12-16'], ['11.89', '8.92', '2017-12-18'], ['25.20', '18.14', '2017-12-14'], ['20.51', '15.60', '2017-12-12'], ['24.03', '18.02', '2017-12-11'], ['12.76', '14.50', '2017-12-17'], ['10.00', '7.50', '2017-12-15'], ['11.28', '8.79', '2017-12-13'], ['12.61', '15.13', '2017-12-16'], ['10.00', '7.50', '2017-12-16'], ['19.53', '14.65', '2017-12-17'], ['10.00', '7.50', '2017-12-12'], ['10.00', '7.50', '2017-12-17'], ['12.11', '13.63', '2017-12-14'], ['11.39', '8.54', '2017-12-16'], ['10.00', '7.50', '2017-12-12'], ['13.44', '10.12', '2017-12-12'], ['12.29', '9.22', '2017-12-11'], ['15.10', '11.32', '2017-12-17'], ['25.44', '19.08', '2017-12-15'], ['10.00', '7.50', '2017-12-12'], ['24.05', '18.04', '2017-12-17'], ['34.10', '25.57', '2017-12-16'], ['16.71', '12.53', '2017-12-16'], ['12.96', '9.72', '2017-12-17'], ['21.39', '31.72', '2017-12-14'], ['10.00', '8.48', '2017-12-17'], ['12.22', '9.16', '2017-12-11'], ['10.00', '7.50', '2017-12-15'], ['22.71', '17.03', '2017-12-18'], ['11.82', '9.85', '2017-12-16'], ['12.82', '9.61', '2017-12-17'], ['21.08', '27.34', '2017-12-12'], ['14.00', '10.18', '2017-12-17'], ['15.65', '11.77', '2017-12-17'], ['22.69', '17.07', '2017-12-11'], ['10.00', '7.50', '2017-12-14'], ['10.00', '8.75', '2017-12-15'], ['11.30', '9.34', '2017-12-12'], ['19.23', '14.42', '2017-12-15'], ['19.62', '14.71', '2017-12-15'], ['21.38', '21.55', '2017-12-15'], ['12.88', '11.68', '2017-12-17'], ['35.99', '25.91', '2017-12-17'], ['22.15', '16.62', '2017-12-12'], ['10.00', '7.50', '2017-12-12'], ['13.64', '12.28', '2017-12-14'], ['10.79', '8.09', '2017-12-15'], ['12.11', '9.08', '2017-12-14'], ['16.98', '12.73', '2017-12-17'], ['14.05', '20.53', '2017-12-15'], ['18.49', '19.57', '2017-12-14'], ['12.38', '13.00', '2017-12-16'], ['42.62', '79.91', '2017-12-14'], ['24.50', '18.37', '2017-12-16'], ['175.92', '130.57', '2017-12-15'], ['46.94', '45.76', '2017-12-16'], ['10.40', '7.86', '2017-12-17'], ['16.98', '12.73', '2017-12-17'], ['10.04', '7.83', '2017-12-15'], ['30.50', '22.87', '2017-12-11'], ['19.86', '14.89', '2017-12-15'], ['14.70', '11.02', '2017-12-17'], ['11.60', '8.70', '2017-12-14'], ['12.80', '9.60', '2017-12-18'], ['10.00', '9.75', '2017-12-12'], ['20.26', '15.19', '2017-12-14'], ['13.86', '10.39', '2017-12-11'], ['17.44', '12.56', '2017-12-14'], ['14.00', '10.08', '2017-12-11'], ['10.00', '7.69', '2017-12-17'], ['13.81', '10.36', '2017-12-12'], ['38.93', '29.20', '2017-12-16'], ['12.97', '10.94', '2017-12-13'], ['14.00', '10.08', '2017-12-15'], ['21.68', '16.26', '2017-12-17'], ['14.56', '10.92', '2017-12-12'], ['15.84', '11.88', '2017-12-18'], ['29.57', '21.29', '2017-12-11'], ['23.65', '17.74', '2017-12-17'], ['15.21', '11.41', '2017-12-16'], ['11.46', '9.53', '2017-12-15'], ['17.95', '13.46', '2017-12-18'], ['21.64', '16.23', '2017-12-12'], ['23.42', '17.25', '2017-12-14'], ['10.00', '14.30', '2017-12-11'], ['12.33', '9.25', '2017-12-12'], ['14.82', '11.11', '2017-12-14'], ['18.56', '13.36', '2017-12-13'], ['15.49', '11.62', '2017-12-12'], ['21.39', '16.04', '2017-12-16'], ['18.87', '14.23', '2017-12-15'], ['10.00', '7.50', '2017-12-14'], ['38.02', '51.33', '2017-12-16'], ['19.36', '14.52', '2017-12-17'], ['18.78', '14.18', '2017-12-12'], ['10.00', '7.50', '2017-12-17'], ['15.44', '12.33', '2017-12-16'], ['13.93', '10.62', '2017-12-11'], ['10.00', '7.50', '2017-12-17'], ['32.52', '53.65', '2017-12-12'], ['24.68', '18.51', '2017-12-12'], ['10.96', '20.55', '2017-12-16'], ['14.41', '10.38', '2017-12-17'], ['27.69', '20.77', '2017-12-15'], ['14.60', '16.42', '2017-12-16'], ['10.88', '13.06', '2017-12-16'], ['14.78', '11.08', '2017-12-15'], ['14.00', '10.08', '2017-12-17'], ['10.00', '7.50', '2017-12-11'], ['23.71', '26.83', '2017-12-12'], ['10.00', '7.50', '2017-12-12'], ['10.00', '7.50', '2017-12-16'], ['13.38', '10.03', '2017-12-15'], ['10.70', '8.02', '2017-12-11'], ['14.66', '10.99', '2017-12-16'], ['17.14', '12.85', '2017-12-11'], ['10.00', '7.50', '2017-12-12'], ['10.00', '7.50', '2017-12-12'], ['10.00', '7.50', '2017-12-14'], ['16.10', '12.07', '2017-12-11'], ['19.91', '14.93', '2017-12-12'], ['11.67', '9.04', '2017-12-12'], ['14.00', '12.19', '2017-12-16'], ['16.77', '12.58', '2017-12-17'], ['14.81', '19.99', '2017-12-11'], ['11.10', '8.32', '2017-12-16'], ['10.00', '7.50', '2017-12-11']]

我希望python3函数将其中的日期作为唯一的日期,所以我们得到7个日期,现在我想根据7个唯一的日期来计算所有列表中2个元素[0]&[1]的总和

I want a python3 function to take dates out of them as unique, so we get 7 dates, now i want to calculate the sum of 2 elements [0]&[1] in all lists based on 7 dates as unique

例如,所有以"2017-12-14"作为索引[2]的列表都将被删除&创建一个新列表 [具有元素[2]的所有列表元素[0]的总和=='2017-12-14',具有元素[2] ==的所有列表元素[1]的总和2017-12-14','2017-12-14']

for example all lists that has '2017-12-14' as index[2] will be taken out & a new list is created [SUM of all list element[0] that has element[2]=='2017-12-14',SUM of all list element[1] that has element[2]=='2017-12-14','2017-12-14']

因此最终将在一个列表中返回7个新列表

so eventually 7 new lists will be returned in a list

@Willem Van Onsem,您的评论启发了我&我继续&发挥了我的作用

@Willem Van Onsem , your comments inspired me & i went ahead & did the function my self

def new(date):
    a = b = 0.00
    for each in asd:
        if date == each[2]:
            a+=float(each[0])
            b+=float(each[1])
    return [round(a,2),round(b,2),date]

dates = list(set([each[2] for each in asd]))

for date in dates:
    line = new(date)
    print (line)

ouptut:

[81.19, 60.89, '2017-12-18']
[261.84, 210.95, '2017-12-11']
[42.81, 33.09, '2017-12-13']
[446.51, 351.14, '2017-12-15']
[377.58, 336.79, '2017-12-12']
[418.1, 319.52, '2017-12-17']
[292.48, 293.25, '2017-12-14']
[422.74, 394.62, '2017-12-16']

推荐答案

您可以对Python3使用解压缩:

You can use unpacking for Python3:

from collections import defaultdict
d = defaultdict(list)
s = [['14.00', '10.08', '2017-12-14'], ['14.87', '11.32', '2017-12-16'], ['10.00', '7.50', '2017-12-17'], ['15.38', '11.53', '2017-12-14'], ['13.43', '10.07', '2017-12-17'], ['12.23', '17.71', '2017-12-16'], ['11.89', '8.92', '2017-12-18'], ['25.20', '18.14', '2017-12-14'], ['20.51', '15.60', '2017-12-12'], ['24.03', '18.02', '2017-12-11'], ['12.76', '14.50', '2017-12-17'], ['10.00', '7.50', '2017-12-15'], ['11.28', '8.79', '2017-12-13'], ['12.61', '15.13', '2017-12-16'], ['10.00', '7.50', '2017-12-16'], ['19.53', '14.65', '2017-12-17'], ['10.00', '7.50', '2017-12-12'], ['10.00', '7.50', '2017-12-17'], ['12.11', '13.63', '2017-12-14'], ['11.39', '8.54', '2017-12-16'], ['10.00', '7.50', '2017-12-12'], ['13.44', '10.12', '2017-12-12'], ['12.29', '9.22', '2017-12-11'], ['15.10', '11.32', '2017-12-17'], ['25.44', '19.08', '2017-12-15'], ['10.00', '7.50', '2017-12-12'], ['24.05', '18.04', '2017-12-17'], ['34.10', '25.57', '2017-12-16'], ['16.71', '12.53', '2017-12-16'], ['12.96', '9.72', '2017-12-17'], ['21.39', '31.72', '2017-12-14'], ['10.00', '8.48', '2017-12-17'], ['12.22', '9.16', '2017-12-11'], ['10.00', '7.50', '2017-12-15'], ['22.71', '17.03', '2017-12-18'], ['11.82', '9.85', '2017-12-16'], ['12.82', '9.61', '2017-12-17'], ['21.08', '27.34', '2017-12-12'], ['14.00', '10.18', '2017-12-17'], ['15.65', '11.77', '2017-12-17'], ['22.69', '17.07', '2017-12-11'], ['10.00', '7.50', '2017-12-14'], ['10.00', '8.75', '2017-12-15'], ['11.30', '9.34', '2017-12-12'], ['19.23', '14.42', '2017-12-15'], ['19.62', '14.71', '2017-12-15'], ['21.38', '21.55', '2017-12-15'], ['12.88', '11.68', '2017-12-17'], ['35.99', '25.91', '2017-12-17'], ['22.15', '16.62', '2017-12-12'], ['10.00', '7.50', '2017-12-12'], ['13.64', '12.28', '2017-12-14'], ['10.79', '8.09', '2017-12-15'], ['12.11', '9.08', '2017-12-14'], ['16.98', '12.73', '2017-12-17'], ['14.05', '20.53', '2017-12-15'], ['18.49', '19.57', '2017-12-14'], ['12.38', '13.00', '2017-12-16'], ['42.62', '79.91', '2017-12-14'], ['24.50', '18.37', '2017-12-16'], ['175.92', '130.57', '2017-12-15'], ['46.94', '45.76', '2017-12-16'], ['10.40', '7.86', '2017-12-17'], ['16.98', '12.73', '2017-12-17'], ['10.04', '7.83', '2017-12-15'], ['30.50', '22.87', '2017-12-11'], ['19.86', '14.89', '2017-12-15'], ['14.70', '11.02', '2017-12-17'], ['11.60', '8.70', '2017-12-14'], ['12.80', '9.60', '2017-12-18'], ['10.00', '9.75', '2017-12-12'], ['20.26', '15.19', '2017-12-14'], ['13.86', '10.39', '2017-12-11'], ['17.44', '12.56', '2017-12-14'], ['14.00', '10.08', '2017-12-11'], ['10.00', '7.69', '2017-12-17'], ['13.81', '10.36', '2017-12-12'], ['38.93', '29.20', '2017-12-16'], ['12.97', '10.94', '2017-12-13'], ['14.00', '10.08', '2017-12-15'], ['21.68', '16.26', '2017-12-17'], ['14.56', '10.92', '2017-12-12'], ['15.84', '11.88', '2017-12-18'], ['29.57', '21.29', '2017-12-11'], ['23.65', '17.74', '2017-12-17'], ['15.21', '11.41', '2017-12-16'], ['11.46', '9.53', '2017-12-15'], ['17.95', '13.46', '2017-12-18'], ['21.64', '16.23', '2017-12-12'], ['23.42', '17.25', '2017-12-14'], ['10.00', '14.30', '2017-12-11'], ['12.33', '9.25', '2017-12-12'], ['14.82', '11.11', '2017-12-14'], ['18.56', '13.36', '2017-12-13'], ['15.49', '11.62', '2017-12-12'], ['21.39', '16.04', '2017-12-16'], ['18.87', '14.23', '2017-12-15'], ['10.00', '7.50', '2017-12-14'], ['38.02', '51.33', '2017-12-16'], ['19.36', '14.52', '2017-12-17'], ['18.78', '14.18', '2017-12-12'], ['10.00', '7.50', '2017-12-17'], ['15.44', '12.33', '2017-12-16'], ['13.93', '10.62', '2017-12-11'], ['10.00', '7.50', '2017-12-17'], ['32.52', '53.65', '2017-12-12'], ['24.68', '18.51', '2017-12-12'], ['10.96', '20.55', '2017-12-16'], ['14.41', '10.38', '2017-12-17'], ['27.69', '20.77', '2017-12-15'], ['14.60', '16.42', '2017-12-16'], ['10.88', '13.06', '2017-12-16'], ['14.78', '11.08', '2017-12-15'], ['14.00', '10.08', '2017-12-17'], ['10.00', '7.50', '2017-12-11'], ['23.71', '26.83', '2017-12-12'], ['10.00', '7.50', '2017-12-12'], ['10.00', '7.50', '2017-12-16'], ['13.38', '10.03', '2017-12-15'], ['10.70', '8.02', '2017-12-11'], ['14.66', '10.99', '2017-12-16'], ['17.14', '12.85', '2017-12-11'], ['10.00', '7.50', '2017-12-12'], ['10.00', '7.50', '2017-12-12'], ['10.00', '7.50', '2017-12-14'], ['16.10', '12.07', '2017-12-11'], ['19.91', '14.93', '2017-12-12'], ['11.67', '9.04', '2017-12-12'], ['14.00', '12.19', '2017-12-16'], ['16.77', '12.58', '2017-12-17'], ['14.81', '19.99', '2017-12-11'], ['11.10', '8.32', '2017-12-16'], ['10.00', '7.50', '2017-12-11']]
for *b, a in s:
   d[a].extend(b)

new_data = {a:sum(map(float, b)) for a, b in d.items()}

输出:

{'2017-12-14': 585.73, '2017-12-16': 817.3600000000001, '2017-12-18': 142.08, '2017-12-17': 737.62, '2017-12-13': 75.89999999999999, '2017-12-12': 714.3699999999999, '2017-12-15': 797.65, '2017-12-11': 472.79}

或者,使用 itertools.groupby 的较短解决方案:

Or, a shorter solution using itertools.groupby:

import itertools
new_data = {a:sum(sum(map(float, i[:-1])) for i in list(b)) for a, b in itertools.groupby(sorted(s, key=lambda x:x[-1]), key=lambda x:x[-1])}

输出:

{'2017-12-16': 817.36, '2017-12-17': 737.62, '2017-12-18': 142.08, '2017-12-14': 585.7299999999999, '2017-12-15': 797.6500000000001, '2017-12-13': 75.9, '2017-12-11': 472.7900000000001, '2017-12-12': 714.37}

关于您最近的评论,这应该可行:

Regarding your recent comment, this should work:

new_data = [(a, list(b)) for a, b in itertools.groupby(sorted(s, key=lambda x:x[-1]), key=lambda x:x[-1])]
second_final_data = {a:list(zip(*h)) for a, h in new_data}
final_data = {a:[sum(map(float, c)), sum(map(float, d))] for a, [c, d, e] in second_final_data.items()}

输出:

{'2017-12-16': [422.74, 394.62], '2017-12-17': [418.09999999999997, 319.5199999999999], '2017-12-18': [81.19000000000001, 60.89000000000001], '2017-12-15': [446.51, 351.1399999999999], '2017-12-14': [292.47999999999996, 293.25], '2017-12-13': [42.81, 33.089999999999996], '2017-12-12': [377.58000000000004, 336.79], '2017-12-11': [261.84000000000003, 210.95000000000002]}

这篇关于python根据日期将列表列表中的列表元素汇总为唯一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆