I am trying to learn "core" patterns so I can start using them more often. Consider I am trying to solve the following problem.
data = {2,{4,2,3},5,{7,3},{},4,1}
(assume List
will not appear beyond level 1)
To get only non-list elements I can use the following pattern.
data //. {x:___,{___},y:___}:>{x,y}
Is there a way to do the same using a sub-pattern replacement (regular expressions allow that)
data//.{___,t:{___},___}:>Nothing
(I want to only replace t
with Nothing
)
Next how do I get only list elements.
data //. (...)
Allowed functionality:
• Basic Pattern Objects
• Composite Patterns
• Restrictions on Patterns
• Pattern Defaults
<< Pattern Matching Functions are NOT allowed except for the following >>
• Replace
(third argument levelspec
NOT allowed)
• ReplaceAll
• ReplaceRepeated
Comments
Post a Comment