Generating Combinations in python

后端 未结 3 1698
忘了有多久
忘了有多久 2020-12-29 07:54

I am not sure how to go about this in Python, if its even possible. What I need to do is create an array (or a matrix, or vector?) from 3 separate arrays. Each array as 4

相关标签:
3条回答
  • 2020-12-29 08:18

    The simplest way:

    for i in Class1:
        for j in Class2:
            for k in Class3:
                print (i,j,k)
    
    0 讨论(0)
  • 2020-12-29 08:32

    What you want is called a Cartesian product:

    import itertools
    
    iterables = [ [1,2,3,4], [88,99], ['a','b'] ]
    
    for t in itertools.product(*iterables):
        print t
    
    0 讨论(0)
  • 2020-12-29 08:34

    Check the Python itertools standard module:

    itertools.combinations(iterable, r)

    Return r length subsequences of elements from the input iterable.

    0 讨论(0)
提交回复
热议问题