On Feb 10, 11:58 am, green351 <mmamashra...@gmail.com> wrote: > On Feb 10, 10:45 am, John H Palmieri <jhpalmier...@gmail.com> wrote: > > > > > On Feb 10, 10:31 am, green351 <mmamashra...@gmail.com> wrote: > > > > Hi. I have the following problem: consider the following list of > > > compositions and its cartesian product with itself. > > > > C83=Compositions(8, length=3); > > > cp=CartesianProduct(C83,C83) > > > > I want to remove certain elements from this set/list and then to count > > > the elements left. For example I want to remove the element [[1,1,6], > > > [1,2,5]] and in general for [[a,b,c],[d,e,f]], I want to remove it if > > > a=d=1 OR b=e=1 Or c=f=1. There are other things I want to remove but > > > I think if I figure out the correct syntax for the above I could > > > probably figure the rest out. How would I do this? > > > One way is to use "filter": > > > Y = cp.filter(lambda x: x[0][0] != 1 or x[1][0] != 1) > > > keeps pairs x=[[a,b,c], [d,e,f]] so that either a != 1 or d != 1, so > > you could do something like > > > Y = cp.filter(lambda x: (x[0][0] != 1 or x[1][0] != 1) and (x[0][1] != > > 1 or x[1][1] != 1) and > > (x[0][2] != 1 or x[1][2] != 1)) > > Thanks for the quick replies! > John, > So I tried the following using what you have above: > > C22=Compositions(6, length=2); > cp2=CartesianProduct(C22,C22); > A= cp2.filter(lambda x: x[0][0] != 1 and x[1][1] != 1 and x[0][0] != 2 > and x[1][1] != 2 and x[0][0] != 3 and x[1][1] != 3); A.list() > [[[4, 2], [1, 5]], [[4, 2], [2, 4]], [[5, 1], [1, 5]], [[5, 1], [2, > 4]]] > > My goal here is to get rid of ALL [[a,b],[c,d]] with a=c or b=d. But > why does it also get rid of [[3,3],[2,4]]? Maybe I don't understand > the syntax. I'm guessing x[0][0] ! =1 refers to the element x= [[a,b], > [c,d]] with a=c=1. Is this correct?
No: if x = [[a, b], [c,d]], then x[0] is [a,b], the 0th entry of x. Then x[0][0] is the 0th entry of [a,b], namely a. SImilarly, x[1] is [c,d]... If you want to get rid of all [[a,b], [c,d]] with a==c or b==d, then you want to keep all such terms with a != c and b != d. So you could say cp2.filter(lambda x: x[0][0] != x[1][0] and x[0][1] != x[1][1]). Does that clear anything up? --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---