Testing for list membership
I'd like to adopt Sage in my teaching this fall, so I've started to play around with some basic manipulations that I would expect my students to be able to do. I am an absolute beginner in Sage and Python.
Suppose that I want all the partitions of 6 that contain the number "2". My first attempt failed:
L=Partitions(6).list(); L
[[6], [5, 1], [4, 2], [4, 1, 1], [3, 3], [3, 2, 1], [3, 1, 1, 1], [2, 2, 2], [2, 2, 1, 1], [2, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1]]
[j for j in L if j.count(2)>0]
"Error in lines 1-1\nTraceback (most recent call last):\n
File \"/projects/6313dd6a-b1df-46ed-885a-a1bdf71f3fc3/.sagemathcloud/sage_server.py\", line 879, in execute\n exec compile(block+'\n', '', 'single') in namespace, locals\n File \"\", line 1, in <module>\n File \"sage/structure/element.pyx\", line 431, in sage.structure.element.Element.__getattr__ (build/cythonized/sage/structure/element.c:4644)\n
return getattr_from_other_class(self, P._abstract_element_class, name)\n File \"sage/structure/misc.pyx\", line 253, in sage.structure.misc.getattr_from_other_class (build/cythonized/sage/structure/misc.c:1582)\n
raise dummy_attribute_error\nAttributeError: 'Partitions_n_with_category.element_class' object has no attribute 'count'\n"
However, if I create a new list E by copying and pasting the above, it works fine.
E=[[6], [5, 1], [4, 2], [4, 1, 1], [3, 3], [3, 2, 1], [3, 1, 1, 1], [2, 2, 2], [2, 2, 1, 1], [2, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1]]
[j for j in E if j.count(2)>0]
[[4, 2], [3, 2, 1], [2, 2, 2], [2, 2, 1, 1], [2, 1, 1, 1, 1]]
Is there a better way to do this, without copying and pasting??