Message Boards Message Boards

1
|
6223 Views
|
5 Replies
|
7 Total Likes
View groups...
Share
Share this post:

[?] Select from a list of lists based on position criteria?

Posted 7 years ago

I would like to make a selection from a list of lists using position criteria, in particular, for example I would like to find all lists in my list of lists which have '0' in positions {{1},{3}}.

I am trying to make it work with something like

Select[{0,0,1,0},{0,0,0,0},{0,1,0,1},{1,0,0,0}, SubsetQ[Position[#,0],{{1},{3}}]

which will output {{0,0,0,0},{0,1,01}}

What is my mistake, or is there an easier way to do this?

Thank you Mary

POSTED BY: Mary Shepard
5 Replies

Dear All,

sorry my previous post was a bit short and I failed to explain. There are of course lots of solutions. Here are some more:

Select[{{0, 0, 1, 0}, {0, 0, 0, 0}, {0, 1, 0, 1}, {1, 0, 0, 0}}, #[[{1, 3}]] == {0, 0} &]

Here's another one:

Select[{{0, 0, 1, 0}, {0, 0, 0, 0}, {0, 1, 0, 1}, {1, 0, 0, 0}}, #.{1, 0, 1, 0} == 0 &]

Also there is one which is quite close to Mary's original solution:

Select[{{0, 0, 1, 0}, {0, 0, 0, 0}, {0, 1, 0, 1}, {1, 0, 0, 0}}, SubsetQ[Position[#, 0], {{1}, {3}}] &]

Note that there are tiny differences between all of these. For example we can distinguish between approximate numbers and exact numbers:

Select[{{0, 0, 1, 0}, {0, 0, 0, 0}, {0, 1, 0, 1}, {1, 0, 0, 0}}, #[[1]] == 0 && #[[3]] == 0 &]

and

Select[{{0, 0, 1, 0}, {0, 0, 0, 0}, {0, 1, 0, 1}, {1, 0, 0, 0}}, #[[1]] === 0 && #[[3]] === 0 &]

both work, but they react differently to finite precision:

Select[1. {{0, 0, 1, 0}, {0, 0, 0, 0}, {0, 1, 0, 1}, {1, 0, 0, 0}}, #[[1]] === 0 && #[[3]] === 0 &]

and

Select[1. {{0, 0, 1, 0}, {0, 0, 0, 0}, {0, 1, 0, 1}, {1, 0, 0, 0}}, #[[1]] == 0 && #[[3]] == 0 &]

give different results. Same can be achieved with Cases.

Cheers,

Marco

POSTED BY: Marco Thiel

Mary, Marco gave you an great answer with Select and there is also Selects brother Cases

data = {{0, 0, 1, 0}, {0, 0, 0, 0}, {0, 1, 0, 1}, {1, 0, 0, 0}};

Cases[data, {0, _, 0, _}]

{{0, 0, 0, 0}, {0, 1, 0, 1}}

and probably a bunch of other solutions. But most importantly you had a wrong syntax to start with. Your data were not list of lists. If you paste your code in Mathematica you see a lot of red and purple, which means errors:

enter image description here

Compare Marco's and my way of data syntax with yours. In programing even a single type might destroy all the rest of code.

POSTED BY: Vitaliy Kaurov
Posted 7 years ago

Thanks a lot to both of you! both solutions are sufficient.

What I don't understand yet is why I don't completely understand why the error isn't removed by formating as a list of lists:

Select[{{0,0,1,0},{0,0,0,0},{0,1,0,1},{1,0,0,0}}, SubsetQ[Position[#,0],{{1},{3}}]

Is the problem with the #? Or is it just not ok to call up a list from a list of lists?

POSTED BY: Mary Shepard

If you look at Vitaliy's comments you will see that there are colours that highlight that your syntax is not correct. For examples your parenthesis don't match and you also missed a "&".

Select[{{0, 0, 1, 0}, {0, 0, 0, 0}, {0, 1, 0, 1}, {1, 0, 0, 0}}, SubsetQ[Position[#, 0], {{1}, {3}}] &]

is close to your solution.

Cheers,

Marco

POSTED BY: Marco Thiel

Hi Mary,

Select[{{0, 0, 1, 0}, {0, 0, 0, 0}, {0, 1, 0, 1}, {1, 0, 0, 0}}, #[[1]] == 0 && #[[3]] == 0 &]

Cheers,

Marco

POSTED BY: Marco Thiel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract