How to generate all combination from values in dict of lists in Python

后端 未结 5 1481
野的像风
野的像风 2020-12-09 03:22

I would like to generate all combinations of values which are in lists indexed in a dict:

{\'A\':[\'D\',\'E\'],\'B\':[\'F\',\'G\',\'H\'],\'C\':[\'I\',\'J\']}         


        
5条回答
  •  天涯浪人
    2020-12-09 04:07

    from itertools import combinations
    
    a=['I1','I2','I3','I4','I5']
    
    list(combinations(a,2))
    

    The output will be:

    [('I1', 'I2'),
     ('I1', 'I3'),
     ('I1', 'I4'),
     ('I1', 'I5'),
     ('I2', 'I3'),
     ('I2', 'I4'),
     ('I2', 'I5'),
     ('I3', 'I4'),
     ('I3', 'I5'),
     ('I4', 'I5')]
    

提交回复
热议问题