Re: Extracting elements of lists
- To: mathgroup at smc.vnet.net
- Subject: [mg45495] Re: Extracting elements of lists
- From: bobhanlon at aol.com (Bob Hanlon)
- Date: Sat, 10 Jan 2004 00:00:41 -0500 (EST)
- References: <btncql$1s$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Here are a few ways:
data = Table[Random[], {6},{4}];
extracts1 = data[[All,{1,2}]];
extracts2 = {#[[1]],#[[2]]}& /@ data;
extracts3 = Take[#,2]& /@ data;
extracts4 = Drop[#,-2]& /@ data;
extracts1 == extracts2 == extracts3 == extracts4
True
Bob Hanlon
In article <btncql$1s$1 at smc.vnet.net>, sashan <nothing at important.com> wrote:
<< I'm trying to extract a elements from a list and return another list
with those elements. I've tried doing it using a while loop and
recursion and haven't been able to do it.
The list I'm working with is a list of lists where each inner list
contains 4 elements. Example:
{{0, 1, 0, 0}, {0.04, 1, -0.0008, 0}, {0.1, 0.999987, -0.00499995,
0}, {0.15, 0.999937, -0.0112494, 0}, {0.18, 0.999869, -0.0161983,
0},....
This is my recursive attempt:
listoftimex[l_] := {{First[l][[1]], First[l][[2]]}, listoftimex[Rest[l]]}
{{0, 1}, {{0.04,
1}, {{0.1, 0.999987}, {{
0.15, 0.999937}, {{0.18, 0.999869}, {{0.23, 0.99965}, {{0.26,
0.999429}, \
{{0.31, 0.998847}, {{0.35, 0.998128}, {{0.39,
It extracts the 1st 2 elements of the list and puts them in a list but
it nests lists too deep (too many { ) and it errors with 'Recursion
depth of 256 exceeded'.
This is my attempt using a while loop:
timex[l_] := Module[{i =
1, ans = {}}, While[i < Length[l], Append[ans, {l[[i,
1]], l[[i, 2]]}]; i = i + 1]; Return[ans]];
This doesn't work and returns an empty list {}.
>><BR><BR>