2

I would like to check out whether the field of table TestProjectcontains the parameter from the Client-side passed, nested for loop is ugly, is there any efficient and easy way to realize it? Thanks so much for any advice.

def test(parameter_a: list, parameter_b: list) -> bool:
    age = TestPeople.age
    project_code = TestProject.project_code
    if age is not None and age <= 16:
        for code in project_code:
            for p in parameter_a:
                if code[:len(p)] == p:
                    return False
    return True

1 Answer 1

4

May be use itertools.product and all?

from itertools import product

def test(parameter_a: list, parameter_b: list) -> bool:
    age = TestPeople.age
    project_code = TestProject.project_code
    return (
        age is None 
        or age > 16
        or all(code.startswith(p) for code, p in product(project_code, parameter_a))
    )

EXPLANATION:

Here python tries to use short-circuit evaluation, so it will first check whether age is None, if it is, return True, otherwise check age > 16, if it is return True otherwise check if all the code, p pair match the condition code.startswith(p), if all the pairs pass the condition, return True, otherwise if any of the pair fail, return False immediately without checking the rest.

Sign up to request clarification or add additional context in comments.

16 Comments

code.startswith(p) is cleaner and probably faster than comparing p to a slice.
You still have to return True if the condition is false, which can be done with a single expression return age is None or age > 16 or all(...).
(I was writing up essentially the same answer when I saw yours pop up. :))
@chepner thanks, forgot about the first if check.
@SayandipDutta Thanks so so so much, just confirm again, no need to return True, really?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.