Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List into list
#1
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()
    
Reply
#2
(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.
Reply
#3
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?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
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()
Reply
#5
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,091 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 878 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,714 Oct-26-2022, 04:03 PM
Last Post: deanhystad
Question Keyword to build list from list of objects? pfdjhfuys 3 1,500 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  Split a number to list and list sum must be number sunny9495 5 2,197 Apr-28-2022, 09:32 AM
Last Post: Dexty
  How to check if a list is in another list finndude 4 1,793 Jan-17-2022, 05:04 PM
Last Post: bowlofred
  Different out when using conda list and pip list Led_Zeppelin 1 3,966 Jan-14-2022, 09:30 PM
Last Post: snippsat
  Use one list as search key for another list with sublist of list jc4d 4 2,107 Jan-11-2022, 12:10 PM
Last Post: jc4d
  Need to parse a list of boolean columns inside a list and return true values Python84 4 2,036 Jan-09-2022, 02:39 AM
Last Post: Python84

Forum Jump:

User Panel Messages

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