How to convert a list to a set in sagemath?

1.3k Views Asked by At

I am trying to convert a list to a set in sagemath 8.2

H = Set(range(1,n+1))

[1, 2, 3, 4]

L=Combinations(H,2).list()

[[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]

Now, I would like to use SetPartitions but L is a list.

How can I convert this list to set?

1

There are 1 best solutions below

0
On

The items of the list L are lists themselves. Being mutable, lists are not hashable, and therefore cannot be used as elements in a set, so we turn them into tuples first.

sage: n = 3
sage: H = Set(range(1, n+1))
sage: H
{1, 2, 3}
sage: L = Combinations(H, 2).list()
sage: L
[[1, 2], [1, 3], [2, 3]]
sage: M = set(tuple(c) for c in L)
sage: M
{(1, 2), (1, 3), (2, 3)}
sage: SetPartitions(M)
Set partitions of {(1, 2), (1, 3), (2, 3)}
sage: P = SetPartitions(M)
sage: P
Set partitions of {(1, 2), (1, 3), (2, 3)}
sage: for p in P:
....:     print p
....:
{{(1, 2), (1, 3), (2, 3)}}
{{(1, 2)}, {(1, 3), (2, 3)}}
{{(1, 2), (2, 3)}, {(1, 3)}}
{{(1, 2), (1, 3)}, {(2, 3)}}
{{(1, 2)}, {(1, 3)}, {(2, 3)}}