#πŸ”’ Simple question

34 messages Β· Page 1 of 1 (latest)

open hornet
#

Need help figuring out code logistics, its probably a really simple solution that just doesn't come to mind, how can I loop over the same conditions but execute different functions, without repeating the code? It feels redundant to write

for this:
for that:
do this

for this
for that
do that

Note that "this" needs to complete the loop before "that"

zenith basinBOT
#

@open hornet

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.

open hornet
#

very much, "this" needs to be done before "that"

empty trout
#

you could make a list of functions and then iterate over that list

open hornet
#

I was thinking that

pure summit
#

So something like this. ```py
for a, b in (this, that), (that, this):
for x in a:
for y in b:
func(x, y)

empty trout
#

do the functions require passing in arguments?

open hornet
#

can it be done simpler by making a list of "conditions"?

#

or do I need to transform the conditions into functions

#

because its essentially looping over a list of lists and then checking two different conditions

empty trout
#

as far as I understand, the conditions would be evaluated as soon as they are encountered, so you can't really make a list of them like that

#

you would need funcs or a lambda at least

open hornet
#

true

#

ok, how could I turn it into a lambda function

empty trout
#

what is the condition

pure summit
#

It may be useful to make a function that accepts a function predicate

open hornet
pure summit
#

what about the second condition?

open hornet
#
for i in range(len(object)): 
  for j in range(len(object[i]:
    if 10 in #function that finds neighbors of the current position and outputs a list of the values at those neighbors, requires the current position i, and j, as inputs:
      object[i][j] += 1
pure summit
#

What about making a generator for the nested for loops?

#
def cells():
  for i in range(object)):
    for j in range(len(object[i])):
      yield i, j

for i, j in cells():
  if foo:
    pass

if i, j in cells():
  if bar:
    pass
open hornet
#

wow, did not know this existed, this could help

#

thanks!

pure summit
#

.rp generators

static prairieBOT
#

Here are the top 5 results:

How to Use Generators and yield in Python
Generative Adversarial Networks: Build Your First Models
Using the NumPy Random Number Generator
Build a Python Directory Tree Generator for the Command Line
Iterators and Iterables in Python: Run Efficient Iterations
open hornet
#

is it possible to reduce it to a lambda function?

pure summit
#

like a generator comprehension?

open hornet
#

yes

pure summit
#

Yes, you can nest comprehensions

#

.rp comprehension

static prairieBOT
#

Here are the top 5 results:

Python Set Comprehensions: How and When to Use Them
When to Use a List Comprehension in Python
Python Dictionary Comprehensions: How and When to Use Them
Python's list Data Type: A Deep Dive With Examples
Python's filter(): Extract Values From Iterables
open hornet
#

thank you very much

zenith basinBOT
#
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.