Tcl 组合多个列表的元素
我想要获取任意数量的列表,并返回元素组合列表,但只从每个列表中组合一个元素。我只有sudo代码,因为我不知道从哪里开始 我确实找到了这个程序的解决方案,但我不理解scala代码。我正在用Tcl编写我的程序,但如果您能帮助我自由地用java、python或伪代码之类的语言编写答案。谁能帮我把下面的伪代码变成现实 例如:Tcl 组合多个列表的元素,tcl,combinations,Tcl,Combinations,我想要获取任意数量的列表,并返回元素组合列表,但只从每个列表中组合一个元素。我只有sudo代码,因为我不知道从哪里开始 我确实找到了这个程序的解决方案,但我不理解scala代码。我正在用Tcl编写我的程序,但如果您能帮助我自由地用java、python或伪代码之类的语言编写答案。谁能帮我把下面的伪代码变成现实 例如: # example: {a b} {c} {d e} # returns: {a c d} {a c e} {b c d} {b c e} # how? # exa
# example: {a b} {c} {d e}
# returns: {a c d} {a c e} {b c d} {b c e}
# how?
# example: {a b} {c} {d e}
# iters: 0 0 0
# 0 0 1
# 1 0 0
# 1 0 1
#
#
# set done false
#
# while {!done} {
#
# list append combination_of_list due to iteration counts
#
# foreach list $lists {
# increment the correct count (specifically {0->1} {0->0} {0->1}) }
# reset the approapraite counts to 0
# }
#
# if all the counts in all the lists are at or above their max {
# set done true
# }
# }
下面是一个伪代码,描述生成所有组合的算法:
list_of_lists = {{a b}{c}{d e}}
def copy(list):
copy = {}
for element in list:
copy.add(element)
return copy;
def combine(list1, list2):
combinations = {}
for item1 in list1:
for item2 in list2:
combination = copy(item1)
combination.add(item2)
combinations.add(combination)
return combinations
results = {{}}
while list_of_lists.length>0:
results = combine(results, list_of_lists[0])
list_of_lists.remove(0)
它首先将{{}
与{abc}
它生成{{a}{b}}
,它将与{c}
结合,在下一次迭代中生成{a c}{b c}
更新:
Javascript版本:
var list_of_lists = [["a", "b"],["c"],["d", "e"]];
function copy(list) {
var copy = [];
for (element of list) {
copy.push(element);
}
return copy;
}
function combine(list1, list2) {
var combinations = [];
for (let item1 of list1) {
var combination = copy(item1);
for (let item2 of list2){
combination.push(item2);
combinations.push(combination);
}
}
return combinations;
}
results = [[]]
while (list_of_lists.length>0) {
results = combine(results, list_of_lists[0]);
list_of_lists.splice(0,1);
}
console.log(results);
下面是一个伪代码,描述生成所有组合的算法:
list_of_lists = {{a b}{c}{d e}}
def copy(list):
copy = {}
for element in list:
copy.add(element)
return copy;
def combine(list1, list2):
combinations = {}
for item1 in list1:
for item2 in list2:
combination = copy(item1)
combination.add(item2)
combinations.add(combination)
return combinations
results = {{}}
while list_of_lists.length>0:
results = combine(results, list_of_lists[0])
list_of_lists.remove(0)
它首先将{{}
与{abc}
它生成{{a}{b}}
,它将与{c}
结合,在下一次迭代中生成{a c}{b c}
更新:
Javascript版本:
var list_of_lists = [["a", "b"],["c"],["d", "e"]];
function copy(list) {
var copy = [];
for (element of list) {
copy.push(element);
}
return copy;
}
function combine(list1, list2) {
var combinations = [];
for (let item1 of list1) {
var combination = copy(item1);
for (let item2 of list2){
combination.push(item2);
combinations.push(combination);
}
}
return combinations;
}
results = [[]]
while (list_of_lists.length>0) {
results = combine(results, list_of_lists[0]);
list_of_lists.splice(0,1);
}
console.log(results);
本页讨论了各种Tcl解决方案: Donal Fellows在本页末尾介绍了这一变化:
proc product args {
set xs {{}}
foreach ys $args {
set result {}
foreach x $xs {
foreach y $ys {
lappend result [list {*}$x $y]
}
}
set xs $result
}
return $xs
}
这样运行会得到以下结果:
% product {a b} {c} {d e}
{a c d} {a c e} {b c d} {b c e}
文件:
,
,
,
,
,
,
本页讨论了各种Tcl解决方案: Donal Fellows在本页末尾介绍了这一变化:
proc product args {
set xs {{}}
foreach ys $args {
set result {}
foreach x $xs {
foreach y $ys {
lappend result [list {*}$x $y]
}
}
set xs $result
}
return $xs
}
这样运行会得到以下结果:
% product {a b} {c} {d e}
{a c d} {a c e} {b c d} {b c e}
文件:
,
,
,
,
,
,
我希望我正确地实施了这一点;我不确定。我得到的最终输出是不正确的,它是:
{acd}{acd}{abcd}{abcde}
但是我会仔细检查代码,看看我是否正确实现了它;我不确定。我得到的最终结果不正确,它是:{acd}{acd}{abcde}{abcde}
但是我会仔细检查代码,看看我是否正确实现了它。多纳尔·费罗斯是一个伟大的美国英雄。多纳尔·费罗斯是一个伟大的美国英雄。