Obtaining Size of a List and of elements of a List

3,364 views
Skip to first unread message

Vince

unread,
Sep 2, 2011, 6:44:37 AM9/2/11
to sage-support
Dear all,

If I have a list, how do I obtain the cardinality of the list, the
command Cardinality() doesn't seem to always work. For example, the
following code produces a set ComSet of sets of combinations.

Rows=3
RowVector=[2,3,2]
ComSet=[]
for j in range(Columns):
C=Combinations(range(Rows),RowVector[j])
ComSet.append(C.list())
show(ComSet)

However using Cardinality() on ComSet does not seem to work:

ComSet.Cardinality()

Basically I am looking for the mathematica command "Length".
Furthermore how would I be able to map that command on to the elements
of ComSet?

Many thanks,
Vince

Robert Bradshaw

unread,
Sep 2, 2011, 7:01:05 AM9/2/11
to sage-s...@googlegroups.com
On Fri, Sep 2, 2011 at 3:44 AM, Vince <vincent...@gmail.com> wrote:
> Dear all,
>
> If I have a list, how do I obtain the cardinality of the list, the
> command Cardinality() doesn't seem to always work. For example, the
> following code produces a set ComSet of sets of combinations.
>
> Rows=3
> RowVector=[2,3,2]
> ComSet=[]
> for j in range(Columns):
>    C=Combinations(range(Rows),RowVector[j])
>    ComSet.append(C.list())
> show(ComSet)

What's Columns?

> However using Cardinality() on ComSet does not seem to work:
>
> ComSet.Cardinality()
>
> Basically I am looking for the mathematica command "Length".


FYI, most Python and Sage commands start with a lower case. In this
case, however, what you're looking for is len(ComSet). If you have an
object x, type x-dot-tab to see what methods it supports.

> Furthermore how would I be able to map that command on to the elements
> of ComSet?

Use "list comprehensions" (it's a Python thing).

sage: L = [[1, 2, 3], [4, 5]]
sage: [len(a) for a in L]
[3, 2]

Vincent Knight

unread,
Sep 2, 2011, 7:27:44 AM9/2/11
to sage-s...@googlegroups.com
Many thanks for this. Apologies for incomplete code: Columns=3.

Vince
> --
> To post to this group, send email to sage-s...@googlegroups.com
> To unsubscribe from this group, send email to sage-support...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/sage-support
> URL: http://www.sagemath.org

Vincent Knight

unread,
Sep 2, 2011, 8:06:27 AM9/2/11
to sage-s...@googlegroups.com
I'm afraid that's not working. Appartently len has been removed and replaced with cardinality(). Thanks for the tip about ".[tab]". However I still seem to be rather stuck:

If I run this code:

Columns=3
Rows=3
RowVector=[2,3,2]
ComSet=[]
for j in range(Columns):
   C=Combinations(range(Rows),RowVector[j])
   ComSet.append(C.list())
show(ComSet)

ComSet then appears as: [[[0,1],[0,2],[1,2]],[[0,1,2]],[[0,1],[0,2],[1,2]]

which is what I require. If I then use ".[tab]" on ComSet I get various options, none of which include "len" and or "cardinality". The only option that looks relatively right would be "count" but I can't seem to find any help on that command and can't seem to use it correctly.

If I use .[tab] on any element of ComSet, for example on [[0,1],[0,2],[1,2]I seem to get a similar problem (and len does not appear as an option):

Defining: 

C=[[0,1],[0,2],[1,2]]

and then C.[tab] gives me the same options as before (i.e. count which I don't seem to be able to find any help for). If however I define C in a different manner:

C=Combinations(range(3),2)

then C.[tab] does indeed give me cardinality. 

I am obviously missing something simple. Is there a straightforward way to obtain the length of a set?

Any further help would be greatly appreciated,
Vince





--
To post to this group, send email to sage-s...@googlegroups.com
To unsubscribe from this group, send email to sage-support...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-support
URL: http://www.sagemath.org



--
Dr Vincent Knight
Cardiff School of Mathematics
Senghennydd Road,
Cardiff
CF24 4AG
(+44) 29 2087 5548
Skype: drvinceknight

D. S. McNeil

unread,
Sep 2, 2011, 9:49:13 AM9/2/11
to sage-s...@googlegroups.com
In your code, ComSet is a Python list (not a set) as are many of its
components, and you use len(x) to get the size:

sage: ComSet, type(ComSet), len(ComSet)
([[[0, 1], [0, 2], [1, 2]], [[0, 1, 2]], [[0, 1], [0, 2], [1, 2]]],
<type 'list'>, 3)
sage: ComSet[0], type(ComSet[0]), len(ComSet[0])
([[0, 1], [0, 2], [1, 2]], <type 'list'>, 3)
sage: ComSet[0][0], type(ComSet[0][0]), len(ComSet[0][0])
([0, 1], <type 'list'>, 2)
sage: ComSet[0][0][0], type(ComSet[0][0][0])
(0, <type 'int'>)

cardinality is a method not of Python lists, but of the Combinations
object. For example:

sage: C
Combinations of [0, 1, 2] of length 2
sage: C.cardinality()
3
sage: list(C)
[[0, 1], [0, 2], [1, 2]]
sage: C.list()
[[0, 1], [0, 2], [1, 2]]
sage: len(C.list())
3

The reason tab-completion doesn't reveal len is because len is a
function, not a method on the object, and the dot-tab procedure
returns the object's contents. (Admittedly, if you type
ComSet.__[tab], you can see the special methods, including
ComSet.__len__ which is used behind the scenes, but you would never
write ComSet.__len__() in real code.)

Does that help?


Doug

Vincent Knight

unread,
Sep 2, 2011, 3:24:48 PM9/2/11
to sage-s...@googlegroups.com

Thanks,

This really has clarified everything :)

Much appreciated,
Vince

Reply all
Reply to author
Forward
0 new messages