permutations of two lists in python

后端 未结 5 1044
甜味超标
甜味超标 2020-12-02 12:57

I have two lists like:

list1 = [\'square\',\'circle\',\'triangle\']
list2 = [\'red\',\'green\']

How can I create all permutations of these

5条回答
  •  忘掉有多难
    2020-12-02 14:00

    How about

    [x + y for x in list1 for y in list2] + [y + x for x in list1 for y in list2]
    

    Example IPython interaction:

    In [3]: list1 = ['square', 'circle', 'triangle']
    
    In [4]: list2 = ['red', 'green']
    
    In [5]: [x + y for x in list1 for y in list2] + [y + x for x in list1 for y in list2]
    Out[5]: 
    ['squarered',
     'squaregreen',
     'circlered',
     'circlegreen',
     'trianglered',
     'trianglegreen',
     'redsquare',
     'greensquare',
     'redcircle',
     'greencircle',
     'redtriangle',
     'greentriangle']
    

提交回复
热议问题