我有一个这样的两级列表:
[['Demonstrative-Pronoun', 'Personal-Pronoun', 'Adjective', 'Noun', 'Noun', 'Indefinite-Pronouns'], ['Verb', 'Adjective', 'Adverb', 'Noun', 'Noun'], ['Verb', 'Verb', 'Verb', 'Verb', 'Adjective', 'Noun', 'Noun', 'Verb'], ['Verb', 'Verb', 'Verb', 'Verb', 'Verb', 'Verb', 'Noun']]
Run Code Online (Sandbox Code Playgroud)
我想从这些列表中形成所有可能的字符串,例如:
有没有更快的方法在python中执行此操作或任何快速方法?此列表是硬编码的,但实际上可能包含任意数量的元素和子元素
谢谢!
使用itertools.product基本上做了一堆嵌套的for循环:
import itertools
items = [...]
for grouping in itertools.product(*items):
print ' '.join(grouping)
Run Code Online (Sandbox Code Playgroud)
这将为您提供所有1680种组合.
来自评论:"你可以用地图(集合,项目)替换项目,它将删除重复项.这将计数减少到120."