python - Same values in two lists (pairs) -
so need have list going have 1 list load of values between 1 , 8 randomly generated , list load of values between 1 , 8 randomly also. have managed on code below:
from random import * lista = [] listb = [] inp = int(input('number of values generated')) x in range(0,inp): num = randint(0,8) lista.append(num) if num == 0: numb = randint(1,8) else: numb = randint(0,8) listb.append(numb) print(lista) print(listb)
the value in first list can't 0 , value in seond list can't 0 on same trial. have in code. problem have.
[4, 5, 2, 5, 1]
[1, 2, 3, 2, 4]
in lista, 5 produced twice , 2 below on second list produced twice also. can't figure out solution these out lists, when create pair this. thanks.
you can use helper function below generate unique number not in list , append list.
this should work you:
def generateunique(list, start, end): # helper function generate , return unique number not in list num = randint(start, end) while num in list: # loop keep generating value, until unique in given list num = randint(start, end) return num random import * lista = [] listb = [] inp = int(input('number of values generated')) x in range(0,inp): num = generateunique(lista, 0, 8) lista.append(num) if num == 0: numb = generateunique(listb, 1, 8) else: numb = generateunique(listb, 0, 8) listb.append(numb) print(lista) print(listb)
hope helps!
Comments
Post a Comment