“Função de combinações de python” Respostas de código

Python combinado

# 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
BreadCode

Permutação de Python

import itertools

a = [1, 2, 3]
n = 3

perm_iterator = itertools.permutations(a, n)

for item in perm_iterator:
    print(item)
Kodi4444

Como obter todas as combinações possíveis no Python

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

Função de combinações de python

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)
Homely Hamster

Respostas semelhantes a “Função de combinações de python”

Perguntas semelhantes a “Função de combinações de python”

Mais respostas relacionadas para “Função de combinações de python” em Python

Procure respostas de código populares por idioma

Procurar outros idiomas de código