Ejemplo 1: combinaciones de cadenas de impresión en Python
test_str = "abc"
res = [test_str[i: j] for i in range(len(test_str))
for j in range(i + 1, len(test_str) + 1)]
print(res)#['a', 'ab', 'abc', 'b', 'bc', 'c']
Ejemplo 2: como obtener todas las combinaciones posibles en Python
all_combinations = [list(zip(each_permutation, list2)) for each_permutation in itertools.permutations(list1, len(list2))]
Ejemplo 3: combinaciones de Python
def permutations(iterable, r=None):
# permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
# permutations(range(3)) --> 012 021 102 120 201 210
pool = tuple(iterable)
n = len(pool)
r = n if r is None else r
if r > n:
return
indices = list(range(n))
cycles = range(n, n-r, -1)
yield tuple(pool[i] for i in indices[:r])
while n:
for i in reversed(range(r)):
cycles[i] -= 1
if cycles[i] == 0:
indices[i:] = indices[i+1:] + indices[i:i+1]
cycles[i] = n - i
else:
j = cycles[i]
indices[i], indices[-j] = indices[-j], indices[i]
yield tuple(pool[i] for i in indices[:r])
break
else:
return
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)