python中的笛卡尔积,但不是成对的集合

Cartesian product in python, but instead of pairs, sets

我怎样才能在python中找到两组字符之间的所有组合,有点像笛卡尔积,但我不希望有对,而是组合的子集。

例如:

假设我们有两组三个元素

1
2
a = {"a","b","c"}
b = {"a","b","c"}

我如何得到这个输出:

1
c = {{"a","a"}, {"a","b"}, {"a","c"}, {"b","a"}, {"b","b"}, {"b","c"}, {"c","a"}, {"c","b"}, {"c","c"}}


您将无法获取所需的输出,因为-例如'a'、'a'不是有效值(集合不允许重复,而该值将仅为'a')。

但是,您可以通过创建一组元组来实现目标结果,例如:

1
2
3
4
set([(x, y) for x in a for y in b])

output: {('b', 'a'), ('a', 'a'), ('b', 'b'), ('a', 'b'), ('c', 'b'),
         ('c', 'c'), ('b', 'c'), ('a', 'c'), ('c', 'a')}