Ask Your Question
0

collection of sets

asked 2010-09-17 08:38:04 +0200

sriram gravatar image

updated 2015-01-14 12:06:29 +0200

FrédéricC gravatar image

How to create a collection of sets and how find its subcollections of specified size

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
2

answered 2010-09-18 13:56:39 +0200

Using Sage code:

sage: X = Set([1, 3, "two"])  # note capital S
sage: X.subsets()
Subsets of {1, 3, 'two'}
sage: X.subsets(size=2)
Subsets of {1, 3, 'two'} of size 2
sage: X.subsets(size=2).list()
[{1, 3}, {1, 'two'}, {3, 'two'}]
edit flag offensive delete link more
0

answered 2010-09-17 08:49:20 +0200

niles gravatar image

Does something like this do what you're looking for?

sage: s = set([1,3,"two"]) # make a set from a list of items
sage: t = list(subsets(s)); t  # this is the list of all subsets of s
[[], [1], [3], [1, 3], ['two'], [1, 'two'], [3, 'two'], [1, 3, 'two']]
sage: for x in subsets(s):  # subsets(s) is an iterator, so you can loop through it
....:     if len(x) == 2:   # and extract items of a specified length
....:         print x
....:         
[1, 3]
[1, 'two']
[3, 'two']

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2010-09-17 08:38:04 +0200

Seen: 254 times

Last updated: Sep 18 '10