Python Forum
about write file wrong (Edit directly online)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
about write file wrong (Edit directly online)
#5
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.
Reply


Messages In This Thread
RE: about write file wrong (Edit directly online) - by deanhystad - Jan-29-2021, 05:32 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Write to file emilng 1 1,747 Nov-08-2020, 08:44 PM
Last Post: Gribouillis
  Read directly from excel file using python script dvldgs05 0 2,274 Oct-19-2018, 02:51 AM
Last Post: dvldgs05
  write split words of sentence to file bluefrog 1 3,005 Aug-27-2018, 01:28 AM
Last Post: micseydel
  Homework - Read from/Write to file (renamed from Help help help) Amitkafle 1 3,066 Jan-11-2018, 07:24 AM
Last Post: wavic
  function to write the contents of a webpage into a new file roadrage 4 6,076 Dec-01-2016, 09:28 PM
Last Post: snippsat
  Extract data csv file and write in another file alexgrand 3 5,175 Nov-14-2016, 06:54 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020