Foros Python

Versión completa: Cómo saber si una lista tiene elementos repetidos
Actualmente estas viendo una versión simplificada de nuestro contenido. Ver la versión completa con el formato correcto.
Tengo este código

from itertools import product

frase = {'1' : [' w', ' x', ' y', ' z'], '2' : [' w', ' x', ' y', ' z'], '3' : [' w', ' x', ' y', ' z']}

for c in product(*[frase[x] for x in sorted(frase.keys())]):
    print(''.join( c))



y este es el resultado



w w w
w w x
w w y
w w z
w x w
w x x
w x y
w x z
w y w
w y x
w y y
w y z
w z w
w z x
w z y
w z z
x w w
x w x
x w y
x w z
x x w
x x x
x x y
x x z
x y w
x y x
x y y
x y z
x z w
x z x
x z y
x z z
y w w
y w x
y w y
y w z
y x w
y x x
y x y
y x z
y y w
y y x
y y y
y y z
y z w
y z x
y z y
y z z
z w w
z w x
z w y
z w z
z x w
z x x
z x y
z x z
z y w
z y x
z y y
z y z
z z w
z z x
z z y
z z z




Quiero saber como puedo hacer para que solo imprima las que no tienen letras repetidas, si alguien me puede ayudar se lo agradecería.
Hola, bienvenido.

Lo podrías hacer así:

Código:
from itertools import product
import collections

frase = {'1' : [' w', ' x', ' y', ' z'], '2' : [' w', ' x', ' y', ' z'], '3' : [' w', ' x', ' y', ' z']}

for c in product(*[frase[x] for x in sorted(frase.keys())]):
    if not any(filter(lambda x: x > 1, collections.Counter( c).values())):
        print(''.join( c))

Sobre la función filter(): https://micro.recursospython.com/recurso...ilter.html.
Sobre la función any(): https://micro.recursospython.com/recurso...n-any.html.

Saludos
(07-11-2021, 05:42 PM)Francisco escribió: [ -> ]Hola, bienvenido.

Lo podrías hacer así:

Código:
from itertools import product
import collections

frase = {'1' : [' w', ' x', ' y', ' z'], '2' : [' w', ' x', ' y', ' z'], '3' : [' w', ' x', ' y', ' z']}

for c in product(*[frase[x] for x in sorted(frase.keys())]):
    if not any(filter(lambda x: x > 1, collections.Counter( c).values())):
        print(''.join( c))

Sobre la función filter(): https://micro.recursospython.com/recurso...ilter.html.
Sobre la función any(): https://micro.recursospython.com/recurso...n-any.html.

Saludos

 

Buenísimo, muchísimas gracias me sirvió tu solución ahora me fijo en los enlaces que pasaste como se aplica todo. En caso de que tenga a alguna duda sobre alguna cosa te puedo escribir directamente a ti? Si no es mucha molestia claro.
(07-11-2021, 05:42 PM)Francisco escribió: [ -> ]Hola, bienvenido.

Lo podrías hacer así:

Código:
from itertools import product
import collections

frase = {'1' : [' w', ' x', ' y', ' z'], '2' : [' w', ' x', ' y', ' z'], '3' : [' w', ' x', ' y', ' z']}

for c in product(*[frase[x] for x in sorted(frase.keys())]):
    if not any(filter(lambda x: x > 1, collections.Counter( c).values())):
        print(''.join( c))

Sobre la función filter(): https://micro.recursospython.com/recurso...ilter.html.
Sobre la función any(): https://micro.recursospython.com/recurso...n-any.html.

Saludos




Ahora estoy tratando de hacerlo asi: 

from itertools import product
import collections

frase = {
    '1' : ['o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'],
    '2' : [' o', ' p', ' q', ' r', ' s', ' t', ' u', ' v', ' w', ' x', ' y', ' z'],
    '3' : [' o', ' p', ' q', ' r', ' s', ' t', ' u', ' v', ' w', ' x', ' y', ' z'],
    '4' : [' o', ' p', ' q', ' r', ' s', ' t', ' u', ' v', ' w', ' x', ' y', ' z'],
    '5' : [' o', ' p', ' q', ' r', ' s', ' t', ' u', ' v', ' w', ' x', ' y', ' z'],
    '6' : [' o', ' p', ' q', ' r', ' s', ' t', ' u', ' v', ' w', ' x', ' y', ' z'],
    '7' : [' o', ' p', ' q', ' r', ' s', ' t', ' u', ' v', ' w', ' x', ' y', ' z'],
    '8' : [' o', ' p', ' q', ' r', ' s', ' t', ' u', ' v', ' w', ' x', ' y', ' z'],
    '9' : [' o', ' p', ' q', ' r', ' s', ' t', ' u', ' v', ' w', ' x', ' y', ' z'],
    '10' : [' o
', ' p
', ' q
', ' r
', ' s
', ' t
', ' u
', ' v
', ' w
', ' x
', ' y
', ' z
'],
}

for c in product(*[frase[x] for x in sorted(frase.keys())]):
    if not any(filter(lambda x: x > 1, collections.Counter( c).values())):
        print(''.join(c))



 pero se queda cargando y no arranca, sabras porque es?
¡De nada! En mi caso tarda unos segundos, pero luego empieza a imprimir. Depende de la velocidad de cada computadora y cuán rápido se pueda ejecutar el product().

Saludos