ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 04 Jun 2015 00:49:37 +0200Testing for list membershiphttps://ask.sagemath.org/question/27014/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??schmuThu, 04 Jun 2015 00:49:37 +0200https://ask.sagemath.org/question/27014/Partition list satisfying conditionshttps://ask.sagemath.org/question/9944/partition-list-satisfying-conditions/Hi, I am looking for a function which will partition a list. For a simple example, suppose I have the list `[1,2,3,4,5,6]`. Suppose I want all the even integers in one list and the odd integers in another. Is there a function that can do this, and perhaps yeild something like `[[1,3,5],[2,4,6]]`? In this case, the number of partitions is 2, so it is fairly easy, but in my problem the number is not known. In mathematica, the function I want is called GatherBy.sjcavazos2Sun, 24 Mar 2013 18:35:39 +0100https://ask.sagemath.org/question/9944/