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.
Age Doesn't Matter, Your Passion Does
- Shreya Jain
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
  Compile list of dictianories out of another list of dictianories by certain keys CatorCanulis 10 633 Jun-10-2021, 08:35 PM
Last Post: perfringo
  Order a list with successive permutations based on another list yvrob 3 605 Mar-19-2021, 08:20 AM
Last Post: supuflounder
  Saving list in a list quest_ 3 736 Mar-10-2021, 09:58 AM
Last Post: quest_
Star Convert Bytearray into List using list() Shlok 2 504 Feb-18-2021, 10:44 AM
Last Post: deanhystad
  Adding List Element if Second part of the List Elements are the Same quest_ 3 621 Nov-25-2020, 04:33 PM
Last Post: bowlofred
  Count number of occurrences of list items in list of tuples t4keheart 1 570 Nov-03-2020, 05:37 AM
Last Post: deanhystad
Question Save list with nested list into CSV SpongeB0B 1 1,131 Oct-12-2020, 07:26 AM
Last Post: bowlofred
  Appending to list of list in For loop nico_mnbl 2 577 Sep-25-2020, 04:09 PM
Last Post: nico_mnbl
  How to make a list of values from a dictionary list? faryad13 2 647 Sep-03-2020, 03:45 PM
Last Post: faryad13
  Append list into list within a for loop rama27 2 784 Jul-21-2020, 04:49 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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