Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

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

Search the Archive

Re: extract from list and keep track

  • To: mathgroup at smc.vnet.net
  • Subject: [mg62171] Re: [mg62131] extract from list and keep track
  • From: danl at wolfram.com
  • Date: Sun, 13 Nov 2005 02:08:51 -0500 (EST)
  • References: <200511120831.DAA19138@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

> I'm trying to write a program for the following. Seems simple, but I'm
> unable to to do it:
>
> I have a list of increasing odd numbers. I want to create a table that
> indicates which numbers in this list are 1(mod 8) and 7(mod 8) and what
> position in the list these numbers have.
> Then I would like to have them removed form the list to get a new
> (reduced)
> list.
>
>
> Example: 3, 7, 9, 21, 43, 57, 63, 71, 75, 99.   I want to form this a
> Table:
>
> 2      7
> 3      9
> 6    57
> 7    63
> 8    71
>
> 7 is 7(mod 8) and is the 2nd member in the list, 9 is 1(mod 8) and is the
> 3rd member in the list, etc.
>
> The next list would become, having removed the ones in the table:
>
> 3, 21, 43, 75, 99
>
> And I would be using some other congruence criteria. Etc.
>
> Thanks,
> Andrea


One approach is to sow elements into two separate lists, then reap the
results. A small amount of code must then be expended to get to the
desired level of the resulting lists.

In[31]:=
extractAnKeepRest[ll_List,cond_]:=
 Map[First,First[
   Rest[Reap[MapIndexed[If[cond[#1],Sow[{#2[[1]],#1},1],Sow[#,2]]&,ll
      ],{1,2}]]]]

In[9]:= cond[a_]:=Abs[Mod[a,8,-1]]===1

In[4]:= ll={3,7,9,21,43,57,63,71,75,99}

In[32]:= {extracted,dregs}=extractAnKeepRest[ll,cond]

Out[32]= {{{2,7},{3,9},{6,57},{7,63},{8,71}},{3,21,43,75,99}}


Daniel Lichtblau
Wolfram Research



  • Prev by Date: Re: Tilting at Windmills?
  • Next by Date: Re: Re: Re: Plot Angle between Vectors
  • Previous by thread: extract from list and keep track
  • Next by thread: Re: extract from list and keep track