Message Boards Message Boards

0
|
4973 Views
|
8 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Position of a subset by its length and member?

Posted 8 years ago

Hello

I am struggling to understand how Position and Select work when using patterns. Although I've got many examples right (thanks to the help of the members of this list) I still get stuck trying to find the right pattern (rule?) for some examples.

list={{y3}, {y0 y3, y3}, {y0 y1 y3, y1 y3}, {}}

I need to find the position of {y3} so I tried

Position[list, _?((Length[#] == 1  &) \[And] (MemberQ[#, y3] &))]

but it didn't work. I am definitely missing something since ((Length[lista[[1]]] == 1) \[And] ( MemberQ[lista[[1]], y3])) returns true as expected. Please tell what I am missing.

Many thanks

Cheers

Ed

POSTED BY: Eduardo Mendes
8 Replies
Posted 8 years ago

Many thanks. I can see the difference.

POSTED BY: Eduardo Mendes

...and have a look at

Hold[Position[ list, _?(Length[#] == 1 \[And] MemberQ[#, y3] &)]] // FullForm

and

Hold[Position[ list, _?(Length[#] == 1 \[And] MemberQ[#, y3]) &]] // FullForm

note the different expressions for Function[...]

POSTED BY: Hans Dolhaine
Posted 8 years ago

I see. Many thanks.

Cheers

Ed

POSTED BY: Eduardo Mendes

I suppose it is important whether the & is within the parenthesis or not:

Position[list, _?(Length[#] == 1 \[And] MemberQ[#, y3] &)]

and

Position[list, _?(Length[#] == 1 \[And] MemberQ[#, y3]) &]

does not work.

Cheers,

M.

POSTED BY: Marco Thiel
Posted 8 years ago

Hi Marco

Many thanks. So the problem was the extra parentheses? (() and ()) is not allowed, is it? Do you know why not?

Cheers

Ed

POSTED BY: Eduardo Mendes
Posted 8 years ago

Hi Marco

Many thanks again. Would you mind telling me why MemberQ[list[[1]] and not in the way I did?

Yes, I want to generalize by using multiple conditions.

Cheers

Ed

POSTED BY: Eduardo Mendes

Hi Ed,

you are actually right. I copied the wrong line:

Position[list, _?(Length[#] == 1 \[And] MemberQ[#, y3] &)]

is better. The only problem was a matter of parenthesis.

Sorry,

Marco

POSTED BY: Marco Thiel

Hi Ed,

you were actually quite close:

Position[list, _?(Length[#] == 1 \[And] MemberQ[list[[1]], y3] &)]

Of course, there are easier ways in this case:

Position[list, {y3}]

but you might be asking because you wan too generalise this.

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