V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
koplyp
V2EX  ›  Python

求助各位大神, 如何将(a,(b,(c,(d,(e,(f))))))转换为(f,(e,(d,(c,(b,(a))))))

  •  
  •   koplyp ·
    lypynwa · 2018-03-17 16:21:41 +08:00 · 7893 次点击
    这是一个创建于 2203 天前的主题,其中的信息可能已经有所发展或是发生改变。
    68 条回复    2018-03-26 15:42:41 +08:00
    j0hnj
        1
    j0hnj  
       2018-03-17 17:35:33 +08:00
    一个笨办法,但是能实现你的要求

    def flatten(l):
    ret = []
    for elem in l:
    if isinstance(elem, list):
    ret.extend(flatten(elem))
    else:
    ret.append(elem)
    return ret


    def reconstruct(l):
    inner = [l[0]]

    for elem in l[1:]:
    inner = [elem, inner]

    return inner


    if __name__ == '__main__':
    print(reconstruct(flatten(['a', ['b', ['c', ['d', ['e', ['f']]]]]])))
    j0hnj
        2
    j0hnj  
       2018-03-17 17:37:38 +08:00
    排版乱了…看这里吧 https://paste.ubuntu.com/p/5F27Gybh4F/
    whileFalse
        3
    whileFalse  
       2018-03-17 17:46:57 +08:00   ❤️ 2
    source = ('a',('b',('c',('d',('e',('f'))))))
    target = None
    while True:
    target = (source[0],) if target is None else (source[0], target)
    if len(source) == 2:
    source = source[1]
    else:
    break

    print(target)
    noNOno
        4
    noNOno  
       2018-03-17 17:51:07 +08:00
    替换掉字符串包含的括号,按字符串内字母(a,b,c,d,e,f)的字典顺序倒排,再按字符串长度加上括号,可以么
    koplyp
        5
    koplyp  
    OP
       2018-03-17 18:04:38 +08:00
    感谢各位大佬,学习了
    jorneyr
        6
    jorneyr  
       2018-03-17 18:09:06 +08:00
    前后一起搜索,找到一对字符进行交换,然后继续搜索
    haozhang
        7
    haozhang  
       2018-03-17 18:32:30 +08:00 via iPhone
    双栈运算了解一下
    summerwar
        8
    summerwar  
       2018-03-17 18:43:11 +08:00   ❤️ 1
    import re
    a = '(a,(b,(c,(d,(e,(f))))))'
    re.sub(r'\((.*?),\((.*?),\((.*?),\((.*?),\((.*?),\((.*?)\)\)\)\)\)\)',r'(\6,(\5,(\4,(\3,(\2,(\1))))))',a)
    ballshapesdsd
        9
    ballshapesdsd  
       2018-03-17 18:47:26 +08:00
    @summerwar #8 万一不是 6 个呢
    summerwar
        10
    summerwar  
       2018-03-17 18:58:39 +08:00
    @ballshapesdsd 以此类推即可。

    解决问题的关键是解决当下的,如果想写一个函数适用于所有情况,那是不可能的
    yeeling
        11
    yeeling  
       2018-03-17 19:11:16 +08:00
    let a = '(a,(b,(c,(d,(e,(f))))))';
    rst = a.substr(0,a.indexOf(')')).split(',').reverse().join(',') + a.substr(a.indexOf(')'));
    console.log(rst);
    这样呢
    l1093178
        12
    l1093178  
       2018-03-17 19:38:41 +08:00
    ast.parse 了解一下?
    rrfeng
        13
    rrfeng  
       2018-03-17 19:55:31 +08:00
    不是双栈,是一个队列的事儿
    az422
        14
    az422  
       2018-03-17 20:39:50 +08:00 via Android   ❤️ 1
    当作一个字符串,从头开始遍历,用数组 arr 记下字母的值。 再次遍历,判断是字母则反向插入 arr 各个值。
    xrlin
        15
    xrlin  
       2018-03-17 21:04:12 +08:00 via iPhone
    楼上正解
    SeanChense
        16
    SeanChense  
       2018-03-17 21:40:36 +08:00
    入栈的时候去掉括号,再出栈配上括号就行了。
    contmonad
        17
    contmonad  
       2018-03-17 22:43:50 +08:00 via iPhone
    任何函数式语言都自带这个操作:reverse
    gowl
        18
    gowl  
       2018-03-17 22:47:13 +08:00
    gowl
        19
    gowl  
       2018-03-17 22:49:54 +08:00
    gowl
        20
    gowl  
       2018-03-17 22:52:01 +08:00
    gowl
        21
    gowl  
       2018-03-17 22:52:39 +08:00
    谁教教我插图片啊 ><
    gowl
        22
    gowl  
       2018-03-17 22:56:44 +08:00
    gowl
        23
    gowl  
       2018-03-17 22:59:20 +08:00
    <img src="https://i.loli.net/2018/03/17/5aad2b85743e9.png" class="embedded_image">
    gowl
        24
    gowl  
       2018-03-17 23:09:04 +08:00
    然后再用 Python 写一个 lisp 解释器就可以了;)
    vegito2002
        25
    vegito2002  
       2018-03-17 23:13:23 +08:00
    这个就是一个单链表反转, 不用栈操作, 直接把字母全都提取出来, 倒序, 然后加括号就行了, 你看你的输出, 除了最后的右括号, 左边的实际上就是一个简单的循环结构, 一个 for loop 完成
    (f,(e,(d,(c,(b,(a,
    去掉最后一个逗号, 然后加上对应数量的右括号就行了;
    gbin
        26
    gbin  
       2018-03-17 23:39:57 +08:00
    gbin
        27
    gbin  
       2018-03-18 00:05:44 +08:00 via Android
    @whileFalse 求详解,get 不到重点。
    contmonad
        28
    contmonad  
       2018-03-18 00:07:18 +08:00   ❤️ 3
    为什么你们都把输入当做字符串,不是 tuple 吗?如果允许稍微改一下题目,在最后加一个 nil 就很简单了:

    l = ('a', ('b', ('c', ('d', ('e', ('f', ()))))))
    acc = ()
    while l:
       acc, l = (l[0], acc), l[1]
    linuap
        29
    linuap  
       2018-03-18 00:30:14 +08:00
    @contmonad
    不用改题目
    看 3l 回答,是一样的方法
    ballshapesdsd
        30
    ballshapesdsd  
       2018-03-18 00:34:59 +08:00
    @contmonad 完美
    imn1
        31
    imn1  
       2018-03-18 00:54:20 +08:00
    1.正则提取->列表 A
    2.A 倒序->B
    3.AB zip ->二维列表 C
    4.替换 C (有需要的话用正则)
    有需要的 escape 一下

    数据源非字符串的话,可以借用 json 过渡
    coffeSlider
        32
    coffeSlider  
       2018-03-18 01:15:04 +08:00   ❤️ 1
    import re
    def wtf(s):
    return re.sub('\w', '{}', s).format(*[i for i in s[::-1] if i.isalpha()])

    "Life is short"
    aokihu
        33
    aokihu  
       2018-03-18 01:36:36 +08:00 via Android
    这个不是二叉树么!
    animal
        34
    animal  
       2018-03-18 01:39:42 +08:00 via Android
    我怎么觉得应该把这串字符转换成树形结构,用中序或者后序遍历
    aminic
        35
    aminic  
       2018-03-18 03:01:17 +08:00 via Android
    递归就可以吧
    nicktogo
        36
    nicktogo  
       2018-03-18 05:17:19 +08:00 via iPhone
    two pointer ?
    qq529633582
        37
    qq529633582  
       2018-03-18 05:48:20 +08:00   ❤️ 2
    >>> def reverse(a):
    ... f = (lambda x, y : (x[0],y) if len(x)==1 else f(x[1], (x[0],y)))
    ... return f(a[1], (a[0],))
    ...
    >>> reverse(('a',('b',('c',('d',('e',('f',)))))))
    ('f', ('e', ('d', ('c', ('b', ('a',))))))
    woodo
        39
    woodo  
       2018-03-18 08:56:03 +08:00
    >>> n=ord('f')-ord('a')
    >>> for x in s:
    ... if x in string.ascii_letters:
    ... print(chr(ord(x)+n), end=''); n-=2
    ... else: print(x,end='')
    ... print()
    realpg
        40
    realpg  
       2018-03-18 09:37:35 +08:00
    @contmonad #28
    @qq529633582 #37

    老师,这题太难了,我自己换了个题啊
    param
        41
    param  
       2018-03-18 10:12:12 +08:00 via Android
    差一点,
    shwomen1234fs
        42
    shwomen1234fs  
       2018-03-18 10:15:26 +08:00
    print(reduce(lambda a, b: "({},({}))".format(str(b), str(a)), "(a,(b,(c,(d,(e,(f))))))".replace("(", "").replace(")", "").split(",")))
    shwomen1234fs
        43
    shwomen1234fs  
       2018-03-18 10:21:11 +08:00
    print("({})".format(reduce(lambda a, b: "{},({})".format(b, a), "(a,(b,(c,(d,(e,(f))))))".replace("(", "").replace(")", "").split(","))))
    SolidZORO
        44
    SolidZORO  
       2018-03-18 10:53:29 +08:00
    写个有可读性的。

    ```
    const source = '(a,(b,(c,(d,(e,(f))))))';
    const sourceList = source.split('');

    const charList = [];
    const keyList = [];

    // Find Char & Key
    sourceList.map((char, i) => {
    if (/[a-z]/i.test(char)) {
    charList.push(char);
    keyList.push(i);
    }
    });

    // Replace sourceList String
    keyList.map((key, i) => {
    sourceList[key] = charList.reverse()[i];
    });

    // (a,(b,(c,(d,(e,(f))))))
    console.log(source);

    // (f,(b,(d,(d,(b,(f))))))
    console.log(sourceList.join(''));
    ```

    https://gist.github.com/SolidZORO/d30fdac96fd00af1be9b77e2c64ab9fa
    SolidZORO
        45
    SolidZORO  
       2018-03-18 10:58:42 +08:00 via iPhone
    🤦‍♂️ 看了下节点,JS 代码误入。
    jazoma
        46
    jazoma  
       2018-03-18 11:47:57 +08:00
    from functools import reduce

    def f(ys,s=[]):
    x,xs = ys
    if type(xs) != tuple:
    return reduce(lambda x,y: (y,(x)), s+[x,xs])
    return f(xs,s+[x])
    jazoma
        47
    jazoma  
       2018-03-18 11:53:03 +08:00
    -from functools import reduce
    -
    -def f(ys,s=[]):
    -----x,xs = ys
    -----if type(xs) != tuple:
    ---------return reduce(lambda x,y: (y,(x)), s+[x,xs])
    -----return f(xs,s+[x])
    aec4d
        48
    aec4d  
       2018-03-18 12:02:33 +08:00
    a = ('a', ('b', ('c', ('d', ('e', ('f'))))))
    from functools import reduce


    def reverse(T):
    ret = []
    while len(T) != 1:
    ret.append(T[0])
    T = T[-1]
    ret.append(T[0])
    return reduce(lambda x, y: (y, x), ret)


    x = reverse(a)
    print(x)
    epicnoob
        50
    epicnoob  
       2018-03-18 13:55:24 +08:00
    大神真多。
    think2011
        51
    think2011  
       2018-03-18 15:24:18 +08:00   ❤️ 2
    乱入一个 JavaScript 的 版本..

    ```js
    const str = '(a,(b,(c,(d,(e,(f))))))'
    const tmp = str.match(/\w/g)

    str.replace(/\w/g, $1 => tmp.pop())
    ```
    lovefantasy
        52
    lovefantasy  
       2018-03-18 16:49:53 +08:00 via Android
    大佬们秀的我头晕
    sun1991
        53
    sun1991  
       2018-03-18 18:07:24 +08:00
    感觉歪了啊, 这题考的应该是 lexer 方面的知识吧?
    xpresslink
        54
    xpresslink  
       2018-03-18 20:56:16 +08:00
    @aokihu 你去看看 lisp 或者 sicp 这个就是链表的实现
    ch3nOr
        55
    ch3nOr  
       2018-03-18 21:31:56 +08:00
    yankebupt
        56
    yankebupt  
       2018-03-18 22:02:26 +08:00
    如果没有特殊规定一般情况怎么处理的话, 我觉得是递归查找最内侧的括号,然后一层一层往外翻,每翻一层,然后把原来内部括号外侧的的元素括起来,替换掉递归层...
    这样改过顺序的
    ((((((f),e),d),c),b),a) 大概会翻成 ((((((a),b),c),d),e),f)
    不知猜的对不对...
    上面代码比较多,有可能有满足这个的代码没看到,提前抱歉
    no1xsyzy
        57
    no1xsyzy  
       2018-03-18 23:11:06 +08:00
    我其实就想知道问题到底是什么。
    输入 /输出 的到底是 字符串 /多层列表?
    另外,中间有一处出现 3 个的话怎么办?换句话说 (a,b,(c))=>?

    如果是按考试风格题目,输入输出均为多层列表,无异常情况,不就是个 reverse 么(把 list 看作 cons )

    def mdreverse(inp, sav=[]):
    if len(inp) == 0:
    return sav
    car, cdr, *_ = inp+[[]]
    return multireverse(cdr, [car, sav] if sav else [car])

    mdreverse([1,[2,[3]]]) # [3, [2, [1]]]
    mdreverse([1]) # [1]
    mdreverse([1,[2,[3,[4,[5,[6]]]]]]) # [6, [5, [4, [3, [2, [1]]]]]]
    mdreverse([]) # []
    Antidictator
        58
    Antidictator  
       2018-03-19 08:59:11 +08:00   ❤️ 1
    @gowl #24 用 imgur 的图可以,你这个好像经常不行
    yangfch3
        59
    yangfch3  
       2018-03-19 09:46:00 +08:00
    正则表达式的平衡组专门用于解决这种 nested 结构的解析等问题,了解一下

    正则:\([^\)]*[^\(\)]*(((?'Open'\([^\(]*)[^\(\)]*)+((?'-Open'\)))+)*(?(Open)(?!))
    源文本:(a,(b,(c,(d,(e,(f))))))
    匹配结果:(a,(b,(c,(d,(e,(f

    拿到匹配结果再处理、reverse

    前提是你的运行环境的正则引擎支持平衡组
    araraloren
        60
    araraloren  
       2018-03-19 10:59:04 +08:00
    @think2011 awesome ! Give u ten B.
    fortunezhang
        61
    fortunezhang  
       2018-03-19 14:33:22 +08:00
    str1 = '(a,(b,(c,(d,(e,(f,g)))))))'
    str1 = str1.replace(')', ' ').replace('(', ' ').replace(',', ' ')
    str1 = str1[::-1].split()
    str1 = '(' + ',('.join(str1) + ')' * len(str1)
    print(str1)
    gowl
        62
    gowl  
       2018-03-19 16:27:54 +08:00
    @Antidictator 多谢~
    gowl
        63
    gowl  
       2018-03-19 16:29:33 +08:00
    gowl
        64
    gowl  
       2018-03-19 16:30:15 +08:00
    dizzy
        65
    dizzy  
       2018-03-19 16:43:39 +08:00
    @param 请问手机上使用 ipython 是什么软件?
    param
        66
    param  
       2018-03-19 19:48:36 +08:00 via Android
    @dizzy termux
    tihiro
        67
    tihiro  
       2018-03-20 19:50:43 +08:00
    def get_elements(s):
    q = []
    while len(s) != 1:
    q.append(s[0])
    s = s[1]
    q.append(s)

    return q


    def nested_list(l):
    res = ()
    ele0 = l[0]
    res += (ele0, )
    l.remove(ele0)
    if len(l) > 0:
    res += ((nested_list(l), ))

    return res


    def main():
    s = ('a', ('b', ('c', ('d', ('e', ('f'))))))
    l = get_elements(s)
    l = l[::-1]
    ll = nested_list(l)
    print(ll)


    if __name__ == '__main__':
    main()
    xiongshengyao
        68
    xiongshengyao  
       2018-03-26 15:42:41 +08:00
    最简单的思路…一个队列一个栈
    ```
    def main(source_str):
    el = []
    ret = []
    for i in source_str:
    if i not in ('(', ',', ')'):
    el.append(i)
    ret.append("#")
    else:
    ret.append(i)

    for index, value in enumerate(ret):
    if value == "#":
    ret[index] = el.pop()

    return "".join(ret)


    if __name__ == "__main__":
    result = main("(a,(b,(c,(d,(e,(f))))))")
    print(result)
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5917 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 02:37 · PVG 10:37 · LAX 19:37 · JFK 22:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.