emmendes
(Eduardo Mendes)
August 20, 2016, 9:22pm
1
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
mthiel
(Marco Thiel)
August 20, 2016, 9:40pm
2
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
emmendes
(Eduardo Mendes)
August 20, 2016, 9:58pm
3
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
mthiel
(Marco Thiel)
August 20, 2016, 10:02pm
4
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
emmendes
(Eduardo Mendes)
August 20, 2016, 10:13pm
5
Hi Marco
Many thanks. So the problem was the extra parentheses? (() and ()) is not allowed, is it? Do you know why not?
Cheers
Ed
mthiel
(Marco Thiel)
August 20, 2016, 10:17pm
6
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.
hdolhaine
(Hans Dolhaine)
August 24, 2016, 12:00am
8
…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[…]
emmendes
(Eduardo Mendes)
August 24, 2016, 2:36pm
9
Many thanks. I can see the difference.