MathGroup Archive 2004

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

Search the Archive

RE: labeling problem

  • To: mathgroup at smc.vnet.net
  • Subject: [mg46447] RE: [mg46430] labeling problem
  • From: "David Park" <djmp at earthlink.net>
  • Date: Thu, 19 Feb 2004 03:01:54 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

Janos,

lst = {{a, b}, {c, d}, e, f, {g, h, i}, j, k, {l, m}, {n, o, p},
      q, {r, s}, {{t}}};

First I am going to assume that you are going to search only for patterns at
the first level of the list. Otherwise you will need a more complicated
naming scheme that will not generate duplicated.

Also, I am going to return replacement rules, instead of setting named
variables. Otherwise you won't know what the named variables actually are.

extractListPatterns[list_, pattern_, char_:"p"] :=
  Module[
    {positions, names},
    positions = Position[list, pattern, {1}, Heads -> False] // Flatten;
    names =
      Table[Symbol[char <> ToString[Part[positions, i]]], {i, 1,
          Length[positions]}];
    Thread[names -> Part[list, positions]]
    ]

extractListPatterns[lst, {_, _, _}]
{p5 -> {g, h, i}, p9 -> {n, o, p}}

extractListPatterns[lst, {{_}}]
{p12 -> {{t}}}

extractListPatterns[lst, {_, _}]
{p1 -> {a, b}, p2 -> {c, d}, p8 -> {l, m}, p11 -> {r, s}}

extractListPatterns[lst, x_Symbol]
{p3 -> e, p4 -> f, p6 -> j, p7 -> k, p10 -> q}

extractListPatterns[lst, {_, _, _, _}]
{}


For a more general scheme that will extract patterns at any level, or at
specified levels...

extractListPatterns2[list_, pattern_, var_:p, levelspec_:Infinity] :=
  Module[
    {positions, names, parts},
    positions = Position[list, pattern, levelspec, Heads -> False];
    names = Table[p @@ Part[positions, i], {i, 1, Length[positions]}];
    parts = Extract[list, positions];
    Thread[names -> parts]
    ]

extractListPatterns2[lst, {_, _, _}]
{p[5] -> {g, h, i}, p[9] -> {n, o, p}}

extractListPatterns2[lst, {{_}}]
{p[12] -> {{t}}}

extractListPatterns2[lst, {_Symbol}]
{p[12, 1] -> {t}}

extractListPatterns2[lst, {_, _}]
{p[1] -> {a, b}, p[2] -> {c, d}, p[8] -> {l, m}, p[11] -> {r, s}}

extractListPatterns2[lst, _Symbol, p, 1]
{p[3] -> e, p[4] -> f, p[6] -> j, p[7] -> k, p[10] -> q}

extractListPatterns2[lst, _Symbol, p, {2}]
{p[1, 1] -> a, p[1, 2] -> b, p[2, 1] -> c, p[2, 2] -> d, p[5, 1] -> g,
  p[5, 2] -> h, p[5, 3] -> i, p[8, 1] -> l, p[8, 2] -> m, p[9, 1] -> n,
  p[9, 2] -> o, p[9, 3] -> p, p[11, 1] -> r, p[11, 2] -> s}


extractListPatterns2[lst, {_, _, _, _}]
{}

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/


From: Janos [mailto:janos.lobb at yale.edu]
To: mathgroup at smc.vnet.net

Before I re-invent the wheel let me ask:

I need to assign  names to list elements in a nested list based upon
their position in the nested list.  For example if I have a list:

lst = {{a,b},{c,d},e,f,{g,h,i},j,k,{l.m},{n,o,p},q,{r,s},{{t}}}

then
pos=Position[lst,{_,_,_}] will give me

{{5}.{9}}

Now, I am thinking to assign a unique name to the list elements on
these positions, like p5={g,h,i} and p9={n,o,p}.  I did not find yet
how to create variable names with concatenation auto-magically from
program.  In other programming languages I can do something like this:

var="p"+NumToString[5]
&var={a,b,c}

and I could refer to it afterward as p5 and its value would be {a,b,c}.
  The most handy would be if I could take just the name of the position
and assign that to it, like:
For[i=1, i<=Length[pos], i++, NumberToName[pos[[i]]]=lst[[pos[[i]] ]] ]

and I would get:

five={g,h,i} and nine={n,o,p}.

Of course lst[[pos[[i]] ]] where' i' goes from 1 to 2 is somewhat doing
it but it is a little bit complicated.

I looked the book and the online documentation but could not find a
function called NumberToName which would do

NumberToName[5]
five

and vice versa

NameToNumber[five]
5

Is there such function or procedure in someone's drawer ?
If not, then what is the right way to get a unique variable name with
some clear deterministic nature - like above - from Mathematica ?

Thanks ahead,
János

------------------------------------------
"There was a mighty king in the land of the Huns whose goodness and
wisdom had no equal."
Nibelungen-Lied



  • Prev by Date: Re: how to explain this weird effect? Integrate
  • Next by Date: RE: labeling problem
  • Previous by thread: Re: labeling problem
  • Next by thread: RE: labeling problem