conditional

Replacing Numpy elements if condition is met

Replacing Numpy elements if condition is met Question: I have a large numpy array that I need to manipulate so that each element is changed to either a 1 or 0 if a condition is met (will be used as a pixel mask later). There are about 8 million elements in the array and my …

Total answers: 6

How do you set a conditional in python based on datatypes?

How do you set a conditional in python based on datatypes? Question: This question seems mind-boggling simple, yet I can’t figure it out. I know you can check datatypes in python, but how can you set a conditional based on the datatype? For instance, if I have to write a code that sorts through a …

Total answers: 6

how to do a conditional decorator in python

How to do a conditional decorator in python? Question: Is it possible to decorate a function conditionally? For example, I want to decorate the function foo() with a timer function (timeit), but only when doing_performance_analysis condition is True, like this: if doing_performance_analysis: @timeit def foo(): """ Do something, e.g. sleep, and let timeit return the …

Total answers: 7

Check if something is (not) in a list in Python

Check if something is (not) in a list in Python Question: I have a list of tuples in Python, and I have a conditional where I want to take the branch ONLY if the tuple is not in the list (if it is in the list, then I don’t want to take the if branch) …

Total answers: 3

raise statement on a conditional expression

raise statement on a conditional expression Question: How do I elegantly implement the "Samurai principle" (return victorious, or not at all) on my functions? return <value> if <bool> else raise <exception> Asked By: F.D.F. || Source Answers: Well, you could test for the bool separately: if expr: raise exception(‘foo’) return val That way, you could …

Total answers: 5

Multiple IF conditions in a python list comprehension

Multiple IF conditions in a python list comprehension Question: I was wondering, is it possible to put multiple if conditions in a list comprehension? I didn’t find anything like this in the docs. I want to be able to do something like this ar=[] for i in range(1,n): if i%4 == 0: ar.append(‘four’) elif i%6 …

Total answers: 3

Python shorthand conditional

Python shorthand conditional Question: Here’s a quick one… In Python one can do: foo = foo1 if bar1 else foo2 And that’s cool, but how can I just get a True or False without having to write foo = True if bar1 else False For example, in JS you can forcibly cast a boolean type …

Total answers: 2

Python conditional assignment operator

Python conditional assignment operator Question: Does a Python equivalent to the Ruby ||= operator (“set the variable if the variable is not set”) exist? Example in Ruby : variable_not_set ||= ‘bla bla’ variable_not_set == ‘bla bla’ variable_set = ‘pi pi’ variable_set ||= ‘bla bla’ variable_set == ‘pi pi’ Asked By: Hartator || Source Answers: No, …

Total answers: 10