如何找到形成给定输入的可能组合

发布于 2024-12-10 19:34:59 字数 557 浏览 0 评论 0原文

我有一个像这样的列表,例如列表名称是 output 其中有:

[[[o, g], [g, o]], [[o, g, o, d]], [[o, d]], [[t, s, n, e, e, e, n, c, s]], [[t, s, n, e, e]], [[e, n, c, s]]]

我有一个像这样的输入,比如说 input 是:

ogodtsneeencs

现在显然,>输入可以由输出形成。我尝试了 outputsubsequences() 来查找形成 input 的可能组合,但问题是它不适用于所有 代码>输入。

谁能告诉我如何找到等于inputoutput组合?并且可能存储在某个列表中。

提前致谢。

I have a list like this, say for example the list name is output which has:

[[[o, g], [g, o]], [[o, g, o, d]], [[o, d]], [[t, s, n, e, e, e, n, c, s]], [[t, s, n, e, e]], [[e, n, c, s]]]

And I have a input like this, say input is:

ogodtsneeencs

Now obviously, the input can be formed from output. I tried the subsequences() of output to find the possible combinations that form the input, but the thing is it wont work for all the input.

Can anyone say me how I can find the combinations of output that will be equal to input? And possibly store in some list.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花细雨 2024-12-17 19:34:59

鉴于您提供的这一小组测试数据,我想出了这个:

def list = [[['o', 'g'], ['g', 'o']], [['o', 'g', 'o', 'd']], [['o', 'd']], [['t', 's', 'n', 'e', 'e', 'e', 'n', 'c', 's']], [['t', 's', 'n', 'e', 'e']], [['e', 'n', 'c', 's']]]

// For every combination of the lists
def result = list.combinations().collect { combination ->
  // Join them into strings
  combination*.join().with { stringcombo ->
    // Then find every string in the list
    stringcombo.findAll { word ->
      // Which is not a substring of another string in the list
      (stringcombo - word).every { it.indexOf( word ) == -1 }
    }
  }.permutations()*.join() // Then get every String permutation of these remaining strings
}.flatten().unique() // and get them into a single unique list

// And print them out
result.each {
  println it
}

打印出:

ogodtsneeencs
tsneeencsogod

没有更多数据,很难判断它是否正确,但这可能是您

编辑

更新以返回所有内容 的一个很好的起点有效标记的排列

Given just this small set of test data you have supplied, I came up with this:

def list = [[['o', 'g'], ['g', 'o']], [['o', 'g', 'o', 'd']], [['o', 'd']], [['t', 's', 'n', 'e', 'e', 'e', 'n', 'c', 's']], [['t', 's', 'n', 'e', 'e']], [['e', 'n', 'c', 's']]]

// For every combination of the lists
def result = list.combinations().collect { combination ->
  // Join them into strings
  combination*.join().with { stringcombo ->
    // Then find every string in the list
    stringcombo.findAll { word ->
      // Which is not a substring of another string in the list
      (stringcombo - word).every { it.indexOf( word ) == -1 }
    }
  }.permutations()*.join() // Then get every String permutation of these remaining strings
}.flatten().unique() // and get them into a single unique list

// And print them out
result.each {
  println it
}

Which prints out:

ogodtsneeencs
tsneeencsogod

Without more data, it's hard to tell if it is correct, but it might be a good starting place for you

edit

Updated to return all permutations of the valid tokens

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文