The problem is that the method append
only gets one argument, but you’re passing two: lpossiveis[i]
and dpossiveis[i]
.
A solution would be to concatenate these values, as suggested in other answers.
But if it’s not an exercise where you need to do everything manually, another option is to use the module itertools
, who has the function product
, that already creates these combinations for you.
The only detail is that it returns the combinations in tuples, so you have to join the elements of each tuple into a single string:
from itertools import product
lpossiveis = ['ABD', 'ACD', 'ADB', 'ADC', 'ADE', 'ADF', 'AED', 'AFD', 'BAD', 'BDA', 'CAD', 'CDA', 'DAB', 'DAC', 'DAE', 'DAF', 'DBA', 'DCA', 'DEA', 'DFA', 'EAD', 'EDA', 'FAD', 'FDA']
dpossiveis = [314, 323, 332, 341, 611]
senhas = [ f'{s}{num}' for s, num in product(lpossiveis, dpossiveis) ]
Or, if you need a function to do that:
from itertools import product
def senhas_possiveis(letras, numeros):
return [ f'{s}{num}' for s, num in product(letras, numeros) ]
lpossiveis = ['ABD', 'ACD', 'ADB', 'ADC', 'ADE', 'ADF', 'AED', 'AFD', 'BAD', 'BDA', 'CAD', 'CDA', 'DAB', 'DAC', 'DAE', 'DAF', 'DBA', 'DCA', 'DEA', 'DFA', 'EAD', 'EDA', 'FAD', 'FDA']
dpossiveis = [314, 323, 332, 341, 611]
senhas = senhas_possiveis(lpossiveis, dpossiveis)
The detail is that the code above creates a list of all possible passwords, and depending on the size of the initial lists, it can get very large.
If you really need a list of all the passwords, then leave it at that. But if you just need to go through the passwords (in a for
, for example) and do something with them (and then you won’t need them for anything else), just use product
directly on for
:
for s, num in product(lpossiveis, dpossiveis):
senha = f'{s}{num}'
# fazer algo com a senha
The difference is that product
returns an iterator that does not compute all passwords at once, but rather one at a time (one at each iteration of for
). If the idea is just to do something with each password and then you won’t need them for anything else, this is more efficient than creating a list with all the possibilities (and this approach becomes more interesting if the number of possible passwords is very large).
But if you need a list with all the passwords, there is no way, you have to create it.
Thank you so much for the explanation! I’m starting in the area and the way you explained made me understand several things!
– Thais Helena Moretto