Combination of two lists in Python

Asked

Viewed 713 times

3

I need a code that discovers possible passwords in a universe of LLLDDD (L=letter, d=digit), I found it easier to apply the conditions separately and I arrived at two lists:

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]

Now I need to create a third list between the combination of the two, where the first element of lpossiveis is concatenated with the first element of dpossiveis, the first element of lpossiveis is concatenated with the second element of dpossiveis and so on. The result must be:

senhas: ['ABC314', 'ABC323', 'ABC332', 'ABC341', 'ABC611', 'ACD314', ...  ]

My code is like this:

def senhaspossiveis(lpossiveis, dpossiveis):
    senhas = []
    for i in lpossiveis:
        for i in dpossiveis:
            senhas.append(lpossiveis[i], dpossiveis[i])
    return senhas

But you’re not creating the third list.

3 answers

3


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!

2

Here is a possible solution to your problem:

def senhaspossiveis(lpossiveis, dpossiveis):
    senhas = []
    for letras in lpossiveis:
        for  numeros in dpossiveis:
            combinacaosenha = letras+str(numeros)
            senhas.append(combinacaosenha)
    return senhas

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']
spossiveis = [314, 323, 332, 341, 611]

saida = senhaspossiveis(lpossiveis, spossiveis)

print(saida)

2

Hi there, Thais. I made some changes to your code and I think it worked.

Code:

def senhaspossiveis(lpossiveis, dpossiveis):
    senhas = []
    for l in lpossiveis:
        for d in dpossiveis:
            senhas.append(l + str(d))
    return senhas

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 = senhaspossiveis(lpossiveis, dpossiveis)

Note that the iterations that are occurring in the for are about the list elements, not about the index of the list elements. Hence the concatenation is simple: l + str(d). The str() function transforms dpossible integers into string.

Good morning!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.