如何从共享相同键的元组列表中找到平均值?
How to find the average value from a list of tuples that share the same key?
我有两个根据用户输入创建的列表,我使用以下代码将它们组合成一个元组列表:
daily_hours_list = [4, 2, 1, None, 3, 5]
week_counter_list = [1, 1, 1, 2, 2, 2]
weekly_hours_list = []
for week, time in zip(week_counter_list, daily_hours_list):
if time != None:
weekly_hours_list.append((week, t))
这给了我:
weekly_hours_list = [(1, 4),
(1, 2),
(1, 1),
(2, 3),
(2, 5)]
然后我使用此代码对第 1 周的所有小时数和第 2 周的所有小时数求和:
tup_h = {i:0 for i, v in weekly_hours_list}
for key, value in weekly_hours_list:
tup_h[key] = tup_h[key]+value
weekly_sum_hours = list(map(tuple, tup_h.items()))
给我:
weekly_sum_hours = [(1, 6),
(2, 8)]
一切正常,但我如何找到每周的平均小时数,例如:
weekly_average_list = [(1, 2),
(2, 4)]
我想我需要扩展 for 循环计算,计算周值为 1 和 2 的元组的数量,但不确定如何实现。提前感谢您的帮助。
我认为首先收集每周的小时数会有所帮助。这可以使用字典轻松完成,其中键是周数,值是该周的小时列表。在内置的 collections
模块中有一个名为 defaultdict
的数据结构,它正是为这样的情况设计的:
from collections import defaultdict
from statistics import mean
daily_hours_list = [4, 2, 1, None, 3, 5]
week_counter_list = [1, 1, 1, 2, 2, 2]
daily_hours_by_week = defaultdict(list)
for week, time in zip(week_counter_list, daily_hours_list):
if time is not None:
daily_hours_by_week[week].append(time)
sum_hours_by_week = {w: sum(hours) for w, hours in daily_hours_by_week.items()}
avg_hours_by_week = {w: mean(hours) for w, hours in daily_hours_by_week.items()}
在我们的示例中,这意味着您不必为每个周数用空列表初始化字典(这就是您对 tup_h
的 0
初始总和所做的操作).相反,如果我们尝试将一个小时追加到字典中还没有的一周,它会创建一个空列表放在那个键下,然后追加到那个。
一旦我们像这样安排好每周的工作时间,就很容易对其进行其他处理。
我们实际上可以一次完成最后两行,并创建一个包含每周统计元组的字典:
statistics_by_week = {w: sum(hours), mean(hours) for w, hours in daily_hours_by_week.items()}
在此处阅读有关 defaultdict
的更多详细信息:https://docs.python.org/3/library/collections.html#collections.defaultdict
我有两个根据用户输入创建的列表,我使用以下代码将它们组合成一个元组列表:
daily_hours_list = [4, 2, 1, None, 3, 5]
week_counter_list = [1, 1, 1, 2, 2, 2]
weekly_hours_list = []
for week, time in zip(week_counter_list, daily_hours_list):
if time != None:
weekly_hours_list.append((week, t))
这给了我:
weekly_hours_list = [(1, 4),
(1, 2),
(1, 1),
(2, 3),
(2, 5)]
然后我使用此代码对第 1 周的所有小时数和第 2 周的所有小时数求和:
tup_h = {i:0 for i, v in weekly_hours_list}
for key, value in weekly_hours_list:
tup_h[key] = tup_h[key]+value
weekly_sum_hours = list(map(tuple, tup_h.items()))
给我:
weekly_sum_hours = [(1, 6),
(2, 8)]
一切正常,但我如何找到每周的平均小时数,例如:
weekly_average_list = [(1, 2),
(2, 4)]
我想我需要扩展 for 循环计算,计算周值为 1 和 2 的元组的数量,但不确定如何实现。提前感谢您的帮助。
我认为首先收集每周的小时数会有所帮助。这可以使用字典轻松完成,其中键是周数,值是该周的小时列表。在内置的 collections
模块中有一个名为 defaultdict
的数据结构,它正是为这样的情况设计的:
from collections import defaultdict
from statistics import mean
daily_hours_list = [4, 2, 1, None, 3, 5]
week_counter_list = [1, 1, 1, 2, 2, 2]
daily_hours_by_week = defaultdict(list)
for week, time in zip(week_counter_list, daily_hours_list):
if time is not None:
daily_hours_by_week[week].append(time)
sum_hours_by_week = {w: sum(hours) for w, hours in daily_hours_by_week.items()}
avg_hours_by_week = {w: mean(hours) for w, hours in daily_hours_by_week.items()}
在我们的示例中,这意味着您不必为每个周数用空列表初始化字典(这就是您对 tup_h
的 0
初始总和所做的操作).相反,如果我们尝试将一个小时追加到字典中还没有的一周,它会创建一个空列表放在那个键下,然后追加到那个。
一旦我们像这样安排好每周的工作时间,就很容易对其进行其他处理。
我们实际上可以一次完成最后两行,并创建一个包含每周统计元组的字典:
statistics_by_week = {w: sum(hours), mean(hours) for w, hours in daily_hours_by_week.items()}
在此处阅读有关 defaultdict
的更多详细信息:https://docs.python.org/3/library/collections.html#collections.defaultdict