Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python all possible combinations of multiple lists

>>> import itertools
>>> a = [[1,2,3],[4,5,6],[7,8,9,10]]
>>> list(itertools.product(*a))
[(1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 4, 10), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 5, 10), (1, 6, 7), (1, 6, 8), (1, 6, 9), (1, 6, 10), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 4, 10), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 5, 10), (2, 6, 7), (2, 6, 8), (2, 6, 9), (2, 6, 10), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 4, 10), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 5, 10), (3, 6, 7), (3, 6, 8), (3, 6, 9), (3, 6, 10)]
Comment

combination python

# 1. Print all combinations 
from itertools import combinations

comb = combinations([1, 1, 3], 2)
print(list(combinations([1, 2, 3], 2)))
# Output: [(1, 2), (1, 3), (2, 3)]

# 2. Counting combinations
from math import comb
print(comb(10,3))
#Output: 120
Comment

get all combinations from two lists python

a = ["foo", "melon"]
b = [True, False]
c = list(itertools.product(a, b))
>> [("foo", True), ("foo", False), ("melon", True), ("melon", False)]
Comment

how to find a combination of all elements in a python list

import itertools

stuff = [1, 2, 3]
for L in range(0, len(stuff)+1):
    for subset in itertools.combinations(stuff, L):
        print(subset)
Comment

python get all combinations of list

itertools.combinations(iterable, r)
Comment

how to get all possible combinations in python

all_combinations = [list(zip(each_permutation, list2)) for each_permutation in itertools.permutations(list1, len(list2))]
Comment

python possible combinations

import math
n=7
k=5
print(math.comb(n, k))
Comment

all possible combinations in python

import itertools

list1 = list(range(5, 10))
list2 = [1, 2, 3]
list = [list1, list2]

combination = [p for p in itertools.product(*list)]
print(combination)
PythonCopy
Comment

python combinations

import itertools
def subs(l):
    res = []
    for i in range(1, len(l) + 1):
        for combo in itertools.combinations(l, i):
            res.append(list(combo))
    return res
Comment

python compute all combinations

import itertools

stuff = [1, 2, 3]
for L in range(len(stuff) + 1):
    for subset in itertools.combinations(stuff, L):
        print(subset)
Comment

python combinations function

def combinations(iterable, r):
    pool = tuple(iterable)
    n = len(pool)
    for indices in permutations(range(n), r):
        if sorted(indices) == list(indices):
            yield tuple(pool[i] for i in indices)
Comment

PREVIOUS NEXT
Code Example
Python :: how to loop over list 
Python :: import file in parent directory python 
Python :: python while loop 
Python :: pyqt5 button connect 
Python :: dt.weekday_name 
Python :: generate dates between two dates python 
Python :: capwords python 
Python :: ignore pytest errors or warnings 
Python :: how to remove duplicates from a python list 
Python :: print multiple lines python 
Python :: python face recognition 
Python :: checking if a string contains a substring python 
Python :: for loop with enumerate python 
Python :: how to append items to a list in python 
Python :: How to efficiently calculate the nth Catalan number, in Python? 
Python :: python print trailing zeros 
Python :: matplotlib vertical line 
Python :: how to label points in scatter plot in python 
Python :: fasttext python 
Python :: convert np shape (a,) to (a,1) 
Python :: python cocktail sort 
Python :: python3 shebang line 
Python :: Making a txt file then write 
Python :: python merge nested dictionaries 
Python :: np append row 
Python :: audioplayer python 
Python :: how to remove none in python 
Python :: how to install tkinter in pycharm 
Python :: How to select parts of a numpy array 
Python :: auto slug field django 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =