我有一个列表如下:
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]的总和
例如,所有将“2017-12-14”作为索引[2]的列表将被取出&创建新列表[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个新列表
@Willem Van Onsem,你的评论激发了我的灵感。我继续前进做了我自己的功能
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']
答案 0 :(得分:1)
您可以使用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
的简短解决方案:
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}
关于你最近的评论,这应该有效:
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]}
答案 1 :(得分:0)
你可以使用pandas来分组和总和。
import pandas as pd
data = [['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']]
df = pd.DataFrame(data, columns=['val1', 'val2','date'])
df[['val1', 'val2']] = df[['val1', 'val2']].astype(float)
print df.groupby('date')[['val1', 'val2']].sum()
输出:
val1 val2
date
2017-12-11 261.84 210.95
2017-12-12 377.58 336.79
2017-12-13 42.81 33.09
2017-12-14 292.48 293.25
2017-12-15 446.51 351.14
2017-12-16 422.74 394.62
2017-12-17 418.10 319.52
2017-12-18 81.19 60.89
答案 2 :(得分:0)
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding DataContext.DgSelectionChangedCommand,
RelativeSource={RelativeSource AncestorType={x:Type DataGrid}, AncestorLevel=2}}"
CommandParameter="{Binding}" />
</i:EventTrigger>
</i:Interaction.Triggers>
答案 3 :(得分:0)
这是一个不同的答案:
from functools import reduce
from itertools import groupby
lst = [...]
# Make the list summable
lst2 = map(lambda x: [float(x[0]), float(x[1]), x[2]], lst)
# sort it for group by
lst3 = sorted(lst2, key=lambda x: x[-1])
lst4 = groupby(lst3, key=lambda x: x[-1])
# Compute the result
result = map(lambda x: reduce(lambda g, h: [g[0] + h[0], g[1] + h[1]], x[1], [0, 0]) + [x[0]], lst4)
在最后一行中,我们使用reduce来同时对两个参数求和。这可以改写成一个单行,但读起来会很糟糕。
如果你想让它更清晰,你可以改写:
def sumPair(pair1, pair2):
return [pair1[0] + pair2[0], pair1[1] + pair2[1]]
def byDate(line):
return line[-1]
def makeSum(date, data):
return reduce(sumPair, data, [0, 0]) + [date]
def fixFloat(line):
return [float(line[0]), float(line[1]), line[2]]
def groupSort(lst, key):
return groupby(sorted(lst, key=key), key=key)
lst1 = [...]
lst2 = map(fixFloat, lst1)
lst3 = groupSort(lst2, byDate)
result = map(lambda x: makeSum(x[0], x[1]), lst3)