## Selecting Sub-lists After Split

### Selecting Sub-lists After Split

Hello everyone,

Suppose I have a list of real number called myList. I split it into
sub-lists of positive and negative numbers called myNewList by doing
this (there may be a more elegant way):

myNewList = Split[myList, #1 #2 > 0 &]

Now, how can I select all of the sub-lists of negative numbers from
myNewList?

Any tips would be most appreciated.

Regards,

Gregory

### Selecting Sub-lists After Split

Here is a newbie approach:

First[Last[Reap[i = 1; While[i <= Length[newlst],
If[First[newlst[[i]]] < 0, Sow[newlst[[i]]], Null]*
i++; ]]]]

J=E1nos

========================
"I think I may need a bathroom break? Is this possible?" --G.W.Bush
http://www.yqcomputer.com/

### Selecting Sub-lists After Split

Select[myNewList, Negative@First[#]&]

Szabolcs

### Selecting Sub-lists After Split

walk == Rest@NestList[# + Random[] - 1/2 &, 0, 1000];
signedRuns == Split[walk, #1 #2 > 0 &];
Length /@ signedRuns
negativeRuns == Select[signedRuns, First[#] < 0 &];
Length /@ negativeRuns

{12, 11, 2, 22, 1, 207, 2, 3, 78, 1, 202, 38, 66, 355}

{12, 2, 1, 2, 78, 202, 66}

I didn't let negativeRuns display, but I think that's what you wanted.

Bobby

On Fri, 08 Jun 2007 04:34:49 -0500, Gregory Lypny

### Selecting Sub-lists After Split

> Hello everyone,

Select[myNewList, Negative[First[#]] &]
should accomplish it.
Regards,
Don

### Selecting Sub-lists After Split

My suggestion:

In[4]:=
list={1,2,-3,-7,4,-8,9,12,-3};
newlist={Select[list,#>0 &],Select[list,#<0 &]}

Out[5]=
{{1,2,4,9,12},{-3,-7,-8,-3}}

Now can access the positive sublist and negative sublist using newlist[[1,i]] for the positive and newlist[[2,i]] for the negatives.

### Selecting Sub-lists After Split

On 6/8/07 at 5:34 AM, XXXX@XXXXX.COM (Gregory Lypny)

A simple way to do this would be to use pattern matching and Cases.

=46irst generate so data

In[12]:= data = Split[RandomReal[{-1, 1}, 20], #1 #2 > 0 &]

Out[12]= {{0.515309},{-0.014795},{0.329838,0.896598},{-0.414435,
-0.326671,-0.347789,-0.627698,-0.763855,-0.85736,-0.774624},{0.861427,
0.387308,0.590255},{-0.502033},{0.813789},{-0.748274,-0.297541,
-0.170911,-0.948834}}

then to pick out the sub-lists with negative values

In[13]:= Cases[data, {_?(# < 0 &), ___}]

Out[13]= {{-0.014795},{-0.414435,-0.326671,-0.347789,-0.627698,
-0.763855,-0.85736,-0.774624},{-0.502033},{-0.748274,-0.297541,-0.170911,
-0.948834}}

### Selecting Sub-lists After Split

Hi,
I am not sure if I understand you correctly, but I suggest Select/Cases:

myList = {1, -13, 4, 6, -12.3, -14, 10, 100};
Here you can use Select or Cases:
Select[myList, Negative]
{-13, -12.3, -14}
Cases[myList, _?Negative]
{-13, -12.3, -14}

myNewList = Split[myList, #1 #2 > 0 &]
{{1}, {-13}, {4, 6}, {-12.3, -14}, {10, 100}}

To get the elements you must now use level specification, so Cases is the
more natural choice:
Cases[myList, _?Negative, 2]
{-13, -12.3, -14}

But, if you want sub-lists you should use:

Select[myNewList, VectorQ[#, Negative] &]
{{-13}, {-12.3, -14}}

Greetings from Croatia,
Drago

### Selecting Sub-lists After Split

Select[myNewList, First[#] < 0 &]

### Selecting Sub-lists After Split

Hi Gregory,

The expression Cases[myNewList, v_ /; VectorQ[v, Negative]] will do it.

In[1]:=
myList = RandomInteger[{-5, 5}, {20}]

myNewList = Split[myList, #1*#2 > 0 & ]

Cases[myNewList, v_ /; VectorQ[v, Negative]]

Out[1]=
{-3, -5, 0, -2, 4, -1, -5, -3, -1, 0, -1, 4, 1, -3, 1, -3, 5, 4, 3, 5}

Out[2]=
{{-3, -5}, {0}, {-2}, {4}, {-1, -5, -3, -1}, {0}, {-1}, {4, 1}, {-3},
{1}, {-3}, {5, 4, 3, 5}}

Out[3]=
{{-3, -5}, {-2}, {-1, -5, -3, -1}, {-1}, {-3}, {-3}}

Regards,
Jean-Marc

### Selecting Sub-lists After Split

myList = Table[RandomReal[{-1, 1}], {10}];

myNewList = Split[myList, #1 #2 > 0 &];

Select[myNewList, #[[1]] < 0 &]

{{-0.653948,-0.875564,-0.770361},{-0.240326}}

Bob Hanlon