Message Boards Message Boards

0
|
4376 Views
|
10 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Why does Pick[ ] work but Select[ ] fails?

Posted 2 years ago

One could go mad trying to understand the WL

enter image description here

Both Pick and Select respond to True, why does one work but not the other??

POSTED BY: Raymond Low
10 Replies
Posted 2 years ago

Thanks Rohit -- but I would argue that the documentation needs to be changed from what I copied in my first statement above. Below is all the documentation a users gets, the rest are examples.

enter image description here

enter image description here

Pick[] takes an element from a list based on pattern matching done on a second list. From what you are telling me, Select[] takes an element from a list based on a function preformed on the original list -- I don't see how someone could have figured that out from a careful reading of the documentation -- But thank you very much, that was very helpful and now I am aware of it.

POSTED BY: Raymond Low
Posted 2 years ago

Thanks Rohit, but now I am back to being confused...

sec178 = {"DE:UWS", "DE:VVD", "DE:ZEF", "F:0AZ", "F:1EZA", "F:1K5", 
  "F:1K50", "F:1VX", "F:36E", "F:3ER", "F:49Q", "F:4X8", "F:4Y4", 
  "F:5AR1", "F:5NQ", "F:5ZK", "F:6RY", "F:7AR", "F:86B", "F:A19", 
  "F:AQK", "F:BIU2", "F:C36", "F:C3K", "F:CH6", "F:CNE", "F:DGN", 
  "F:FCC", "F:H2Y", "F:LT5", "F:NGO", "F:PFX1", "F:PNU", "F:RPU", 
  "F:SCB", "F:SCB0", "F:SUD", "F:SY9", "F:TG9", "F:TIA", "F:TMR", 
  "F:TMR0", "F:UL5A", "F:UWS", "F:VVD", "F:VVDH", "F:WA3", "F:WCN", 
  "F:ZEF", "F:ZSC", "HK:00257", "HK:00895", "HK:01065", "HK:01363", 
  "HK:01483", "HK:01857", "HK:03989", "L:BIFF", "L:RWI", "MB:533269", 
  "NASDAQ:AQMS", "NASDAQ:CWST", "NASDAQ:ECOL", "NASDAQ:HCCI", 
  "NASDAQ:JAN", "NASDAQ:PESI", "NASDAQ:QRHC", "NASDAQ:SMED", 
  "NASDAQ:SRCL", "NYSE:AWX", "NYSE:CHRA", "NYSE:CLH", "NYSE:CVA", 
  "NYSE:GFL", "NYSE:GFLU", "NYSE:MEG", "NYSE:NES", "NYSE:NRCG", 
  "NYSE:RSG", "NYSE:WCN", "NYSE:WM", "PA:ALEUP", "PA:ALTUV", "PA:DBG",
   "PA:DBGBS", "PA:SCHP", "PA:VIE", "SS:600874", "SZ:002672", 
  "TO:ANRG", "TO:GFL", "TO:WCN", "TSE:5857", "TSE:9793"}

In[168]:= AA = StringMatchQ[sec178, {"NYSE:*", "NASDAQ:*"}]

Out[168]= {False, False, False, False, False, False, False, False, \
False, False, False, False, False, False, False, False, False, False, \
False, False, False, False, False, False, False, False, False, False, \
False, False, False, False, False, False, False, False, False, False, \
False, False, False, False, False, False, False, False, False, False, \
False, False, False, False, False, False, False, False, False, False, \
False, False, True, True, True, True, True, True, True, True, True, \
True, True, True, True, True, True, True, True, True, True, True, \
True, False, False, False, False, False, False, False, False, False, \
False, False, False, False}

