4
$\begingroup$

This is from the Document center.

In[548]:= Pick[f[1, 2, 3, 4, 5, 6], {1, 0, 1, 0, 1, 1}, 1]

Out[548]= f[1, 3, 5, 6]

Can anyone explain how this work when data is not a list?

Pick[list,sel,patt]
picks out those elements of list for which the corresponding element of sel matches patt.

How do they determine corresponding element when they aren't the same type? Also not even a list?

EDIT: Do they rely on level to determine corresponding element? How about this case?

sel = {1, Cos[1], Sin[1]}; 
list = {a, b, f[b]}; 
Pick[list, sel, 1] 

Why is the output {a, f[b]} not {a, b}because only 1 in Sin[1] matches the pattern instead?

$\endgroup$
1
  • $\begingroup$ I read that already but still not clear how do they determine corresponding element? $\endgroup$
    – emnha
    Dec 29, 2020 at 12:52

1 Answer 1

7
$\begingroup$

The documentation page says "The heads in list and sel do not have to be List."

Any heads will work, e.g. Pick[f[1, 2, 3, 4, 5, 6], x[1, 0, 1, 0, 1, 1], 1] (unless f or x evaluate to something incompatible).

The first, third, fifth and sixth elements of sel (or x) are 1s, as specified by patt, so those are the elements of f picked.

Addition to the OP's question

sel = {1, Cos[1], Sin[1]}; 
list = {a, b, f[b]}; 
Pick[list, sel, 1]
{a, f[b]}

The documentation says for this case (in Properties & Relations): The comparison in Pick[list, sel, patt ] is purely structural, with no regard to heads.

An argument in sel containing 1 is matched to the argument in list.

Pick[{f[a], b, f[b, c]}, {Sin[0], Cos[1], x[0, 1]}, 1]
{f[c]}
$\endgroup$
1
  • $\begingroup$ Do they rely on level to determine corresponding element? How about this case? sel = {1, Cos[1], Sin[1]}; list = {a, b, f[b]}; Pick[list, sel, 1] why the output is {a, f[b]} not {a, b}? $\endgroup$
    – emnha
    Dec 29, 2020 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.