简体   繁体   English

字母列表的组合

[英]Combination for a list of lists of letters

What is the fastest way to get a list of the combinations of a list of lists of letters?获取字母列表组合列表的最快方法是什么? I understand that if the list is full of numbers, I can just do this:我知道如果列表中充满了数字,我可以这样做:

randomList = [[1,2,3],[4,5,6],[7,8,9]]
for el in itertools.product(*randomList):
        print(el)

This would print [1,4,7],[1,4,8]...[3,6,8],[3,6,9] But when I try this with letters, the function just doesn't work.这将打印[1,4,7],[1,4,8]...[3,6,8],[3,6,9]但是当我用字母尝试这个时,function 只是没有工作。 Is there anyway for this to work with letters, or is there another approach to this.无论如何,这可以与字母一起使用,还是有另一种方法。 I would prefer not to have a function defined.我不希望定义 function。 In short, need something that gives all combinations of randomList = [[A,B,C],[D,E,F],[G,H,I]]简而言之,需要一些能给出randomList = [[A,B,C],[D,E,F],[G,H,I]]的所有组合的东西

[EDIT]-Thanks to the people who answered, I didn't realize that it was registering as variables instead of strings [编辑]-感谢回答的人,我没有意识到它注册为变量而不是字符串

It wont work maybe because the randomList you have defined doesnt have alphabets but variables listed as A, B, ...它可能不起作用,因为您定义的 randomList 没有字母但变量列为 A,B,...

randomList = [['A','B','C'],['D','E','F'],['G','H','I']]

Try this.尝试这个。

It works, I think you have not given the single quotes for the letters.它有效,我认为您没有给出字母的单引号。

import itertools

randomList = [[1,2,3],[4,5,6],[7,8,9]]
randomList = [['A','B','C'],['D','E','F'],['G','H','I']]
for el in itertools.product(*randomList):
        print(el)

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

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