从元组列表/groupby/lambda 处理数据时出现问题
Problem with processing data from a list of tuples / groupby / lambda
我有一个元组列表,如下所示:
tuplelist = [
(datetime.date(2020, 4, 20), 4.23, 'EUR'),
(datetime.date(2020, 4, 20), 3.76, 'USD'),
(datetime.date(2020, 4, 20), 4.21, 'EUR'),
(datetime.date(2020, 4, 20), 5.20, 'GPB'),
(datetime.date(2020, 4, 20), 3.77, 'USD'),
(datetime.date(2020, 4, 20), 4.27, 'EUR'),
(datetime.date(2020, 4, 20), 3.79, 'USD'),
(datetime.date(2020, 4, 20), 4.30, 'EUR'),
(datetime.date(2020, 4, 20), 5.14, 'GPB'),
(datetime.date(2020, 4, 20), 3.77, 'USD'),
(datetime.date(2020, 4, 25), 4.23, 'EUR'),
(datetime.date(2020, 4, 25), 3.76, 'USD'),
(datetime.date(2020, 4, 25), 4.21, 'EUR'),
(datetime.date(2020, 4, 25), 5.20, 'GPB'),
(datetime.date(2020, 4, 25), 3.77, 'USD'),
(datetime.date(2020, 4, 27), 4.27, 'EUR'),
(datetime.date(2020, 4, 27), 3.79, 'USD'),
(datetime.date(2020, 4, 27), 4.30, 'EUR'),
(datetime.date(2020, 4, 27), 5.14, 'GPB'),
(datetime.date(2020, 4, 28), 3.77, 'USD'),
(datetime.date(2020, 4, 28), 4.23, 'EUR'),
(datetime.date(2020, 5, 2), 3.76, 'USD'),
(datetime.date(2020, 5, 2), 4.21, 'EUR'),
(datetime.date(2020, 5, 2), 5.20, 'GPB'),
(datetime.date(2020, 5, 2), 3.77, 'USD'),
(datetime.date(2020, 5, 2), 4.27, 'EUR'),
(datetime.date(2020, 5, 5), 3.79, 'USD'),
(datetime.date(2020, 5, 5), 4.30, 'EUR'),
(datetime.date(2020, 5, 5), 5.14, 'GPB'),
(datetime.date(2020, 5, 5), 3.77, 'USD')
]
我想按日期和货币符号对其进行分组。它应该看起来像这样(每天):
(datetime.date(2020, 4, 20), [{'EUR': [4.23, 4.21, 4.27, 4.3]}, {'USD': [3.76, 3.77, 3.79, 3.77]}, {'GPB': [5.2, 5.14]}])
我使用这行代码设法按数据对其进行了分组:
tuplelist2dict = {key: [*map(lambda v: {v[2]:v[1]}, values)] for key, values in groupby(tuplelist, lambda x: x[0])}
我得到了这个输出:
(datetime.date(2020, 4, 20), [{'EUR': 4.23}, {'USD': 3.76}, {'EUR': 4.21}, {'GPB': 5.2}, {'USD': 3.77}, {'EUR': 4.27}, {'USD': 3.79}, {'EUR': 4.3}, {'GPB': 5.14}, {'USD': 3.77}])
(datetime.date(2020, 4, 25), [{'EUR': 4.23}, {'USD': 3.76}, {'EUR': 4.21}, {'GPB': 5.2}, {'USD': 3.77}])
(datetime.date(2020, 4, 27), [{'EUR': 4.27}, {'USD': 3.79}, {'EUR': 4.3}, {'GPB': 5.14}])
(datetime.date(2020, 4, 28), [{'USD': 3.77}, {'EUR': 4.23}])
(datetime.date(2020, 5, 2), [{'USD': 3.76}, {'EUR': 4.21}, {'GPB': 5.2}, {'USD': 3.77}, {'EUR': 4.27}])
(datetime.date(2020, 5, 5), [{'USD': 3.79}, {'EUR': 4.3}, {'GPB': 5.14}, {'USD': 3.77}])
然而,我正在努力合并不同货币的值以获得我显示的数据格式。
如有任何提示,我将不胜感激。
我不会尝试在理解中执行所有那些复杂的操作。相反,我会使用两个 for
循环来更新嵌套数据结构,为了简洁起见,使用 .setdefault()
:
result = {}
for date, currency_entries in groupby(tuplelist, lambda x: x[0]):
for _, currency_val, currency_name in currency_entries:
result.setdefault(date, {}).setdefault(currency_name, []).append(currency_val)
这输出:
{datetime.date(2020, 4, 20): {'EUR': [4.23, 4.21, 4.27, 4.3], 'USD': [3.76, 3.77, 3.79, 3.77], 'GPB': [5.2, 5.14]}, datetime.date(2020, 4, 25): {'EUR': [4.23, 4.21], 'USD': [3.76, 3.77], 'GPB': [5.2]}, datetime.date(2020, 4, 27): {'EUR': [4.27, 4.3], 'USD': [3.79], 'GPB': [5.14]}, datetime.date(2020, 4, 28): {'USD': [3.77], 'EUR': [4.23]}, datetime.date(2020, 5, 2): {'USD': [3.76, 3.77], 'EUR': [4.21, 4.27], 'GPB': [5.2]}, datetime.date(2020, 5, 5): {'USD': [3.79, 3.77], 'EUR': [4.3], 'GPB': [5.14]}}
我有一个元组列表,如下所示:
tuplelist = [
(datetime.date(2020, 4, 20), 4.23, 'EUR'),
(datetime.date(2020, 4, 20), 3.76, 'USD'),
(datetime.date(2020, 4, 20), 4.21, 'EUR'),
(datetime.date(2020, 4, 20), 5.20, 'GPB'),
(datetime.date(2020, 4, 20), 3.77, 'USD'),
(datetime.date(2020, 4, 20), 4.27, 'EUR'),
(datetime.date(2020, 4, 20), 3.79, 'USD'),
(datetime.date(2020, 4, 20), 4.30, 'EUR'),
(datetime.date(2020, 4, 20), 5.14, 'GPB'),
(datetime.date(2020, 4, 20), 3.77, 'USD'),
(datetime.date(2020, 4, 25), 4.23, 'EUR'),
(datetime.date(2020, 4, 25), 3.76, 'USD'),
(datetime.date(2020, 4, 25), 4.21, 'EUR'),
(datetime.date(2020, 4, 25), 5.20, 'GPB'),
(datetime.date(2020, 4, 25), 3.77, 'USD'),
(datetime.date(2020, 4, 27), 4.27, 'EUR'),
(datetime.date(2020, 4, 27), 3.79, 'USD'),
(datetime.date(2020, 4, 27), 4.30, 'EUR'),
(datetime.date(2020, 4, 27), 5.14, 'GPB'),
(datetime.date(2020, 4, 28), 3.77, 'USD'),
(datetime.date(2020, 4, 28), 4.23, 'EUR'),
(datetime.date(2020, 5, 2), 3.76, 'USD'),
(datetime.date(2020, 5, 2), 4.21, 'EUR'),
(datetime.date(2020, 5, 2), 5.20, 'GPB'),
(datetime.date(2020, 5, 2), 3.77, 'USD'),
(datetime.date(2020, 5, 2), 4.27, 'EUR'),
(datetime.date(2020, 5, 5), 3.79, 'USD'),
(datetime.date(2020, 5, 5), 4.30, 'EUR'),
(datetime.date(2020, 5, 5), 5.14, 'GPB'),
(datetime.date(2020, 5, 5), 3.77, 'USD')
]
我想按日期和货币符号对其进行分组。它应该看起来像这样(每天):
(datetime.date(2020, 4, 20), [{'EUR': [4.23, 4.21, 4.27, 4.3]}, {'USD': [3.76, 3.77, 3.79, 3.77]}, {'GPB': [5.2, 5.14]}])
我使用这行代码设法按数据对其进行了分组:
tuplelist2dict = {key: [*map(lambda v: {v[2]:v[1]}, values)] for key, values in groupby(tuplelist, lambda x: x[0])}
我得到了这个输出:
(datetime.date(2020, 4, 20), [{'EUR': 4.23}, {'USD': 3.76}, {'EUR': 4.21}, {'GPB': 5.2}, {'USD': 3.77}, {'EUR': 4.27}, {'USD': 3.79}, {'EUR': 4.3}, {'GPB': 5.14}, {'USD': 3.77}])
(datetime.date(2020, 4, 25), [{'EUR': 4.23}, {'USD': 3.76}, {'EUR': 4.21}, {'GPB': 5.2}, {'USD': 3.77}])
(datetime.date(2020, 4, 27), [{'EUR': 4.27}, {'USD': 3.79}, {'EUR': 4.3}, {'GPB': 5.14}])
(datetime.date(2020, 4, 28), [{'USD': 3.77}, {'EUR': 4.23}])
(datetime.date(2020, 5, 2), [{'USD': 3.76}, {'EUR': 4.21}, {'GPB': 5.2}, {'USD': 3.77}, {'EUR': 4.27}])
(datetime.date(2020, 5, 5), [{'USD': 3.79}, {'EUR': 4.3}, {'GPB': 5.14}, {'USD': 3.77}])
然而,我正在努力合并不同货币的值以获得我显示的数据格式。
如有任何提示,我将不胜感激。
我不会尝试在理解中执行所有那些复杂的操作。相反,我会使用两个 for
循环来更新嵌套数据结构,为了简洁起见,使用 .setdefault()
:
result = {}
for date, currency_entries in groupby(tuplelist, lambda x: x[0]):
for _, currency_val, currency_name in currency_entries:
result.setdefault(date, {}).setdefault(currency_name, []).append(currency_val)
这输出:
{datetime.date(2020, 4, 20): {'EUR': [4.23, 4.21, 4.27, 4.3], 'USD': [3.76, 3.77, 3.79, 3.77], 'GPB': [5.2, 5.14]}, datetime.date(2020, 4, 25): {'EUR': [4.23, 4.21], 'USD': [3.76, 3.77], 'GPB': [5.2]}, datetime.date(2020, 4, 27): {'EUR': [4.27, 4.3], 'USD': [3.79], 'GPB': [5.14]}, datetime.date(2020, 4, 28): {'USD': [3.77], 'EUR': [4.23]}, datetime.date(2020, 5, 2): {'USD': [3.76, 3.77], 'EUR': [4.21, 4.27], 'GPB': [5.2]}, datetime.date(2020, 5, 5): {'USD': [3.79, 3.77], 'EUR': [4.3], 'GPB': [5.14]}}