Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Behaviour of 2D array
#6
what I mean:
import random

def create_torus(max_vert, max_horz, density):
    torus = []
    for x in range(max_vert):
        torus.append([])
        for y in range(max_horz):
            torus[-1].append(int(random.randint(1, 100) < density))
    return torus

def pprint_torus(torus):
    for row in torus:
        print(row)


MAXHORZ = 15
MAXVERT = 10
DENSITY = 50
torus = create_torus(MAXVERT, MAXHORZ, DENSITY)
pprint_torus(torus)
create_torus can be one-lier using list comprehension
def create_torus(max_vert, max_horz, density):
    return [[int(random.randint(1, 100) < density) for y in range(max_horz)] for x in range(max_vert)]
Now, because you say it's [variation of] Conway's Game of Life, I think it will be better to move to OOP/class structure sooner than later.

e.g.

import random

class Torus:
    def __init__(self, max_vert, max_horz, density):
        self._torus = [[int(random.randint(1, 100) < density) for y in range(max_horz)] for x in range(max_vert)]

    @property
    def rows(self):
        return len(self._torus)

    @property
    def columns(self):
        return len(self._torus[0])

    def __repr__(self):
        return f"Torus({self.rows} rows x {self.columns} columns: {', '.join(str(row) for row in self._torus)})"

    def __str__(self):
        return '\n'.join(str(row) for row in self._torus)

if __name__ == '__main__':
    MAXHORZ = 15
    MAXVERT = 10
    DENSITY = 50
    torus = Torus(MAXVERT, MAXHORZ, DENSITY)
    print(torus)
    print(repr(torus))
Note, I am using regular lists, not array.array, but you get the idea. You can use whatever you want. However note that you also don't use array.array (despite what you may think):
torus = array('b') # here torus is array.array
torus = [[0] * MAXHORZ] * MAXVERT # here you bind name torus to different object - list of lists, it is no longer array.array

Also note that star imports lile from array import * are generally considered bad practice and discouraged.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
Behaviour of 2D array - by SimonB - Jan-21-2021, 09:56 AM
RE: Behaviour of 2D array - by buran - Jan-21-2021, 10:00 AM
RE: Behaviour of 2D array - by SimonB - Jan-21-2021, 10:34 AM
RE: Behaviour of 2D array - by buran - Jan-21-2021, 10:42 AM
RE: Behaviour of 2D array - by SimonB - Jan-21-2021, 11:41 AM
RE: Behaviour of 2D array - by buran - Jan-21-2021, 12:41 PM
RE: Behaviour of 2D array - by SimonB - Jan-21-2021, 01:29 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  logger behaviour setdetnet 1 966 Apr-15-2023, 05:20 AM
Last Post: Gribouillis
  can someone explain this __del__ behaviour? rjdegraff42 1 806 Apr-12-2023, 03:25 PM
Last Post: deanhystad
  Asyncio weird behaviour vugz 2 1,427 Apr-09-2023, 01:48 AM
Last Post: vugz
  Weird behaviour using if statement in python 3.10.8 mikepy 23 4,108 Jan-18-2023, 04:51 PM
Last Post: mikepy
  Generator behaviour bla123bla 2 1,190 Jul-26-2022, 07:30 PM
Last Post: bla123bla
  Inconsistent behaviour in output - web scraping Steve 6 2,727 Sep-20-2021, 01:54 AM
Last Post: Larz60+
  Adding to the dictionary inside the for-loop - weird behaviour InputOutput007 5 2,957 Jan-21-2021, 02:21 PM
Last Post: InputOutput007
  strange behaviour- plotting nathan_Blanc_Haifa 0 1,546 Dec-27-2020, 01:37 PM
Last Post: nathan_Blanc_Haifa
  OOP behaviour problem JohnB 3 2,510 Aug-18-2020, 07:51 PM
Last Post: JohnB
  understanding basic loop behaviour vinci 5 3,053 Feb-11-2020, 09:53 PM
Last Post: vinci

Forum Jump:

User Panel Messages

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