Re: split
- To: mathgroup at smc.vnet.net
- Subject: [mg73634] Re: split
- From: "dkr" <dkrjeg at adelphia.net>
- Date: Fri, 23 Feb 2007 04:42:27 -0500 (EST)
- References: <erjo7l$nrf$1@smc.vnet.net>
On Feb 22, 4:36 am, Arkadiusz.Ma... at gmail.com wrote: > Hi, > > I want to split a list, say > > z = {1, 3, 2, 6, 4, 7, 5,1,7}; > > into sublist of elements that are less or equal 3. > > so I want to obtain > > {{1,3,2},{6,4,7,5},{1},{7}} > > How to do it? Probably by applying Split, but what to put in Test? > > Split[z,#<=3&] gives : > > {{1, 3, 2, 6}, {4}, {7}, {5}, {1, 7}} > > Why 6 was put in first sublist together with 1, 3, and 2 since 6>3 and > should be together with 4 in the second sublist? > > Thanks, > > Arek Arek, >From your description of what you want to obtain, it appears that you want to split your list into runs of elements that are all less than or equal to 3, or are all greater than 3. In[8]:= z = {1, 3, 2, 6, 4, 7, 5,1,7}; Split[z,(#1<=3&<=3)||(#1>3&>3)&] Out[9]= {{1,3,2},{6,4,7,5},{1},{7}} The problem with your use of Split is that your test has only one variable. Effectively, what your test is requiring for any pair of adjacent elements is that the first element of the pair be less than or equal to 3, while the second element can be anything. Thus the adjacent pair (2,6) satisfies your test, and the 6 gets put in the same sublist as the 2. dkr