#๐Ÿ”’ I need help. I am doing a simulation and I want to check smth but i think it's VERY inneficient.

40 messages ยท Page 1 of 1 (latest)

onyx zinc
#

Can someone help me please?

hushed questBOT
#

@onyx zinc

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

gusty haven
#

by inefficient you mean the way used to check?(if statements)

onyx zinc
#

So I've got a simulation that simulates the natural selection, some individuals are there adn evolve, they adapt to survive more. I want to check how many times have they have reproduced in their whole life. There may be in total 700 000 people to check in total, and I think doing a loop to check it one by one might be inneficient

onyx zinc
#

Well it might not be slow, if it is not tell me pls

gusty haven
#

idk then sorry

stark fog
#

i don't really get how you'd check that (do you store parentage or something?), so perhaps start by writing an inefficient solution and maybe you'll see how to optimize it.

whole aspen
#

maybe store everyone's relationship in a tree?

onyx zinc
stark fog
#

So what's the problem then? You can read anyone's child count straight from their attribute.

onyx zinc
#

Yep ik but I won't know the average, I want to know how many times have they reproduced by average

whole aspen
onyx zinc
#

Is it? Then I will do it that way thx

stark fog
#

You could add 1 to a global counter in addition to the personal one every reproduction. But it's a bit of a silly optimization to do prematurely; realistically just do an extra loop at the end.

onyx zinc
#

I will do an extra loop

#

But I have another question stil

#

Still*

#

Do I have to return a list if the list is modified inside a function or not?

fading nymph
#

no you dont have to

onyx zinc
#

K thx ๐Ÿ™‚

whole aspen
#

i don't think it's good practice to have global variables inside functions though

fading nymph
#

but you should think about whether it makes sense to actually do that or not

#

usually thats a result of bad design

fading nymph
onyx zinc
#

I declare the list inside the main funct and then pass it to the other functio

#

Is there any better and optimal way? I am thinking that a list of 700 00 elements going through functions every time might be bad

onyx zinc
#

Ok I think I have not explained myself well enough

#

That list that I say it contains 700 000 objects is a list of every dead object

#

So therefore, at the end of the sim I will be able to go through them and divide the number of times everyone reproduced by the number of individuals

fading nymph
#

then do a loop

stark fog
onyx zinc
#

I will do it this way then, thanks ๐Ÿ™‚

#

!close

hushed questBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.