Re: split
- To: mathgroup at smc.vnet.net
- Subject: [mg73606] Re: split
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Fri, 23 Feb 2007 04:27:18 -0500 (EST)
- Organization: The Open University, Milton Keynes, UK
- References: <erjo7l$nrf$1@smc.vnet.net>
Arkadiusz.Majka 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? Hi Arek, The built-in function Split compares two consecutive elements at once. So what we want is some lists that are singletons or for which any two consecutive elements are less or equal to three. Also, we want some other lists that are singletons or for which two consecutive elements are greater than three. This is summed up in the expression below. In[1]:= z = {1, 3, 2, 6, 4, 7, 5, 1, 7}; Split[z, (#1 <= 3 && #2 <= 3) || (#1 > 3 && #2 > 3) & ] Out[2]= {{1, 3, 2}, {6, 4, 7, 5}, {1}, {7}} Regards, Jean-Marc