Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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
如何在python中连接多个数组_Python_Arrays - Fatal编程技术网

如何在python中连接多个数组

如何在python中连接多个数组,python,arrays,Python,Arrays,我有4个这样的阵列: temp1 = ['a' , 'b' , 'c'] temp2 = ['d' , 'e' ,'' ] temp3 = ['f'] temp4 = ['g'] 我想要输出: adfg aefg afg bdfg befg bfg cdfg cefg cfg 我用以下方法解决了这个问题: temp1 = ['a' , 'b' , 'c'] temp2 = ['d' , 'e' ,'' ] temp3 = ['f'] temp4 = ['g'] for list_1 in t

我有4个这样的阵列:

temp1 = ['a' , 'b' , 'c']
temp2 = ['d' , 'e' ,'' ]
temp3 = ['f']
temp4 = ['g']
我想要输出:

adfg
aefg
afg
bdfg
befg
bfg
cdfg
cefg
cfg
我用以下方法解决了这个问题:

temp1 = ['a' , 'b' , 'c']
temp2 = ['d' , 'e' ,'' ]
temp3 = ['f']
temp4 = ['g']
for list_1 in temp1:
    for list_2 in temp2:
        for list_3 in temp3:
            for list_4 in temp4:
                temp_list = ''
                if list_1: temp_list += list_1
                if list_2: temp_list += list_2
                if list_3: temp_list += list_3
                if list_4: temp_list += list_4
                print "%s " %(temp_list)
但我认为我的代码效率不高

如何制作好算法并使其高效

如果temp3为空,例如:

temp1 = ['a' , 'b' , 'c']
temp2 = ['d' , 'e' ,'' ]
temp3 = []
temp4 = ['g']

使用Python理解。它们更干净、更优雅

temp1 = ['a' , 'b' , 'c']
temp2 = ['d' , 'e' ,'' ]
temp3 = ['f']
temp4 = ['g']

templist = [i+j+k+l for i in temp1 for j in temp2 for k in temp3 for l in temp4] 
# Result - ['adfg', 'aefg', 'afg', 'bdfg', 'befg', 'bfg', 'cdfg', 'cefg', 'cfg']
i+j+k+l有助于连接字符串。每当你想使用列表中的项目时,试着使用理解


您可以使用Python理解来检查这一点。它们更干净、更优雅

temp1 = ['a' , 'b' , 'c']
temp2 = ['d' , 'e' ,'' ]
temp3 = ['f']
temp4 = ['g']

templist = [i+j+k+l for i in temp1 for j in temp2 for k in temp3 for l in temp4] 
# Result - ['adfg', 'aefg', 'afg', 'bdfg', 'befg', 'bfg', 'cdfg', 'cefg', 'cfg']
i+j+k+l有助于连接字符串。每当你想使用列表中的项目时,试着使用理解

您可以查看此信息

您可以使用:


更新:

如果更新的问题中的
temp3
为空,我想您应该在生成结果时跳过它。如果是这种情况,则只能使用包含以下项目的列表:

>>> input_lists = [arr for arr in (temp1, temp2, temp3, temp4) if arr]
>>> result = product(*input_lists)
>>> ["".join(item) for item in result]
['adg', 'aeg', 'ag', 'bdg', 'beg', 'bg', 'cdg', 'ceg', 'cg']
您可以使用:


更新:

如果更新的问题中的
temp3
为空,我想您应该在生成结果时跳过它。如果是这种情况,则只能使用包含以下项目的列表:

>>> input_lists = [arr for arr in (temp1, temp2, temp3, temp4) if arr]
>>> result = product(*input_lists)
>>> ["".join(item) for item in result]
['adg', 'aeg', 'ag', 'bdg', 'beg', 'bg', 'cdg', 'ceg', 'cg']
使用

使用


如果数组为空呢???temp1=['a','b','c']temp2=['d','e','']temp3=[]temp4=['g']输出将为[]是,它将返回
[]
,这是此函数的预期行为。如果存在空数组,您希望输出是什么?如果是空的
temp3
,我更新了答案。是的,我认为必须使用“if”进行筛选,但谢谢您如果
temp3
为空,您接受的答案将导致
[]
结果。如果数组为空,情况如何???temp1=['a','b','c']temp2=['d','e','']temp3=[]temp4=['g']输出将为[]是,它将返回
[]
,这是此函数的预期行为。如果存在空数组,您希望输出是什么?如果是空的
temp3
,我更新了答案。是的,我认为它必须使用“if”进行过滤,但谢谢您如果
temp3
为空,您接受的答案将导致
[]