|
[Date Index]
[Thread Index]
[Author Index]
RE: Building List
- To: mathgroup at smc.vnet.net
- Subject: [mg48035] RE: [mg48015] Building List
- From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
- Date: Sat, 8 May 2004 01:23:39 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
>-----Original Message-----
>From: Bruce W. Colletti [mailto:bcolletti at compuserve.com]
To: mathgroup at smc.vnet.net
>Sent: Friday, May 07, 2004 10:30 AM
>To: mathgroup at smc.vnet.net
>Subject: [mg48035] [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
>
>
If all your sublists are of even length, it's easy:
In[1]:= l = {{1, 2}, {3, 4, 5, 6}, {7, 8}};
In[2]:= Partition[Flatten[l, 1], 2]
Out[2]= {{1, 2}, {3, 4}, {5, 6}, {7, 8}}
...else, if you then drop the last elements:
In[3]:= l2 = {{1, 2}, {3, 4, 5, 6, 6.5}, {7, 8}};
In[4]:= Flatten[#, 1] &[Partition[#, 2] & /@ l2]
Out[4]= {{1, 2}, {3, 4}, {5, 6}, {7, 8}}
...or if you want to retain it (as singleton):
In[5]:= Flatten[#, 1] &[Partition[#, 2, 2, {1, 1}, {}] & /@ l2]
Out[5]= {{1, 2}, {3, 4}, {5, 6}, {6.5}, {7, 8}}
...or retain the first element as singleton:
In[21]:=
Flatten[#, 1] &[
Partition[#, 2, 2, {1 + Mod[Length[#], 2], 1}, {}] & /@ l2]
Out[21]= {{1, 2}, {3}, {4, 5}, {6, 6.5}, {7, 8}}
...??
--
Hartmut Wolf
Prev by Date:
Re: kuen surface
Next by Date:
Re: FindRoot cannot find obvious solution
Previous by thread:
RE: Building List
Next by thread:
RE: Building List
|