Jan-29-2021, 05:32 AM
(This post was last modified: Jan-29-2021, 05:37 AM by deanhystad.)
This is my best guess at what you are trying to accomplish. I left out the file write, but it would be very similar to the print.
import random import itertools def add_unique(xlist, ylists): """Append xlist to ylists only if xlist[i] is not in ylist[i] for every value if i. """ for y, x in zip(ylists, xlist): if x in y: break; # Not unique else: # Is unique. Only get here if didn't break out of for loop. for ylist, x in zip(ylists, xlist): ylist.append(x) xlist_values = [str(n+1) for n in range(9)] ylists = [[] for _ in range(9)] times = 0 while len(ylists[0]) < 9: xlist = random.sample(xlist_values, k=9) add_unique(xlist, ylists) times += 1 print(f"FINISH {times} TIMES") for index in range(9): print(', '.join([ylist[index] for ylist in ylists]))I am not sure what the code is supposed to do. Since it makes a 9x9 matrix of numbers in the range 1..9 it looks suspiciously like a Sudoku maker. If so it does not make valid Sudoku puzzles. Values are unique for each row and each column, but not for each 3x3 box.