简体   繁体   English

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

[英]How to print all possibilities of nested lists in python?

Here is my list: 这是我的清单:

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

Or 要么

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

I'm trying to print all the combinations: 我正在尝试打印所有组合:

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

Should I use recursive function? 我应该使用递归函数吗? I tried with no result. 我试了没有结果。

Itertools (permutations, product, combinations) doesn't help me. Itertools(排列,产品,组合)对我没有帮助。

Could you help me? 你可以帮帮我吗?

I think itertools.product() actually is what you are looking for: 我认为itertools.product()实际上就是你要找的东西:

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

prints 版画

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' does help: '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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM