[Date Index]
[Thread Index]
[Author Index]
Re: list/sequence problem: a 'keep' list to a list of
*To*: mathgroup at smc.vnet.net
*Subject*: [mg97192] Re: [mg97147] list/sequence problem: a 'keep' list to a list of
*From*: Miles Rains <mileskrains at gmail.com>
*Date*: Sat, 7 Mar 2009 02:41:51 -0500 (EST)
*References*: <200903060926.EAA22779@smc.vnet.net>
Doctor (or is it Sir?),
Very nice, thanks much. I was pretty sure there had to be a more concise,
canonical way to accomplish the task. As you surmised, the first element
would always be True.
You have prompted me to gain a better understanding and appreciation of
FoldList. I've only been using Mathematica for ~10 years, so I still have a lot to
learn :)
Miles
On Fri, Mar 6, 2009 at 12:11, DrMajorBob <btreat1 at austin.rr.com> wrote:
> Sorry, that fails (with "dummy" as second argument to FoldList) if the
> first element of keepPrior is True.
>
> You haven't specified what you want to do in that case, but maybe this
> would do:
>
> keepPrior = {True, False, True, False, True, False, False, True, True,
> True, False, True};
> counter = 0;
> Rest@FoldList[(counter++;
> If[#2, Append[#1, counter], {counter}]) &, {}, keepPrior]
>
> {{1}, {2}, {2, 3}, {4}, {4, 5}, {6}, {7}, {7, 8}, {7, 8, 9}, {7, 8, 9,
> 10}, {11}, {11, 12}}
>
> Bobby
>
>
> On Fri, 06 Mar 2009 14:04:10 -0600, DrMajorBob <btreat1 at austin.rr.com>
> wrote:
>
> keepPrior = {False, True, False, True, False, False, True, True, True,
>> False, True};
>> counter = 0;
>> Rest@FoldList[(counter++; If[#2, Flatten@{#1, counter}, {counter}]) &,
>> dummy, keepPrior]
>>
>> {{1}, {1, 2}, {3}, {3, 4}, {5}, {6}, {6, 7}, {6, 7, 8}, {6, 7, 8,
>> 9}, {10}, {10, 11}}
>>
>> or
>>
>> counter = 0;
>> Rest@FoldList[(counter++; If[#2, Append[#1, counter], {counter}]) &,
>> dummy, keepPrior]
>>
>> {{1}, {1, 2}, {3}, {3, 4}, {5}, {6}, {6, 7}, {6, 7, 8}, {6, 7, 8,
>> 9}, {10}, {10, 11}}
>>
>> Bobby
>>
>> On Fri, 06 Mar 2009 03:26:41 -0600, mkr <MilesKRains at gmail.com> wrote:
>>
>> I have a list of Booleans, indicating whether prior values in sequence
>>> are to be 'kept'. I want to generate a list of accumulated 'kept'
>>> positions for each position in the sequence. The following code
>>> works, but I would love to see a cleaner, more obivous approach.
>>> Anyone?
>>>
>>> In[]=
>>>
>>> keepPrior=
>>> {False,True,False,True,False,False,True,True,True,False,True};
>>>
>>> keptPriors=Function[aclist,If[Split[aclist][[-1,1]]==True,Range[Length
>>> [aclist]-Length[Split[aclist][[-1]]],Length[aclist]],{Length
>>> [aclist]}]]/@Rest[FoldList[Append,{},keepPrior]];
>>>
>>> TableForm[Transpose[{keepPrior,ToString/@keptPriors}],TableHeadings->
>>> {Automatic,None}]
>>>
>>>
>>> Out[]=
>>>
>>> 1 False {1}
>>> 2 True {1, 2}
>>> 3 False {3}
>>> 4 True {3, 4}
>>> 5 False {5}
>>> 6 False {6}
>>> 7 True {6, 7}
>>> 8 True {6, 7, 8}
>>> 9 True {6, 7, 8, 9}
>>> 10 False {10}
>>> 11 True {10, 11}
>>>
>>>
>>>
>>
>>
>>
>
>
> --
> DrMajorBob at bigfoot.com
>
Prev by Date:
**Re: Re: "Do What I Mean" - a suggestion for improving**
Next by Date:
**Re: Re: "Do What I Mean" - a suggestion for improving**
Previous by thread:
**Re: list/sequence problem: a 'keep' list to a list of**
Next by thread:
**Re: list/sequence problem: a 'keep' list to a list of 'kept' lists**
| |