Python Forum
List into list - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: List into list (/thread-22561.html)



List into list - Reldaing - Nov-17-2019

Hi, I'm trying to get familiar with lists. Right now, I have a little Issue. I'm trying to convert this code (kind of sodoku) into a function, who takes in arguments lists . here's my code:
def Hi1():
    a = [[1, 2, 3, 4, 5, 6, 7, 8, 9], [4, 5, 6, 7, 8, 9, 1, 2, 3], [7, 8, 9, 1, 2, 3, 4, 5, 6], [2, 3, 1, 5, 6, 4, 8, 9, 7], [5, 6, 4, 8, 9, 7, 2, 3, 1],[8, 9, 7, 2, 3, 1, 5, 6, 4], [3, 1, 2, 6, 4, 5, 9, 7, 8], [6, 4, 5, 9, 7, 8, 3, 1, 2], [9, 7, 8, 3, 1, 2, 6, 4, 5]]
    for i in range(len(a)):
        for j in range(len(a[i])):
            print(a[i][j], end=' ')
        print()
And this is what I made, I can't understand how array works in python, Thank ya all !
def Hi2(a):
    a=[[],[],[],[],[],[],[],[]]
    for i in range(len(a)):
        for j in range(len(a[i])):
            print(a[i][j], end=' ')
        print()
    



RE: List into list - SheeppOSU - Nov-17-2019

(Nov-17-2019, 10:16 PM)Reldaing Wrote: I can't understand how array works in python
Imagine an array as a shelf. If I make an array like this for example array = [1, 2, 3, 4] you can imagine it as a shelf with number from left to right. If I want to call upon a certain part of the array, I use numbers. So if I call column 0 array[0] then it will return the 1. If I put an array, inside an array, then it's basically shelves in shelves. I use numbers to call upon the shelf I want, then I use numbers to call from things in that shelf. Here's an example colors = [[0, 0, 0], [255, 255, 255], [200, 10, 10]] Here is a list of colors. The colors are numbers stored in arrays. So to call the first color (black), I do this colors[0] and that will return the array for the color black. So if colors[0] returns an array, than I can call from it once again, like this colors[0][1] and that will give me the second number of the rgb array.
I hope this helps.


RE: List into list - perfringo - Nov-18-2019

Maybe you don't understand how names work in Python?

In function Hi2 you assign to name 'a' a new value (empty matrix). It doesn't matter with which argument you call this function, it will always deal with empty 'a' assigned in body of the function.

What are you trying to accomplish?


Array - Reldaing - Nov-18-2019

Hi, I'm trying to get familiar with array. Right now, I've made something like a sodoku: Here's my code:
def Hi1():
    a = [[1, 2, 3, 4, 5, 6, 7, 8, 9], [4, 5, 6, 7, 8, 9, 1, 2, 3], [7, 8, 9, 1, 2, 3, 4, 5, 6], [2, 3, 1, 5, 6, 4, 8, 9, 7], [5, 6, 4, 8, 9, 7, 2, 3, 1],[8, 9, 7, 2, 3, 1, 5, 6, 4], [3, 1, 2, 6, 4, 5, 9, 7, 8], [6, 4, 5, 9, 7, 8, 3, 1, 2], [9, 7, 8, 3, 1, 2, 6, 4, 5]]
    for i in range(len(a)):
        for j in range(len(a[i])):
            print(a[i][j], end=' ')
        print()
Output:
>>> Hi1() 1 2 3 4 5 6 7 8 9 4 5 6 7 8 9 1 2 3 7 8 9 1 2 3 4 5 6 2 3 1 5 6 4 8 9 7 5 6 4 8 9 7 2 3 1 8 9 7 2 3 1 5 6 4 3 1 2 6 4 5 9 7 8 6 4 5 9 7 8 3 1 2 9 7 8 3 1 2 6 4 5
The problem is that I don't know how to turn this code in a fonction which takes lists in argument(I want to enter the lists in the console, and then it gives me the sodoku). This is what I've made so far. Could you please fix my prblem? Thanks !

def Hi2(a):
    a=[[],[],[],[],[],[],[],[]]
    for i in range(len(a)):
        for j in range(len(a[i])):
            print(a[i][j], end=' ')
        print()



RE: List into list - ThomasL - Nov-18-2019

Have a look at this sample. I advise you work through a python tutorial to learn the basics.
def enter_sudoku(size):
    sudoku = []
    for i in range(size):
        row = [int(number) for number in list(input(f"Enter exactly {size} digits [1-9] without spaces: "))]
        sudoku.append(row)
    return sudoku


def show_sudoku(sudoku):
    for row in sudoku:
        for number in row:
            print(f"{number}", end=' ')
        print()

matrix = enter_sudoku(9)
show_sudoku(matrix)