2019-07-22 15:07:35 +0200 | received badge | ● Famous Question (source) |
2019-02-10 02:16:27 +0200 | received badge | ● Notable Question (source) |
2017-07-20 20:27:44 +0200 | received badge | ● Popular Question (source) |
2015-06-04 01:30:23 +0200 | commented answer | Testing for list membership Thanks very much, this is exactly what I needed! |
2015-06-04 01:29:25 +0200 | received badge | ● Scholar (source) |
2015-06-04 00:49:37 +0200 | asked a question | 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]
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?? |