在python中从子序列获取所有可能的字符串

在python中从子序列获取所有可能的字符串,python,Python,我有这样一个代码: inp = [['6', '0', '5', '9', '8'], ['='], ['9', '0', '5', '8', '6']] 我想要这个结果: outp = ['6=9','0=9','5=9' ... '8=8', '8=6'] inp的大小可以不同您可以使用itertools.product: 输出变为: 您可以使用itertools.product: 输出变为: 您希望将一个列表中的每个项目与另一个列表中的每个项目相匹配。这是笛卡尔积。它是在美国实施的 您

我有这样一个代码:

inp = [['6', '0', '5', '9', '8'], ['='], ['9', '0', '5', '8', '6']]
我想要这个结果:

outp = ['6=9','0=9','5=9' ... '8=8', '8=6']
inp的大小可以不同

您可以使用itertools.product:

输出变为:

您可以使用itertools.product:

输出变为:


您希望将一个列表中的每个项目与另一个列表中的每个项目相匹配。这是笛卡尔积。它是在美国实施的

您可以这样做:

for left, operator, right in product(*inp):
    print ''.join(left, operator, right)

您希望将一个列表中的每个项目与另一个列表中的每个项目相匹配。这是笛卡尔积。它是在美国实施的

您可以这样做:

for left, operator, right in product(*inp):
    print ''.join(left, operator, right)

上述问题的简单而完整的解决方案是修复列表常量的每一项,并更改其他两个列表的项

inp = [['6', '0', '5', '9', '8'], ['='], ['9', '0', '5', '8', '6']]
outp = []
for right in inp[2]:
    for oper in inp[1]:
        for left in inp[0]:
            temp = str(left) + str(oper) + str(right)
            outp.append(temp)
print(outp)
上述程序的输出:

['6=9', '0=9', '5=9', '9=9', '8=9', '6=0', '0=0', '5=0', '9=0', '8=0', '6=5', '0=5', '5=5', '9=5', '8=5', '6=8', '0=8', '5=8', '9=8', '8=8', '6=6', '0=6', '5=6', '9=6', '8=6']

上述问题的简单而完整的解决方案是修复列表常量的每一项,并更改其他两个列表的项

inp = [['6', '0', '5', '9', '8'], ['='], ['9', '0', '5', '8', '6']]
outp = []
for right in inp[2]:
    for oper in inp[1]:
        for left in inp[0]:
            temp = str(left) + str(oper) + str(right)
            outp.append(temp)
print(outp)
上述程序的输出:

['6=9', '0=9', '5=9', '9=9', '8=9', '6=0', '0=0', '5=0', '9=0', '8=0', '6=5', '0=5', '5=5', '9=5', '8=5', '6=8', '0=8', '5=8', '9=8', '8=8', '6=6', '0=6', '5=6', '9=6', '8=6']

可能重复的您可能会发现此线程很有用,但您需要它。加入您的结果以从元组中获取STR可能重复的您可能会发现此线程很有用,但您需要它。加入您的结果以从元组中获取STR请不要将代码作为答案,解释你的代码是做什么的,以及它是如何解决问题的。请不要只是把代码当作答案,解释你的代码是做什么的,以及它是如何解决问题的。
from itertools import product
result =[''.join((left, operator, right)) for left,operator,right in product(*inp)]