# Creating a subset of a list according to a condition

Anthony Or shared this question 6 years ago

I wonder if there is a simple way of creating a subset of a list according to a condition. I encountered this problem a few days ago when I was designing an applet for "Linear Programming". I first constructed a list of integer points (list1). I then tried to create another list (list1_1) which contains the elements of list1 lying in the feasible region (named "sol"), using the command "IsInRegion". Originally I thought that there should be a simple command to do so, and I was surprised that I couldn't find one after looking through all the commands in the "List Commands" category.

I finally found of a way out as follows. Since a boolean variable is assumed the value '1' or '0' for 'true' or 'false', I create another list list1_1 by dividing each element (i, j) in list1 by IsInRegion[(i,j), sol] so that all the elements not in the feasible region would becomes undefined (since they are divided by 0), while all the elements in the feasible region would become unchanged (divided by 1). (See the attached.) We can remove these undefined elements using the command "RemoveUndefined".

I don't know whether this way is the only possible way to achieve my aim. If anyone knows a simpler way please kindly share with us.

(The final ver of "Linear Programming" is upload to GeoGebraTube at https://www.geogebra.org/ma... .

Regards,

Anthony

https://ggbm.at/554957

1

Anthony,

I used a similar premise but different method.

It is easier if list1 is flattened and not a list of lists.

list6=Join[list1]

list7=RemoveUndefined[Zip[K / IsInRegion[K, sol], K, list6]]

It would be good if something like this works

list7=KeepIf[IsInRegion[x,sol],list6]

Simon

1

Hi Antony,

Here is another attempt, it doesn't operate in the way I expected but it seems to do the job.

list6 is as Simon has it.

list7 = {}

Then this command: Zip[If[IsInRegion[A, sol], Append[list7, A]], A, list6]

The rest is in the attached.

Michael

https://ggbm.at/554963

1

It would be good if something like this works

list7=KeepIf[IsInRegion[x,sol],list6]

It would also be nice if it was possible to create a tool that takes a (variable length) list and an arbitrary region and returns a new list. Unfortunately the tool creation kit can't cope with tools that complex. It only does it for the specified region (ellipse OR poygon OR inequality etc), can't cope with the variable K in the zip command, specifies a list of exactly two points as input (or even the points itself...). Sigh. I guess you can't have it all...

1

Hi Simon and Michael,

Thank you very much for telling me the use of the Zip, Join, Append and KeepIf commands! The Zip command is particularly useful. I just don't understand how Zip command works when I read its Help (in fact I can't understand the example given by the Help). Also, I was not aware that my list1 is a list of lists before Simon told me. I also overlooked the KeepIf command (because it is not put in the category "List Commands", but in the "Logical Commands" category), although this command cannot solve my problem.

Your suggestions are very useful and I shall improve my applet using your suggestions. Thank you very much! :D

Anthony