Single lists with at least 2 difference elements

Asked

Viewed 216 times

2

Considering the numbers:

01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25

How do I generate lists of 15 items (numbers cited above), and they need to have at least 2 items of difference between them. For example:

01,02,03,04,05,06,07,08,09,10,11,12,13,14,15
01,02,03,04,05,06,07,08,09,10,11,12,13,17,18
01,02,03,04,05,06,07,08,09,10,11,12,13,20,22
01,02,03,04,05,06,07,08,09,10,11,12,13,21,25

Notice at the end, I changed only the last 2 items of each list, they are unique, do not exist in the other lists. How to do this in practice, where the 2 difference items can be in any position?

1 answer

0


Update 1

Another way to get to the result is by removing the elements already added in the lists:

import random

def gera_lista(qnt):
  listas = []
  numeros = random.sample(range(1, 100), 25)
  # Sorteia 13 elementos da lista `numeros`
  primeiros = random.sample(numeros, 13)
  # Pega a diferença entre a lista `numeros` e a lista `primeiros`
  # e define como novo valor da lista `numeros`
  numeros = list(set(numeros).difference(set(primeiros)))
  for _ in range(0, qnt):
    # Se o numero de elementos na lista: `numeros` for
    # igual a zero para o laço `for`
    if len(numeros) == 0: break
    # Sorteia os dois ultimos numeros e
    # depois cria uma nova lista com a junção da
    # lista: `numeros` com a lista: `sorteados`
    sorteados = random.sample(numeros, 2)
    lista = primeiros + sorteados
    # Adiciona a nova lista na lista: `listas`
    listas.append(lista)
    # Remove os numeros sorteados da lista: `numeros`
    [numeros.remove(x) for x in sorteados if x in numeros]
  return listas

print(gera_lista(20))

See working on repl.it and also I created a Gist in Githubgist


Create a function and use random.sample to draw two numbers and chain.from_iterable to check whether these two items are contained in the list.

def diferente(lista):
    # Sortea dois números
    a, b = random.sample(numeros, 2)
    # Verifica se 'a' e 'b' não esta na lista
    # se estiver, chama a função novamente, caso contrário
    # adiciona
    [lista.extend([a,b]) if (a not in chain.from_iterable(listas)) and (b not in chain.from_iterable(listas)) else diferente(lista)]

declare an empty list:

nova_lista = []

Draw the first 13 numbers and store in a variable:

# Sorteia os 13 primeiros elementos
primeiros = random.sample(numeros, 13)

then inside the for, add the first elements in the new list, call the function to add the other two and a reset in the list:

# Adiciona os primeiros elementos na nova lista
nova_lista.extend(primeiros)
# Adiciona os outros dois elementos
diferente(nova_lista)
listas.append(nova_lista)
# Reseta a lista
nova_lista = []

See the full code:

import random
from itertools import chain

numeros = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]
listas = []

def gera_lista(qnt):
    nova_lista = []
    # Sorteia os 13 primeiros elementos
    primeiros = random.sample(numeros, 13)
    for i in range(0, qnt):
        # Adiciona os primeiros elementos na nova lista
        nova_lista.extend(primeiros)
        # Adiciona os outros dois elementos
        diferente(nova_lista)
        listas.append(nova_lista)
        nova_lista = []

def diferente(lista):
    # Sortea dois números
    a, b = random.sample(numeros, 2)
    # Verifica se 'a' e 'b' não esta na lista
    # se estiver, chama a função novamente, caso contrário
    # adiciona
    [lista.extend([a,b]) if (a not in chain.from_iterable(listas)) and (b not in chain.from_iterable(listas)) else diferente(lista)]

gera_lista(5)
print(listas)

See working on repl.it and also I created a Gist in Githubgist

References

  • Interesting! Thanks for your help. But how do I generate the numbers so that any number is added between the 25 and not just the 13 first ones? Example: 02,04,05,06,09,10,11,12,13,15,17,18,19,22,25

  • I did it but there generates only 1 list. I don’t know well, but I think I’d have to go storing that item, how many times it came out and on what particular list. But there I don’t know how to do it.

  • Look at it now

  • Excellent! Now it’s perfect. I just wanted to do it to generate all possible combinations, without limiting the amount of results (lists). I tried to put a big number or even adapt the for, but gives error RecursionError: maximum recursion depth exceeded in comparison

Browser other questions tagged

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