Sorry, this content is no longer available

Ask Your Question
2

Append Set to Set

asked 2 years ago

Thrash gravatar image

Consider the sets a={1} and b={2}. As output I want {1,{2}}. I tried

sage: a={1};b={2}
sage: a.add(b)

and

sage: a.union({b})

but both attempts result in an error:

TypeError: unhashable type: 'set'
Preview: (hide)

1 Answer

Sort by » oldest newest most voted
2

answered 2 years ago

rburing gravatar image

updated 2 years ago

To circumvent this problem the frozenset (which can't be modified after creation) was invented:

sage: a.add(frozenset(b))
sage: a
{1, frozenset({2})}

Another alternative is to use Sage's Set:

sage: a=Set([1])
sage: b=Set([2])
sage: a.union(Set([b]))
{1, {2}}
Preview: (hide)
link

Comments

1

How can I remove frozenset in the output?

Thrash gravatar imageThrash ( 2 years ago )

You can't really, unless you make a custom type, or call a custom printing function. I offer SageMath's Set as an alternative.

rburing gravatar imagerburing ( 2 years ago )
1

SageMath's Set is the solution for me, thank you! I didn't know about these two different functions (set and Set) and thought there was only one.

Thrash gravatar imageThrash ( 2 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2 years ago

Seen: 260 times

Last updated: Apr 12 '22