比较字典中的键值作为每天的用电量
Comparing key-values in a dictionary as electrical usage per day
我有一份清单,记录了特定建筑物的用电情况。采样每 30 分钟进行一次。我正在使用字典按 day/month 对样本进行分组,之后我想我可以取每一天的最大值并减去下一天 - 前一天,这样我就可以得到那天的总数。其中总计为千瓦时。
我遇到的一个问题是计算第一个 day/month 的第一个最大值,因为我没有什么可比较的,因为那是第一个最大样本?如果有更好的方法请告诉?
我计划处理的方式是在那段时间找出最小最大值之间的差异,直到我获得足够的数据。
我创建了一个list of list作为例子。第一列将是月份和第二个记录值。
下面显示了按月分组的字典,然后找到最大值。
你可以看到,一旦找到最大值和差异,第 1 个月将不会被计算在内?我应该得到的结果是 {1:4, 2:10, 3:25} 这是按月计算的总数然后
今年 {1:39}?
dict1 = {1: [1, 3, 4, 5], 2: [7, 8, 9, 10, 15], 3: [20, 25, 40]}
dict2 = {1: 5, 2: 15, 3: 40}
这是到目前为止的代码,因为我的方法不起作用,所以我注释掉了获取年和月总数的操作?
list1 = [[1, 1],
[1, 1],
[1, 0],
[1, 3],
[1, 4],
[1, 5], # max
[2, 7],
[2, 8],
[2, 9],
[2, 10],
[2, 15], # max
[3, 20],
[3, 25],
[3, 40]] # max
# using min/max of each day/month I'll miss the usage between the last value for the
# day/month and
# first value of next day/month.
# ex: day 1: 5 - 1 = 4 , day 2: = 9 - 7 = 2, day 3: 40 - 12 = 28
# missed usage from 5 to 7 and 9 to 12
# taking the difference between max values of each day/month
# ex: day 1: = 5, day 2: = 9, day 3 = 40 day 1 = 4, day 2 = 31
#
#
dict1 = {}
dict2 = {}
dict3 = {}
for i, j in list1:
if j > 0: # Samples will always be greater than zero so eliminate any sample that's not.
dict1.setdefault(i, []).append(j) # Group all the samples by day/month.
dict1[i] = sorted(list(set(dict1[i]))) # Check for duplicate samples
max_ = max(dict1[i])
dict2[i] = max_
print(dict1)
print(dict2)
您可以使用字典理解来做您需要的事情:
- 将
list1
读入dict1
:
list1 = [[1, 1], [1, 1], [1, 0], [1, 3], [1, 4], [1, 5],
[2, 7], [2, 8], [2, 9], [2, 10], [2, 15],
[4, 20], [4, 25], [4, 40]]
months = sorted(set(l[0] for l in list1))
dict1 = {m: sorted(set(l[1] for l in list1 if l[0]==m and l[1]>0)) for m in months}
>>> dict1
{1: [1, 3, 4, 5], 2: [7, 8, 9, 10, 15], 4: [20, 25, 40]}
- 操纵
dict1
得到差异:
您可以简单地使用索引 -1 和 0 作为最大值和最小值,因为列表是针对每个日期排序的。
dict2 = {m: dict1[m][-1]-dict1[m-1][-1] if m-1 in dict1 and dict1[m][0]>=dict1[m-1][-1] else dict1[m][-1]-dict1[m][0] for i, m in enumerate(months)}
>>> dict2
{1: 4, 2: 10, 4: 20}
- 使用
dict2
获取当年的总数:
dict3 = {1: sum(dict2.values())}
>>> dict3
{1: 34}
我有一份清单,记录了特定建筑物的用电情况。采样每 30 分钟进行一次。我正在使用字典按 day/month 对样本进行分组,之后我想我可以取每一天的最大值并减去下一天 - 前一天,这样我就可以得到那天的总数。其中总计为千瓦时。
我遇到的一个问题是计算第一个 day/month 的第一个最大值,因为我没有什么可比较的,因为那是第一个最大样本?如果有更好的方法请告诉? 我计划处理的方式是在那段时间找出最小最大值之间的差异,直到我获得足够的数据。
我创建了一个list of list作为例子。第一列将是月份和第二个记录值。
下面显示了按月分组的字典,然后找到最大值。 你可以看到,一旦找到最大值和差异,第 1 个月将不会被计算在内?我应该得到的结果是 {1:4, 2:10, 3:25} 这是按月计算的总数然后 今年 {1:39}?
dict1 = {1: [1, 3, 4, 5], 2: [7, 8, 9, 10, 15], 3: [20, 25, 40]}
dict2 = {1: 5, 2: 15, 3: 40}
这是到目前为止的代码,因为我的方法不起作用,所以我注释掉了获取年和月总数的操作?
list1 = [[1, 1],
[1, 1],
[1, 0],
[1, 3],
[1, 4],
[1, 5], # max
[2, 7],
[2, 8],
[2, 9],
[2, 10],
[2, 15], # max
[3, 20],
[3, 25],
[3, 40]] # max
# using min/max of each day/month I'll miss the usage between the last value for the
# day/month and
# first value of next day/month.
# ex: day 1: 5 - 1 = 4 , day 2: = 9 - 7 = 2, day 3: 40 - 12 = 28
# missed usage from 5 to 7 and 9 to 12
# taking the difference between max values of each day/month
# ex: day 1: = 5, day 2: = 9, day 3 = 40 day 1 = 4, day 2 = 31
#
#
dict1 = {}
dict2 = {}
dict3 = {}
for i, j in list1:
if j > 0: # Samples will always be greater than zero so eliminate any sample that's not.
dict1.setdefault(i, []).append(j) # Group all the samples by day/month.
dict1[i] = sorted(list(set(dict1[i]))) # Check for duplicate samples
max_ = max(dict1[i])
dict2[i] = max_
print(dict1)
print(dict2)
您可以使用字典理解来做您需要的事情:
- 将
list1
读入dict1
:
list1 = [[1, 1], [1, 1], [1, 0], [1, 3], [1, 4], [1, 5],
[2, 7], [2, 8], [2, 9], [2, 10], [2, 15],
[4, 20], [4, 25], [4, 40]]
months = sorted(set(l[0] for l in list1))
dict1 = {m: sorted(set(l[1] for l in list1 if l[0]==m and l[1]>0)) for m in months}
>>> dict1
{1: [1, 3, 4, 5], 2: [7, 8, 9, 10, 15], 4: [20, 25, 40]}
- 操纵
dict1
得到差异:
您可以简单地使用索引 -1 和 0 作为最大值和最小值,因为列表是针对每个日期排序的。
dict2 = {m: dict1[m][-1]-dict1[m-1][-1] if m-1 in dict1 and dict1[m][0]>=dict1[m-1][-1] else dict1[m][-1]-dict1[m][0] for i, m in enumerate(months)}
>>> dict2
{1: 4, 2: 10, 4: 20}
- 使用
dict2
获取当年的总数:
dict3 = {1: sum(dict2.values())}
>>> dict3
{1: 34}