Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #6666 > unrolled thread

Re: How to get sub-list elements at certain position in a long list

Started byDrMajorBob <btreat1@austin.rr.com>
First post2011-11-23 12:18 +0000
Last post2011-11-23 12:18 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Re: How to get sub-list elements at certain position in a long list DrMajorBob <btreat1@austin.rr.com> - 2011-11-23 12:18 +0000

#6666 — Re: How to get sub-list elements at certain position in a long list

FromDrMajorBob <btreat1@austin.rr.com>
Date2011-11-23 12:18 +0000
SubjectRe: How to get sub-list elements at certain position in a long list
Message-ID<jaioa8$eqi$1@smc.vnet.net>
That's obviously NOT your list, since the * and ** instances make it  
illegal syntax.

When I edit out those stars (but why, oh why do I bother?!?), I get

list = {{{{2.5}, {3.8, 31}, {54.23, 46.263, 45.987}, {-82.1476,
      5.17782, -205.36}}, {{2.5}, {4.2, 30}, {233.835, 46.469,
      46.819}, {93.8625, -13.9818, -198.778}}}, {{{2.5}, {4.1,
      30}, {139.845, 15.719, 155.87}, {93.6633,
      4.46398, -205.432}}, {{2.5}, {2.6, 32}, {113.381, 40.988,
      85.09}, {90.1206, 16.9792, -205.314}}}, {{{2.5}, {3.1,
      30}, {113.858, 34.709, 87.567}, {55.3298,
      17.7214, -211.648}}, {{2.5}, {2.7, 30}, {110.54, 31.595,
      85.601}, {-25.8558, 175.588, -121.105}}}}

Now, using

list//TreeForm

(* display suppressed *)

or

Depth@list

5

I see that the "last level" is the fourth. But that level consists of  
numbers, not lists, so you can't want the fourth element of items at that  
level:

Replace[list, x_ :> Length@x, {4}]

{{{{0}, {0, 0}, {0, 0, 0}, {0, 0, 0}}, {{0}, {0, 0}, {0, 0, 0}, {0, 0,
      0}}}, {{{0}, {0, 0}, {0, 0, 0}, {0, 0, 0}}, {{0}, {0, 0}, {0, 0,
     0}, {0, 0, 0}}}, {{{0}, {0, 0}, {0, 0, 0}, {0, 0, 0}}, {{0}, {0,
     0}, {0, 0, 0}, {0, 0, 0}}}}

The third level consists of lists of 1, 2, or 3 elements, so you also  
cannot want the fourth element of THOSE items:

Replace[list, x_ :> Length@x, {3}]

{{{1, 2, 3, 3}, {1, 2, 3, 3}}, {{1, 2, 3, 3}, {1, 2, 3, 3}}, {{1, 2,
    3, 3}, {1, 2, 3, 3}}}

At level 2, we do have lists with four elements:

Replace[list, x_ :> Length@x, {2}]

{{4, 4}, {4, 4}, {4, 4}}

Hence we can easily retrieve them:

Replace[list, x_ :> x[[4]], {2}]

{{{-82.1476,
    5.17782, -205.36}, {93.8625, -13.9818, -198.778}}, {{93.6633,
    4.46398, -205.432}, {90.1206, 16.9792, -205.314}}, {{55.3298,
    17.7214, -211.648}, {-25.8558, 175.588, -121.105}}}

or (faster if the list happened to be huge):

list[[All, All, 4]]

{{{{-82.1476,
     5.17782, -205.36}}, {{93.8625, -13.9818, -198.778}}}, {{{93.6633,
     4.46398, -205.432}}, {{90.1206, 16.9792, -205.314}}}, {{{55.3298,
     17.7214, -211.648}}, {{-25.8558, 175.588, -121.105}}}}

Flatten achieves your preferred format from either of the last two results:

Flatten[list[[All, All, 4]], 1]

{{-82.1476,
   5.17782, -205.36}, {93.8625, -13.9818, -198.778}, {93.6633,
   4.46398, -205.432}, {90.1206, 16.9792, -205.314}, {55.3298,
   17.7214, -211.648}, {-25.8558, 175.588, -121.105}}

Bobby

On Tue, 22 Nov 2011 04:36:37 -0600, Gy Peng <hitphyopt@gmail.com> wrote:

> Dear Experts,
>
> I have a list:
>
> {{{{2.5}, {3.8, 31}, {54.23, 46.263, 45.987}, *{-82.1476,** 5.17782,
> -205.36}*}, {{2.5}, {4.2, 30}, {233.835, 46.469,
>     46.819}, *{93.8625, -13.9818, -198.778}*}}, {{{2.5}, {4.1,30},
> {139.845, 15.719, 155.87}, *{93.6633,**4.46398, -205.432}*}, {{2.5},  
> {2.6,
> 32}, {113.381, 40.988,85.09}, *{90.1206, 16.9792, -205.314}*}}, {{{2.5},
> {3.1,30}, {113.858, 34.709, 87.567}, *{55.3298,**17.7214, -211.648}*},
> {{2.5}, {2.7, 30}, {110.54, 31.595,85.601}, *{-25.8558, 175.588,  
> -121.105}*
> }}}
>
> How do I get each 4th sublist element in the last level? I mean, I want  
> to
> get a list looks like:
> {*{-82.1476, **5.17782, -205.36}, **{93.8625, -13.9818,  
> -198.778},**{90.1206,
> 16.9792, -205.314},**{55.3298,**17.7214, -211.648},**{-25.8558, 175.588,
> -121.105}}*
> which only includes every 4th element in the last level. Do I describe my
> question clearly?
>
> Better not to use any "For", simple Map or Thread method are preferred.
>
> Thank you very much!!!
> *
> *


-- 
DrMajorBob@yahoo.com

[toc] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web