Ask Your Question

Revision history [back]

You should notice that the elements of L are not lists, but partitions (that do not provide a count() function):

sage: L[0]
[6]
sage: L[0].parent()
Partitions of the integer 6
sage: type(L[0])
<class 'sage.combinat.partition.Partitions_n_with_category.element_class'>

Of course, you can transform them as lists with the list function:

sage: list(L[0])
[6]
sage: type(list(L[0]))
<type 'list'>

Hence, you can do;

sage: [j for j in L if list(j).count(2)>0]
[[4, 2], [3, 2, 1], [2, 2, 2], [2, 2, 1, 1], [2, 1, 1, 1, 1]]

But more elegantly, instead of counting, you can check that 2 belongs to the partition with the in operator:

sage: [j for j in L if 2 in j]
[[4, 2], [3, 2, 1], [2, 2, 2], [2, 2, 1, 1], [2, 1, 1, 1, 1]]

You should notice that the elements of L are not lists, but partitions (that do not provide a count() function):function as you saw with the AttributeError):

sage: L[0]
[6]
sage: L[0].parent()
Partitions of the integer 6
sage: type(L[0])
<class 'sage.combinat.partition.Partitions_n_with_category.element_class'>

Of course, you can transform them as lists with the list function:

sage: list(L[0])
[6]
sage: type(list(L[0]))
<type 'list'>

Hence, you can do;

sage: [j for j in L if list(j).count(2)>0]
[[4, 2], [3, 2, 1], [2, 2, 2], [2, 2, 1, 1], [2, 1, 1, 1, 1]]

But more elegantly, instead of counting, you can check that 2 belongs to the partition with the in operator:

sage: [j for j in L if 2 in j]
[[4, 2], [3, 2, 1], [2, 2, 2], [2, 2, 1, 1], [2, 1, 1, 1, 1]]

You should notice that the elements of L are not lists, but partitions (that do not provide a count() function method as you saw with the AttributeError):

sage: L[0]
[6]
sage: L[0].parent()
Partitions of the integer 6
sage: type(L[0])
<class 'sage.combinat.partition.Partitions_n_with_category.element_class'>

Of course, you can transform them as lists with the list function:

sage: list(L[0])
[6]
sage: type(list(L[0]))
<type 'list'>

Hence, you can do;

sage: [j for j in L if list(j).count(2)>0]
[[4, 2], [3, 2, 1], [2, 2, 2], [2, 2, 1, 1], [2, 1, 1, 1, 1]]

But more elegantly, instead of counting, you can check that 2 belongs to the partition with the in operator:

sage: [j for j in L if 2 in j]
[[4, 2], [3, 2, 1], [2, 2, 2], [2, 2, 1, 1], [2, 1, 1, 1, 1]]