-1

How would I be able to find all the combinations between two sets of characters in python, kind of like a cartesian product, but I don't want there to be pairs but subsets of the combinations.

For example:

Let's say we have two sets with three elements

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

How would I get this output:

c = {{"a", "a"}, {"a", "b"}, {"a", "c"}, {"b", "a"}, {"b", "b"}, {"b", "c"}, {"c", "a"}, {"c", "b"}, {"c", "c"}}
Sakib Pathen
  • 155
  • 1
  • 1
  • 7
  • You realize, `{"a", "a"} == {"a"}`, that *sets* have no notion of duplicates, that if you want duplicates, the most natural data-structure is a `tuple`? – juanpa.arrivillaga Sep 25 '17 at 19:33
  • Presumably you just want `list(itertools.product(a, b))`, in which case [this is a nice duplicate](https://stackoverflow.com/questions/2541401/pairwise-crossproduct-in-python). – miradulo Sep 25 '17 at 19:37
  • Possible duplicate of [Pairwise crossproduct in Python](https://stackoverflow.com/questions/2541401/pairwise-crossproduct-in-python) – miradulo Sep 25 '17 at 20:02

1 Answers1

0

You will be unable to get the output you asked for because - for example {'a', 'a'} is not valid value (sets do not allow duplicates and this value would just be 'a').

However you can achieve the result you are aiming for by creating a set of tuples as such:

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')}
Sam Redway
  • 7,014
  • 2
  • 25
  • 36