[Tutor] Counting the # of iterations OR storing # values in a list
Peter Otten
__peter__ at web.de
Mon Aug 13 17:43:43 EDT 2018
Gautam Desai wrote:
> I am currently working with the lattice.py code attached. The code is a
> simulation that models the interactions between bacteria by producing a
> lattice output.
>
> Once you run the code, there should be both blue and red bacteria that
> show
> on a lattice. I can manipulate the size of the lattice to be an integer.
> In this case, I have set it to 250
>
> However, there are different bacteria cells occupying each "pixel" on the
> lattice. I could count the number of red or blue bacteria on the output,
> but It would take an extremely long time
Counting doesn't take very long, at least for *only* 250**2 cells, and
there's even a Counter in the stdlib. When I tried
$ python3 -i lattice.py
>>> from collections import Counter
>>> Counter(np.ravel(my_lattice.lattice))
Counter({0.22950000000000001: 31306, 0.0025400000000000002: 31194})
there was no noticeable delay on my aging hardware. For really large arrays
>>> (my_lattice.lattice == 0.2295).sum()
31306
is probably faster.
More information about the Tutor
mailing list