Hello, I am trying to find certain chemical compounds in Chemical Data who contain certain substrings. I have found a solution but I am not very satisfied with. Is there a better one ? See attach.
Thank you for your time,
Chiel Geeraert
Hello, I am trying to find certain chemical compounds in Chemical Data who contain certain substrings. I have found a solution but I am not very satisfied with. Is there a better one ? See attach.
Thank you for your time,
Chiel Geeraert
You have given a perfectly good solution. I don’t see how it can be significantly improved.
I am not sure what version of Mathematica are you using. I am using latest 10.1 version. It returns ChemicalData as list of so called Entity (orange things below).
chedat = ChemicalData[];
chedat[[;; 5]]
![]()
This is why below I use CommonName. Besides that, using pattern ___ or BlankNullSequence instead of __ or BlankSequence will also give you the “border” cases when hydrogen has no characters on the right or / and left.
Flatten[StringCases[CommonName[chedat], __ ~~ "hydro" ~~ __ ]] // Length
4274
Flatten[StringCases[CommonName[chedat], ___ ~~ "hydro" ~~ ___ ]] // Length
4374
Thank you both. I was not very clear why I was not satisfied with my own solution . In my solution I had al those empty “{}”.
But as you showed in your solution with a simple 'Flatten" command I got rid of them. Something I overlooked completely.
Best regards
Chiel Geeraert