如何将字符串元组的元组转换为单词?

卡敏夸兹

python 中的 ''.join 函数处理一个元组中的字符串。假设我们有字符串的“嵌套”元组元组,如 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中表示输入中使用的符号的有效方法:

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

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

(root, additions)

其中root是字符串,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?)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章