如何在python中打印所有可能的嵌套列表?

如何在python中打印所有可能的嵌套列表?,python,list,recursion,Python,List,Recursion,以下是我的清单: pos = [['det'], ['noun', 'adj'], ['noun'], ['vb'], ['det'], ['vb', 'noun', 'adj']] 或 我正在尝试打印所有的组合: det noun noun vb det vb det adj noun vb det vb det noun noun vb det noun det adj noun vb det noun det noun noun vb det adj det adj noun vb de

以下是我的清单:

pos = [['det'], ['noun', 'adj'], ['noun'], ['vb'], ['det'], ['vb', 'noun', 'adj']]

我正在尝试打印所有的组合:

det noun noun vb det vb
det adj noun vb det vb
det noun noun vb det noun
det adj noun vb det noun
det noun noun vb det adj
det adj noun vb det adj
我应该使用递归函数吗?我试过了,但没有结果

Itertools(排列、产品、组合)对我没有帮助


你能帮我吗?

我想
itertools.product()
实际上就是你想要的:

pos = [['det'], ['noun', 'adj'], ['noun'],
       ['vb'], ['det'], ['vb', 'noun', 'adj']]
for x in itertools.product(*pos):
    print " ".join(x)
印刷品

det noun noun vb det vb
det noun noun vb det noun
det noun noun vb det adj
det adj noun vb det vb
det adj noun vb det noun
det adj noun vb det adj
“itertools”确实有助于:

for i in itertools.product(*pos): print i
('det', 'noun', 'noun', 'vb', 'det', 'vb')
('det', 'noun', 'noun', 'vb', 'det', 'noun')
('det', 'noun', 'noun', 'vb', 'det', 'adj')
('det', 'adj', 'noun', 'vb', 'det', 'vb')
('det', 'adj', 'noun', 'vb', 'det', 'noun')
('det', 'adj', 'noun', 'vb', 'det', 'adj')

你说得对,它确实有效!我不知道为什么我在尝试时没有得到这个结果。无论如何,非常感谢!我有一个问题,当有两位数的字符串数字时,它不起作用。它也会分割数字,这是我不想要的。你知道如何解决这种行为吗?@eljobso从你的评论中,我无法理解你到底在做什么,问题是什么。我建议问一个新问题来描述你的问题。将代码添加到问题中,解释预期输出并显示实际输出。
for i in itertools.product(*pos): print i
('det', 'noun', 'noun', 'vb', 'det', 'vb')
('det', 'noun', 'noun', 'vb', 'det', 'noun')
('det', 'noun', 'noun', 'vb', 'det', 'adj')
('det', 'adj', 'noun', 'vb', 'det', 'vb')
('det', 'adj', 'noun', 'vb', 'det', 'noun')
('det', 'adj', 'noun', 'vb', 'det', 'adj')