gpt4 book ai didi

python - 获取列表列表的 powerset

转载 作者:行者123 更新时间:2023-12-04 11:58:21 25 4
gpt4 key购买 nike

我得到了一个列表列表 s :

s = [["a1", "A"], ["b4", "B"], ["a3", "A"], ["d6", "D"], ["c4", "C"]]
(请注意,列表中的元素不一定以相同的字母开头。为了方便起见,我在这里修改了数据。)
我的目标是将每个列表按其第二个元素排序到一个类别,并通过在每个类别中最多选择一个元素来获得所有可能的组合。
我首先将列表列表散列到字典中:
dic = {i[1]: [] for i in s}
for i in s:
# set the value of the first item key to the second item
dic[i[1]].append(i[0])

dic
>>> {'A': ['a1', 'a3'], 'B': ['b4'], 'C': ['c4'], 'D': ['d6']}
所有可能组合的数量,因此幂集的长度为 s , 应该返回 23 :
{'a1'},
{'a3'},
{'b4'},
{'c4'},
{'d6'},
{'a1', 'b4'},
{'a1', 'c4'},
{'a1', 'd6'},
{'a3', 'b4'},
{'a3', 'c4'},
{'a3', 'd6'},
{'b4', 'c4'},
{'b4', 'd6'},
{'c4', 'd6'},
{'a1', 'b4', 'c4'},
{'a1', 'b4', 'd6'},
{'a1', 'c4', 'd6'},
{'a3', 'b4', 'c4'},
{'a3', 'b4', 'd6'},
{'a3', 'c4', 'd6'},
{'b4', 'c4', 'd6'},
{'a1', 'b4', 'c4', 'd6'},
{'a3', 'b4', 'c4', 'd6'}
我最初打算放多个 for循环,但因为我不能保证有多少 key我会在我的 s (这也会使我的时间复杂度为 O(N^x)),我使用了 itertools.chainitertools.combinations按照 this post :
def powerset(s:list):
return chain.from_iterable(combinations(s, r) for r in range(1, len(s)+1))
问题是这仅考虑单个列表中的元素,因此忽略了约束:“最多不能从每个列表中获取一个以上的元素”。展平列表会忽略类别,所以我没有尝试这样做。
任何解决这个问题的见解将不胜感激。

最佳答案

@don'ttalkjustcode 的答案有效,但不必要地产生了添加虚拟值的开销,并且还会产生一个空集,这不是问题所必需的。
更直接的方法是使用 itertools.combinations从列表的字典中选择列表传递给 itertools.product产生所需的组合:

from itertools import product, combinations

print(*(
set(p)
for r in range(len(dic))
for c in combinations(dic.values(), r + 1)
for p in product(*c)
), sep='\n')
这输出:
{'a1'}
{'a3'}
{'b4'}
{'c4'}
{'d6'}
{'a1', 'b4'}
{'a3', 'b4'}
{'a1', 'c4'}
{'a3', 'c4'}
{'d6', 'a1'}
{'d6', 'a3'}
{'c4', 'b4'}
{'d6', 'b4'}
{'d6', 'c4'}
{'a1', 'c4', 'b4'}
{'a3', 'c4', 'b4'}
{'d6', 'a1', 'b4'}
{'d6', 'a3', 'b4'}
{'d6', 'a1', 'c4'}
{'d6', 'a3', 'c4'}
{'d6', 'c4', 'b4'}
{'d6', 'a1', 'c4', 'b4'}
{'d6', 'a3', 'c4', 'b4'}

关于python - 获取列表列表的 powerset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69038533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com