首页 > 解决方案 > 如何将字符串元组的元组转换为单词?

问题描述

''.join python 中的函数在一个元组中处理字符串。假设我们有 'nested' 字符串元组的元组,如 txt 输入中所示

这里有一些结构作为树。有词根。例如,sea seam 将“se”作为一个词根。他们还与“sex”和“seven”共享“se”作为词根,但“s”只是其他单词“soup”的词根。ram 没有任何共享根。

   _ _ _ r _ _ _ a _ _ _ m%
  /
 /          _ _ _ o _ _ _ u _ _ _ p
-          /
 \        /          _ _ _a% _ _ _ m%
  \_ _ _ s          /
          \        /
           \_ _ _ e _ _ _ x%
                   \
                    \_ _ _ v _ _ _ e _ _ _ n%


#input
txt = "(ram%+s(e(a%m%+x%+ven%)+o%up%))"

#output
[ram, sea, seam, sex, seven, soup]

输出应该是带有根的单词列表,使用“+”分隔。记住并按以下两个条件排序

+ refers to start new word

% refers to the end of the word

希望你能明白我的意思,并希望你能提供帮助。

标签: python

解决方案


你解释这个问题的方式没有最有意义(至少对我来说),但这是我回答它的机会:

您提供的输入格式虽然不错,但不能直接在 python 代码中使用。这是一种有效的方式,您可以在 python 中表示输入中使用的符号:

# empty root node, use empty string
txt = ("", "ram", ("s", "oup", ("e", "am", "x", "ven")))

每个元组都遵循以下形式:

(root, additions)

whereroot是一个字符串,additions是一个元组或一个字符串。要解析txt为有效列表,您可以编写一个递归函数,如下所示:

def parse(x):
    # return [x] if x is just a string.
    if isinstance(x, str): return [x]

    root, additions = x[0], x[1:] 

    words = []
    for addition in additions:

        # recursively 'flatten' all additions in current node
        sub_additions = parse(addition)

        # add the root word to each sub_addition
        sub_additions = [root + addition for addition in sub_additions]

        # add the new sub additions to the list
        words = words + sub_additions

    return words

要使用parse,只需调用它:例如,parse(txt)

注意事项:

  • 不确定这是否是最简单或最 Pythonic 的方法。
  • 仅适用于一组嵌套的元组和字符串,不接受其他类型。
  • 不使用您在答案中使用的确切输入格式。(因为它不是有效的python?)

推荐阅读