Pattern to match only “children” of certain elements

后端 未结 4 2031
隐瞒了意图╮
隐瞒了意图╮ 2020-12-09 21:44

I would like to be able to have a pattern that matches only expressions that are (alternately: are not) children of certain other elements.

For example, a pattern to

4条回答
  •  [愿得一人]
    2020-12-09 22:33

    I am probably misunderstanding you, but, if I do understand correctly you want to match all expressions with head List which have the property that, going upwards in the expression tree, we'll never meet a Graphics. I m not sure how to do this in one pass, but if you are willing to match twice, you can do something like

    lst = {randhead[5], {1, 2, {3, 5}}, Graphics[Line[{{1, 2}, {3, 4}}]]};
    Cases[#, _List] &@Cases[#, Except@Graphics[___]] &@lst
    (*
    ----> {{1, 2, {3, 5}}}
    *)
    

    which first selects elements so that the Head isn't Graphics (this is done by Cases[#, Except@Graphics[___]] &, which returns {randhead[5], {1, 2, {3, 5}}}), then selects those with Head List from the returned list. Note that I've added some more stuff to lst.

    But presumably you knew this and were after a single pattern to do the job?

提交回复
热议问题