RE: Building List
- To: mathgroup at smc.vnet.net
- Subject: [mg48047] RE: [mg48015] Building List
- From: "DrBob" <drbob at bigfoot.com>
- Date: Sat, 8 May 2004 01:23:57 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Here are two methods. The second may be faster if ell is very long.
ell={{1,2},{3,4,5,6},{7,8}};
rule1={a___List,{b_,c_,d__},e___List}:>{a,{b,c},{b,d},e};
ell//.rule1
{{1,2},{3,4},{3,5},{3,6},{7,8}}
rule2={b_,c_,d__}:>Sequence[{b,c},{b,d}];
f=#//.rule2&;
f/@ell
{{1,2},{3,4},{3,5},{3,6},{7,8}}
DrBob
www.eclecticdreams.net
-----Original Message-----
From: Bruce W. Colletti [mailto:bcolletti at compuserve.com]
To: mathgroup at smc.vnet.net
Subject: [mg48047] [mg48015] Building List
Re Mathematica 5.0.1.0.
I have a list of lists -- e.g., L = { {1,2},{3,4,5,6},{7,8} } -- and
want to replace element-lists (whose length exceeds 2) with another list
built from that element. All else is untouched.
For instance, using L above, replace {3,4,5,6} with {3,4}, {3,5}, {3,6}.
This transforms L to the desired form { {1,2}, {3,4}, {3,5}, {3,6},
{7,8} }.
Although I can do this using Sow and Reap, am hoping there's an easier
way using rules (/.). In general, I want to replace those L-elements x
(that meet a criterion) with foo[x].
Thanks.
Bruce