MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: make a set of conditions without the parameters

  • To: mathgroup at smc.vnet.net
  • Subject: [mg57979] Re: make a set of conditions without the parameters
  • From: "Ray Koopman" <koopman at sfu.ca>
  • Date: Wed, 15 Jun 2005 05:58:12 -0400 (EDT)
  • References: <d8jlkt$sum$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Guy Israeli wrote:
> Hi,
>
> I have a big list made out of smaller lists. I want to filter out specific
> smaller lists, for instance those who have 1 in the first place and 3 in the
> 7th place.
>
> I know what item cannot be in what place for the condition described above
> but the only thing i could do is to do those rules using string like this:
>
>
> t2 = {{0, 0, 0, {3}, 0, {4}, 0, 0, 0}, {0, {7}, {3}, 0, 0, 0, {1}, {4}, 0},
> {{4},
> 0, 0, 0, {7}, 0, 0, 0, {6}}, {0, {9}, {5}, {4}, 0, {6}, 0, 0, 0}, {{8}, 0,
> 0, 0, {9}, 0, 0, 0, {3}}, {0, {4}, {6}, {8}, 0, {1}, 0, 0, 0}, {{6}, 0,
> 0, 0, 0, 0, 0, 0, {5}}, {0, {2}, {7}, 0, 0, 0, {3}, {8}, 0}, {0, 0,
> 0, {5}, 0, {7}, 0, 0, 0}};
> rulesperline = Table[MapIndexed[ToString["#[["] <> ToString[#1] <>
> ToString["]]!="] <> ToString[#2] &, (Part[#, i] & /@ Partition[Flatten[t2],
> 9] ) // Flatten], {i, 1, 9}];
>
> newrules = Fold[(#1 /. ToString["#[[0]]!={"] <> ToString[#2] <>
> ToString["}"] -> X) &,
> rulesperline, Range[9]]
>
> which will result in
>
> {{X, X, #[[4]]!={3}, X, #[[8]]!={5}, X, #[[6]]!={7}, X, X}, {X, #[[7]]!={2},
> X, #[[9]]!={4}, X, #[[4]]!={6}, X, #[[2]]!={8}, X}, {X, #[[3]]!={2},
> X, #[[5]]!={4}, X, #[[6]]!={6}, X, #[[7]]!={8}, X}, {#[[3]]!={1}, X,
> X, #[[4]]!={4}, X, #[[8]]!={6}, X, X, #[[5]]!={9}}, {X, X, #[[7]]!={3},
> X, #[[9]]!={5}, X, X, X, X}, {#[[4]]!={1}, X, X, #[[6]]!={4},
> X, #[[1]]!={6}, X, X, #[[7]]!={9}}, {X, #[[1]]!={2}, X, X, X, X,
> X, #[[3]]!={8}, X}, {X, #[[4]]!={2}, X, X, X, X, X, #[[8]]!={8}, X}, {X,
> X, #[[6]]!={3}, X, #[[3]]!={5}, X, #[[5]]!={7}, X, X}}
>
> and then to cancel  the underlines
>
> newrulesfull = Select[#, # != "X" &] & /@ newrules
>
> {{#[[4]]!={3}, #[[8]]!={5}, #[[6]]!={7}}, {#[[7]]!={2}, #[[9]]!={4}, \
> #[[4]]!={6}, #[[2]]!={8}}, {#[[3]]!={2}, #[[5]]!={4}, #[[6]]!={6}, \
> #[[7]]!={8}}, {#[[3]]!={1}, #[[4]]!={4}, #[[8]]!={6}, #[[5]]!={9}}, \
> {#[[7]]!={3}, #[[9]]!={5}}, {#[[4]]!={1}, #[[6]]!={4}, #[[1]]!={6}, \
> #[[7]]!={9}}, {#[[1]]!={2}, #[[3]]!={8}}, {#[[4]]!={2}, #[[8]]!={8}}, \
> {#[[6]]!={3}, #[[3]]!={5}, #[[5]]!={7}}}
>
> The X here is just to mark that it is unimportnat.
>
> however, each element is a string here. and now starts my problem
>
> I would want to do Select[somelist, And[newrulesfull [[1]]]&] meaning it
> will give the elements in somelist that match the all of the conditions in
> rulesperline[[1]]. But since it is a string it doesn't do much, and
> everytime I do ToExpression it gives me a lot of errors because it doesn't
> know what #[[4]] or some other number is.
>
> How can I make a list of rules like that so that I can use Select on those
> conditions?

Short answer: use x[[{positions}]] != {values} as your test.

In[1]:= t2 = {{ 0, 0,  0, {3},0,{4},0,  0, 0 },
              { 0,{7},{3}, 0, 0, 0,{1},{4},0 },
              {{4},0,  0,  0,{7},0, 0,  0,{6}},
              { 0,{9},{5},{4},0,{6},0,  0, 0 },
              {{8},0,  0,  0,{9},0, 0,  0,{3}},
              { 0,{4},{6},{8},0,{1},0,  0, 0 },
              {{6},0,  0,  0, 0, 0, 0,  0,{5}},
              { 0,{2},{7}, 0, 0, 0,{3},{8},0 },
              { 0, 0,  0, {5},0,{7},0,  0, 0 }};

In[2]:= {u2,v2} = Transpose[Transpose@Select[
                  Transpose@{Range@Length@#,#},ListQ[#[[2]]]&]&/@t2]
Out[2]= {{{4,6}, {2,3,7,8}, {1,5,9}, {2,3,4,6}, {1,5,9}, {2,3,4,6},
          {1,9}, {2,3,7,8}, {4,6}},
         {{{3},{4}}, {{7},{3},{1},{4}}, {{4},{7},{6}},
          {{9},{5},{4},{6}}, {{8},{9},{3}}, {{4},{6},{8},{1}},
          {{6},{5}}, {{2},{7},{3},{8}}, {{5},{7}}}}

u2[[i]] gives the important positions in row i of t2.
v2[[i]] gives the corresponding values.
If the important elements in t2 were given as {x}
only to mark them as important and to allow for {0},
and if the values in the to-be-inspected lists
will all be simple scalars, then Flatten the rows of v2:

In[3]:= v2 = Flatten/@v2
Out[3]= {{3,4}, {7,3,1,4}, {4,7,6}, {9,5,4,6}, {8,9,3}, {4,6,8,1},
         {6,5}, {2,7,3,8}, {5,7}}

In[4]:= x = Table[Random[Integer,9],{10^4},{9}];

In[5]:= Table[Length@Select[x,#[[u2[[i]]]] != v2[[i]]&],{i,Length@t2}]
Out[5]= {9896, 10000, 9991, 9998, 9988, 9997, 9912, 9999, 9889}

Here are the 9 lists that were rejected by rule 3:

In[6]:= With[{i = 3}, Select[x, #[[u2[[i]]]] == v2[[i]]&]]
Out[6]= {{4,5,6,8,7,8,4,5,6},
         {4,8,9,8,7,6,2,4,6},
         {4,7,8,6,7,9,5,8,6},
         {4,1,2,7,7,6,0,3,6},
         {4,0,1,3,7,3,3,3,6},
         {4,2,4,5,7,7,4,9,6},
         {4,2,8,0,7,8,3,5,6},
         {4,1,3,6,7,8,4,0,6},
         {4,2,3,6,7,0,9,2,6}}


  • Prev by Date: Re: Write/WriteString -- writing delimited txt to a stream?
  • Next by Date: splitting sublists
  • Previous by thread: Re: make a set of conditions without the parameters
  • Next by thread: spotlight escape sequences