In[169]:= BB = StringMatchQ[#, {"NYSE:*", "NASDAQ:*"}] & /@ sec178

Out[169]= {False, False, False, False, False, False, False, False, \
False, False, False, False, False, False, False, False, False, False, \
False, False, False, False, False, False, False, False, False, False, \
False, False, False, False, False, False, False, False, False, False, \
False, False, False, False, False, False, False, False, False, False, \
False, False, False, False, False, False, False, False, False, False, \
False, False, True, True, True, True, True, True, True, True, True, \
True, True, True, True, True, True, True, True, True, True, True, \
True, False, False, False, False, False, False, False, False, False, \
False, False, False, False}

In[170]:= AA === BB

Out[170]= True

In[172]:= Pick[sec178, AA]
Pick[sec178, BB]

Out[172]= {"NASDAQ:AQMS", "NASDAQ:CWST", "NASDAQ:ECOL", \
"NASDAQ:HCCI", "NASDAQ:JAN", "NASDAQ:PESI", "NASDAQ:QRHC", \
"NASDAQ:SMED", "NASDAQ:SRCL", "NYSE:AWX", "NYSE:CHRA", "NYSE:CLH", \
"NYSE:CVA", "NYSE:GFL", "NYSE:GFLU", "NYSE:MEG", "NYSE:NES", \
"NYSE:NRCG", "NYSE:RSG", "NYSE:WCN", "NYSE:WM"}

Out[173]= {"NASDAQ:AQMS", "NASDAQ:CWST", "NASDAQ:ECOL", \
"NASDAQ:HCCI", "NASDAQ:JAN", "NASDAQ:PESI", "NASDAQ:QRHC", \
"NASDAQ:SMED", "NASDAQ:SRCL", "NYSE:AWX", "NYSE:CHRA", "NYSE:CLH", \
"NYSE:CVA", "NYSE:GFL", "NYSE:GFLU", "NYSE:MEG", "NYSE:NES", \
"NYSE:NRCG", "NYSE:RSG", "NYSE:WCN", "NYSE:WM"}

In[178]:= Select[sec178, AA]
Select[sec178, BB]
Select[sec178, StringMatchQ[#, {"NYSE:*", "NASDAQ:*"}] &]

Out[178]= {}

Out[179]= {}

Out[180]= {"NASDAQ:AQMS", "NASDAQ:CWST", "NASDAQ:ECOL", \
"NASDAQ:HCCI", "NASDAQ:JAN", "NASDAQ:PESI", "NASDAQ:QRHC", \
"NASDAQ:SMED", "NASDAQ:SRCL", "NYSE:AWX", "NYSE:CHRA", "NYSE:CLH", \
"NYSE:CVA", "NYSE:GFL", "NYSE:GFLU", "NYSE:MEG", "NYSE:NES", \
"NYSE:NRCG", "NYSE:RSG", "NYSE:WCN", "NYSE:WM"} 

The results of code AA and BB are exactly the same, what is different about the last part of the code in Select[sec178, StringMatchQ[#, {"NYSE:", "NASDAQ:"}] &]???

Pick[] works with both AA and BB, but Select[] does not work with AA nor does it work with BB, but has to have it at BurgerKing -- have it, its own way. Hard to make sense of a code with such anomalies?? What am I missing here???

POSTED BY: Raymond Low
Posted 2 years ago
Select[sec178, AA]

Does not work because AA is not a function that evaluates to True or False, it is a List that contains True or False elements. Re. why Pick works, carefully read the documentation.

Select[sec178, StringMatchQ[#, {"NYSE:*", "NASDAQ:*"}] &]

Works because StringMatchQ[#, {"NYSE:*", "NASDAQ:*"}] & is a function.

POSTED BY: Rohit Namjoshi
Posted 2 years ago

Slot, # & iterates through the list

No Select iterates over the list and calls the specified function for each element. The result contains the elements for which the function value is True.

StringMatchQ implicitly iterates over lists

list = {"NYSE:123", "NASDAQ:456", "NotRelevent"};
StringMatchQ[list, {"NYSE:*", "NASDAQ:*"}]
(* {True, True, False} *)
POSTED BY: Rohit Namjoshi
Posted 2 years ago

Thank you Pedro -- we are getting close, but I am still a little slow. So what I gather is that Slot, # & iterates through the list, like Table, but without the instructions of ,{i, 1, 178}. and StringMatchQ, by itself, looks at only the first element, makes an assessment and quits, not bothering to look at the rest of the elements in the list??

But what did Pick[] do to StringMatchQ so now it has the vitality to ponder the whole list and give me the correct results???

POSTED BY: Raymond Low
Posted 2 years ago

One could go mad trying to understand the WL

There are many learning resources. e.g. EIWL, Wolfram-U

POSTED BY: Rohit Namjoshi
Posted 2 years ago

What is a predicate function?? Slot works but List does not, why

In[89]:= Select[sec1, StringMatchQ[sec1, {"NYSE:*", "NASDAQ:*"}]]

Out[89]= {}

In[121]:= Select[sec1, StringMatchQ[#, {"NYSE:*", "NASDAQ:*"}] &]

Out[121]= {"NASDAQ:ADV", "NASDAQ:ADVWW", "NASDAQ:BAOS", \
"NASDAQ:BOMN", "NASDAQ:CMPR", "NASDAQ:CNET", "NASDAQ:CPOP", \
"NASDAQ:CRTO", "NASDAQ:FLNT", "NASDAQ:IAS", "NASDAQ:ICLK", \
"NASDAQ:ISIG", "NASDAQ:MCHX", "NASDAQ:MDCA", "NASDAQ:MGNI", \
"NASDAQ:NCMI", "NASDAQ:QNST", "NASDAQ:SRAX", "NASDAQ:TRKA", \
"NASDAQ:TRMR", "NASDAQ:VISN", "NASDAQ:WIMI", "NASDAQ:XNET", \
"NYSE:CCO", "NYSE:DLX", "NYSE:DMS", "NYSE:EEX", "NYSE:INUV", \
"NYSE:IPG", "NYSE:OMC", "NYSE:QUOT", "NYSE:TSQ", "NYSE:WPP"}
POSTED BY: Raymond Low

Hello Raymond!

documentation of Select

According to the documentation, Select[...] takes two arguments, a "list" and a criteria "crit" that must be a function. Why? Because every single item from the list will be applied for the criteria "crit".

When you do something like:

In[1]:= Select[mylist, StringMatchQ[mylist, "..."]]

You're not giving a proper criteria.. StringMatchQ itself does not work with a list, only an individual word.

If you do the following:

In[2]:= Select[mylist, StringMatchQ[#, "..."] &]

That will work! Because the second argument of Select is a function, thus, every single String from your list will be tested and selected for those who produce True as output.

Hope it helps!

Pedro Cabral.

POSTED BY: Pedro Cabral
Select[{"NYSE:123", "NASDAQ:456", "NotRelevent"}, 
 StringMatchQ[#, {"NYSE*", "NASDAQ*"}] &]

In Select you need a predicate function in the second argument.

POSTED BY: Shenghui Yang

Welcome to Wolfram Community! Please make sure you know the rules: https://wolfr.am/READ-1ST

The rules explain how to format your code properly. Posting code Images doesn't help other members to copy your code. Please EDIT your post and make sure code blocks start on a new paragraph and look framed and colored like this.

int = Integrate[1/(x^3 - 1), x];
Map[Framed, int, Infinity]

You can also embed notebook or attach notebook.

enter image description here

POSTED BY: Moderation Team
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