开发者社区> 问答> 正文

怎么看python里的匹配

怎么看python里的匹配

展开
收起
云计算小粉 2018-05-10 20:11:06 1682 0
2 条回答
写回答
取消 提交回答
  • import re

    正则表达式

    2019-11-22 14:43:12
    赞同 展开评论 打赏
  • 作者:NightyNight
    链接:https://www.zhihu.com/question/50356943/answer/308811523
    来源:知乎
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

    def _reshape(shaper, recursive_types):

    fn_lst = [_reshape(e, recursive_types) if e.__class__ in recursive_types else next for e in shaper]
    ret_cls = shaper.__class__
    def apply(itor):
        return ret_cls(fn(itor) for fn in fn_lst)
    return apply
    

    def reshape(shaper, recursive_types=(list, tuple, set)):

    _apply = _reshape(shaper, set(recursive_types))
    def apply(seq):
        return _apply(iter(seq))
    return apply
    

    shaper = [(1, 2, 3, 6), {2, 3, 6}, [2, [2]]]
    print(shaper)

    [(1, 2, 3, 6), {2, 3, 6}, [2, [2]]]

    from collections import Iterable
    flatten = lambda nested: list(filter(lambda _: _,

                                     (lambda _: ((yield from flatten(e)) if isinstance(e, Iterable) else (yield e) for e in _))(nested)))

    lst = flatten(shaper)
    print(lst)

    [1, 2, 3, 6, 2, 3, 6, 2, 2]

    reshaper = reshape(shaper)

    print(reshaper(lst))

    [(1, 2, 3, 6), {2, 3, 6}, [2, [2]]]

    这个东西是parser。上面代码里的模型(reshape, _reshape),我认为就是parser的精髓(话说上面这个确实可以直接拿来当parser用)

    2019-07-17 22:25:41
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载