Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 从列表顺序生成链_Algorithm_Combinations - Fatal编程技术网

Algorithm 从列表顺序生成链

Algorithm 从列表顺序生成链,algorithm,combinations,Algorithm,Combinations,我正在寻找如何完成一些我已经掌握的事情,但没有: 我有n个不同大小的列表: {A, B, C, D} {1,2} {X, Y, Z} ...to the nth potentially 我如何从每个级别A1X、A1Y、A1Z等生成所有可能的一个项目链。这是一项算法和数学任务,不,这不是家庭作业(我知道学校正在开始),这是我正在做的事情的一部分,我没有代码——我只需要指出正确的方向来制定我的术语。(您没有要求提供代码,但我倾向于使用Python编写可

我正在寻找如何完成一些我已经掌握的事情,但没有:

我有n个不同大小的列表:

{A, B, C, D}
    {1,2}
       {X, Y, Z}
            ...to the nth potentially
我如何从每个级别A1X、A1Y、A1Z等生成所有可能的一个项目链。这是一项算法和数学任务,不,这不是家庭作业(我知道学校正在开始),这是我正在做的事情的一部分,我没有代码——我只需要指出正确的方向来制定我的术语。

(您没有要求提供代码,但我倾向于使用Python编写可执行的伪代码。您仍然需要将核心算法转换为您选择的语言)

实际上,您所说的是形成列表的数量。可以通过多种方式完成。如果列表的数量事先未知,则递归方法是最自然的

L1*L2*…*Ln
表示以下形式的所有字符串的列表
s1+s2+…+sn
其中
Li
中的
si
+
是串联运算符。作为基础,您可以采用
n==1
、单个列表或
n==0
,根本没有列表。在许多方面,后一种方法更为优雅,在这种情况下,定义空字符串列表的乘积来表示是唯一元素为空字符串的列表

然后:

如果
n==0

否则返回

[a+b | a ranges over L1 and b ranges over (L2 * L3 * ... * Ln)]
其中,
(L2*L3*…*Ln)
是递归计算的(如果n为1,则仅为空字符串)

最后一个列表可以很容易地建立在嵌套循环中,或者用任何支持
列表理解的语言更直接地表达

下面是一个Python实现,它返回给定字符串列表(代码中缩写为
lls
)的所有产品的列表:

这样测试:

lists_of_strings = [['A','B','C','D'],['1','2','3'],['X','Y','Z']]
print(product(lists_of_strings))
输出:

['A1X', 'A1Y', 'A1Z', 'A2X', 'A2Y', 'A2Z', 'A3X', 'A3Y', 'A3Z', 'B1X', 'B1Y', 'B1Z', 'B2X', 'B2Y', 'B2Z', 'B3X', 'B3Y', 'B3Z', 'C1X', 'C1Y', 'C1Z', 'C2X', 'C2Y', 'C2Z', 'C3X', 'C3Y', 'C3Z', 'D1X', 'D1Y', 'D1Z', 'D2X', 'D2Y', 'D2Z', 'D3X', 'D3Y', 'D3Z']
在Python中,这样做没有太多动机,因为
itertools
模块有一个很好的产品,同样的产品可以表示为:

[''.join(p) for p in itertools.product(*lists_of_strings)] 
1) 嵌套循环2)循环中的循环3)递归
[''.join(p) for p in itertools.product(*lists_of_strings)]