Re: sequential elements of list
- To: mathgroup at smc.vnet.net
- Subject: [mg119345] Re: sequential elements of list
- From: fd <felipe.dimer.de.oliveira at gmail.com>
- Date: Tue, 31 May 2011 07:47:54 -0400 (EDT)
- References: <irvrqc$8h9$1@smc.vnet.net>
I am not entirely sure of what you want either, I guess you want
something akin to a function that takes as element of the list as
input and returns the next one - a linked list of some sort.
Let me suggest the following code
mList = {1, 2, 3, 4, 5, 6, 7};
Evaluate[f /@ mList] = RotateLeft[mList]
it may need some re-work to handle the final of the list.
with this code you can access the part n+1 by referring to part n
In[11]:= f[1]
Out[11]= 2
In[7]:= f[2]
Out[7]= 3
In[10]:= f[3]
Out[10]= 4
On May 30, 8:33 pm, Bill Rowe <readn... at sbcglobal.net> wrote:
> On 5/29/11 at 7:38 AM, Paul.McH... at excelitas.com (McHale, Paul)
> wrote:
>
> >Does anyone know of a way to use a pure function to access current
> >and next element of list with uses like Select[]? I know I might be
> >dazed and confused, but I thought there was a way. I.e.
> >mList={1,2,3,4,5,7,6,8};
> >Select[mList, (#? > #??)&]
> >Where #? Is element N and #?? Is element N+1. I would expect the
> >previous code (assuming N?, N?? were valid) to return 7.
>
> It is not clear to me what you want for an output. If you want a
> list of all {mList[[n]],mList[[n+1]]} pairs, this is easily
> accomplished with
>
> Partition[mList,2,1]
>
> If you want a specific pair, say {4,5} from the list above, then
> there are a number of ways to accomplish this such as
>
> Cases[Partition[mList,2,1],{4,_}]
>
> or
>
> mList[[{#,#+1}]]&[Position[mList,4][[1,1]]]