递归/迭代麻烦,将单词数组转换为短语
我正在尝试使用单词列表数组构建“短语”列表。 我有一个看起来像这样的数组:
[ [ 'big', 'small', 'wild' ],
[ 'brown', 'black', 'spotted' ],
[ 'cat', 'dog' ] ]
第一个数组是生成的“短语”中的第一个单词,第二个数组是第二个单词,依此类推。 单词列表的数量是可变的,可以是两个单词列表或五个列表。 我在将该数组转换为如下所示的数组时遇到问题:
[ [ 'big', 'brown', 'cat' ],
[ 'big', 'brown', 'dog' ],
...
[ 'wild', 'spotted', 'dog'] ]
生成的数组的顺序并不重要,但如果原始数组具有三个单词列表,则生成的嵌套数组应该是三个单词长。
我是用 Javascript 编写的,但您可以随意使用您喜欢的任何语言,因为递归概念应该基本相同。
I'm trying to build a list of "phrases" using an array of word lists. I have an array that looks something like this:
[ [ 'big', 'small', 'wild' ],
[ 'brown', 'black', 'spotted' ],
[ 'cat', 'dog' ] ]
The first array is the first word in the resulting "phrase", the second array is the second word, and so on. The number of word lists is variable, it can be two lists of words or five lists. I'm having trouble converting that array to something that looks like this:
[ [ 'big', 'brown', 'cat' ],
[ 'big', 'brown', 'dog' ],
...
[ 'wild', 'spotted', 'dog'] ]
The order of the resulting array doesn't matter, but if the original array has three word lists, then the resulting nested arrays should be three words long.
I'm writing this in Javascript, but feel free to use whatever language you prefer as the recursion concept should be basically the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用 C# 实现怎么样? 不幸的是,列表操作有点隐藏了算法。
}
How about an implementation in C#? Unfortunately, the list manipulation hides the algorithm a bit.
}
在 F# 中,它会
打印以下输出:
In F# it is just this
which prints the following output: