Position of a subset by its length and member?

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

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

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

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

Hi Marco

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

Cheers

Ed

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.

I see. Many thanks.

Cheers

Ed

…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[…]

Many thanks. I can see the difference.