使用 set().intersection() 比较 list/dictionary 中任意数量的 X 的多个集合

Comparing multiple sets for any and any number of X in a list/dictionary using set().intersection()

如标题所示,我想使用 set().intersection() 比较 list/dictionary 中任意数量的 X 的多个集合,并输出一个包含所有匹配项的集合。

我目前有:

asset_list = ["BTC", "ETH", "AAVE", "YFI", "COMP"]
timestamp_dict = {} #this has all timestamps for assets above

timestamp_matches = {}
for i in range(1, len(asset_list)):
    timestamp = set(timestamp_dict[asset_list[0]]).intersection(timestamp_dict[asset_list[i]])
    timestamp_matches[asset_list[i]] = timestamp

它 returns 字典 'timestamp_matches' 包含 'asset_list' 中每个资产的匹配列表,我意识到我已经告诉它了。我以前有:

timestamp_matches = set(timestamp_dict["BTC"]).intersection(
set(timestamp_dict["ETH"]),
set(timestamp_dict["AAVE"]),
set(timestamp_dict["YFI"]),
set(timestamp_dict["COMP"]))

但这意味着我必须对每个资产进行硬编码,而我的 objective.

无法做到这一点

谢谢!

如果你想要数据中所有集合的交集,你可以使用一个变量来存储第一个集合,然后用它来计算它与其他集合的交集。你几乎完成了,只需要修改你的代码一点点:

asset_list = ["BTC", "ETH", "AAVE", "YFI", "COMP"]
timestamp_dict = {} #this has all timestamps for assets above

asset1 =  asset_list[0]
timestamp1 = timestamp_dict[asset1]
timestamp_matches = set(timestamp1)
for i in range(1):
    asset_next = asset_list[i]
    timestamp = timestamp_dict[asset_next]
    timestamp = set(timestamp)
    timestamp_matches = timestamp_matches.intersection(timestamp)

顺便说一句,如果你只用一行代码做一件事,你的代码可读性会提高。

这种递归函数应用functools.reduce就可以了。

在这个例子中,我们使用列表中的第一个集合作为基础,然后将它与其他每个集合相交。

>>> sets = [{1, 2}, {1, 2, 3}, {1, 2, 3, 4}]
>>> functools.reduce(set.intersection, sets[1:], sets[0])
{1, 2}

您的代码可能如下所示:

import functools

asset_list = ["BTC", "ETH", "AAVE", "YFI", "COMP"]
timestamp_dict = {} #this has all timestamps for assets above
sets = [set(v) for k, v in timestamp_dict.items() if k in asset_list]

timestamp_matches = functools.reduce(set.intersection, sets[1:], sets[0])

sets = [set(v) for k, v in timestamp_dict.items() if k in asset_list]

是一个 list comprehension,它为 timestamp_dict 中的每个值创建一个 set,如果对应的键在 asset_list.