简体   繁体   中英

separating numbers with coma in python

I am building a sudoku game, I found a site that can provide me 1 million pre generated games. I downloaded the file (CSV) and want to prepare it for frontend use. Each game is 81 numbers like

346179258187523964529648371965832417472916835813754629798261543631485792254397186
974183652651274389283596714129835476746912538835647921568329147317468295492751863
563472198219386754847195623472638519951247386638519472795864231324951867186723945

I would love to create JS or JSON file with all the puzzles in order to import it in my code. Here is the ideal result for each game (line).

    [
         [5, 3, 4, 6, 7, 8, 9, 1, 2],
         [6, 7, 2, 1, 9, 5, 3, 4, 8],
         [1, 9, 8, 3, 4, 2, 5, 6, 7],
         [8, 5, 9, 7, 6, 1, 4, 2, 3],
         [4, 2, 6, 8, 5, 3, 7, 9, 1],
         [7, 1, 3, 9, 2, 4, 8, 5, 6],
         [9, 6, 1, 5, 3, 7, 2, 8, 4],
         [2, 8, 7, 4, 1, 9, 6, 3, 5],
         [3, 4, 5, 2, 8, 6, 1, 7, 9]
     ]

Here is what I managed to accomplish in python

import csv
import json
holder = []
rows = []
def divide_chunks(l, n): 
    # looping till length l 
    for i in range(0, len(l), n):  
        yield l[i:i + n] 

with open('sudoku.csv', newline='') as csvfile:
    counter = 0
    n = 9
    s=','
    reader = csv.DictReader(csvfile)
    for row in reader:
        if counter > 10:
            break
        print(row['solutions'])
        print(len(row['solutions']))    
        test = [int(str(row['solutions']))]
        #chunk = divide_chunks(test, n)
        # for val in enumerate(chunk):
        #     val = [val]
        #     # for index,item in enumerate(val):
        #     #     item[index] = item+s
        #     # print(val)

        holder.append(test)
        counter +=1


print(holder)

with open('puzzles.json', 'w') as outputfile:
    json.dump(holder,outputfile)

this is my output so far in puzzles.json

[
    [864371259325849761971265843436192587198657432257483916689734125713528694542916378],
    [346179258187523964529648371965832417472916835813754629798261543631485792254397186],
    [695127384138459672724836915851264739273981546946573821317692458489715263562348197],
    [497258316186439725253716498629381547375964182841572639962145873718623954534897261],
    [465912378189473562327568149738645291954821637216397854573284916642159783891736425],
    [194685237382974516657213489823491675541768923769352841215839764436527198978146352],
    [289765431317924856645138729763891542521473968894652173432519687956387214178246395],
    [894231657762495183351876942583624719219387564647159328128763495976542831435918276],
    [563472198219386754847195623472638519951247386638519472795864231324951867186723945],
    [163725948584693271729184365946358127371462589852917634498231756637549812215876493],
    [974183652651274389283596714129835476746912538835647921568329147317468295492751863]
]

Any suggestions please? Thanks

You can break the string into numbers by using a list comprehension

sudoku = 346179258187523964529648371965832417472916835813754629798261543631485792254397186
sudoku_list = [number for number in str(sudoku)]

And then you break that into chunks of length 9 by using another list comprehension

sudoku_final = [sudoku_list[9*i:9*i+9] for i in range(9)]

If you want the output to be integers instead of strings, use int(number) for number in str(sudoku) in the list comprehension

Using Python, you can separate all the numbers into a list by converting them into a string and back like so:

tuple(map(int, str(x)))  # x is the 81 digit number.

With that, you can separate the list of 81 digits into a 9x9 grid like so (iterating through every multiple of 9 and taking the next 9 numbers):

[x[i: i + 9] for i in range(0, 81, 9)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM