Re: split
- To: mathgroup at smc.vnet.net
- Subject: [mg73646] Re: split
- From: "dimitris" <dimmechan at yahoo.com>
- Date: Fri, 23 Feb 2007 04:48:58 -0500 (EST)
- References: <erjo7l$nrf$1@smc.vnet.net>
Hello. Note first the usage mesage for Split function Information@Split "Split[list] splits list into sublists consisting of runs of identical elements. Split[list, test] treats pairs of adjacent \ elements as identical whenever applying the function test to them yields True." Attributes[Split] = {Protected} Pay special attentions to "...identical elements...". Here is your list z = {1, 3, 2, 6, 4, 7, 5, 1, 7} {1, 3, 2, 6, 4, 7, 5, 1, 7} Let wrap your Input with Trace Split[z,#1<=3&]//Trace {{z,{1,3,2,6,4,7,5,1,7}},Split[{1,3,2,6,4,7,5,1,7},#1=E2=89=A43&],{(#1=E2 =89=A43&) [1,3],1=E2=89=A43, True},{(#1=E2=89=A43&)[3,2],3=E2=89=A43,True},{(#1=E2=89=A43&)[2,6],2 =E2=89=A43,True},{(#1=E2=89=A43&) [6,4], 6=E2=89=A43,False},{(#1=E2=89=A43&)[ 4,7],4=E2=89=A43,False},{(#1=E2=89=A43&)[7,5],7=E2=89=A43,False},{(#1=E2 =89=A43&)[5,1],5=E2=89=A43,False}, {(#1=E2=89=A43&)[ 1,7],1=E2=89=A43,True},{{1,3,2,6},{4},{7},{5},{1,7}}} I think you understand now the Output {{1,3,2,6},{4},{7},{5},{1,7}}. Anyway modifying a little your command you get the desired result Split[z, #1 <= 3 === #2 <= 3 & ] {{1, 3, 2}, {6, 4, 7, 5}, {1}, {7}} In this way Split considers two adjacent elements of z "identical" if they are both less or equal 3 or if they are both greater than 3. As another demonstration consider Table[Random[Integer, {0, 10}], {100}] Split[%, #1 <= 6 === #2 <= 6 & ] {8, 6, 4, 3, 3, 8, 6, 9, 5, 7, 2, 10, 10, 10, 8, 1, 3, 4, 4, 6, 10, 2, 6, 5, 1, 7, 1, 4, 4, 3, 5, 3, 5, 1, 6, 10, 7, 4, 7, 2, 0, 8, 0, 2, 2, 3, 6, 7, 6, 8, 0, 9, 1, 5, 1, 5, 10, 5, 2, 4, 5, 3, 5, 3, 10, 5, 3, 6, 7, 9, 8, 3, 1, 8, 0, 0, 9, 2, 4, 9, 2, 8, 5, 5, 9, 6, 2, 8, 4, 1, 0, 10, 6, 5, 5, 8, 0, 10, 1, 3} {{8}, {6, 4, 3, 3}, {8}, {6}, {9}, {5}, {7}, {2}, {10, 10, 10, 8}, {1, 3, 4, 4, 6}, {10}, {2, 6, 5, 1}, {7}, {1, 4, 4, 3, 5, 3, 5, 1, 6}, {10, 7}, {4}, {7}, {2, 0}, {8}, {0, 2, 2, 3, 6}, {7}, {6}, {8}, {0}, {9}, {1, 5, 1, 5}, {10}, {5, 2, 4, 5, 3, 5, 3}, {10}, {5, 3, 6}, {7, 9, 8}, {3, 1}, {8}, {0, 0}, {9}, {2, 4}, {9}, {2}, {8}, {5, 5}, {9}, {6, 2}, {8}, {4, 1, 0}, {10}, {6, 5, 5}, {8}, {0}, {10}, {1, 3}} Kind Regards Dimitris =CE=9F/=CE=97 Arkadiusz.Majka at gmail.com =CE=AD=CE=B3=CF=81=CE=B1=CF=88=CE =B5: > 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