0

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_c: str, parameter_b: list=None) -> bool:
    """
      If project_code contains parameter_a and product_code contains 
      parameter_b, product_code must contains parameter_c """
    project_code = TestProject.project_code
    product_code = TestProduct.product_code
    
    for pj_code in project_code:
        for param in parameter_a:
            if pj_code[:len(param)] == param:
                for pd_code in product_code:
                    for p in parameter_b:
                        if (pd_code[:len(p)] == p):
                            for pc in parameter_c:
                                if pd_code[:len(pc)] == pc:
                                    return True
                            return False
    return True

According to Sayandip Dutta's answer to this speed up nested for-loop logic in Python with itertools.product and all

Can I change my original code to this? I have no data to test, I have to ensure my logic is correct. Is the below code right? If correct, is it really faster than my original code? if not correct, is there anyone who can tell me how to realize it? Thanks so much for any advice.

from itertools import product

def test(parameter_a: list, parameter_c: str, parameter_b: list=None) -> bool:
    project_code = TestProject.project_code
    product_code = TestProduct.product_code

    return (
        all(code.startswith(p) for code, p in product(project_code, parameter_a))
        and all(code.startswith(p) for code, p in product(product_code, parameter_b))
        and all(code.startswith(p) for code, p in product(product_code, parameter_c))
    )
6
  • 1
    I don't know what's supposed to be going on here, but your first and second snippets are doing completely different things - the second snippet is not an optimized version of the first. Commented Feb 28, 2021 at 9:16
  • Also: your first code is a drill down that eventually after lots of tests will return True - only your last for loop ever returns False - but the default is also return True ? .. This whole string of actions works on some data we know nothing about. Commented Feb 28, 2021 at 9:21
  • @user2357112supportsMonica I would like to check out If project_code contains parameter_a and product_code contains parameter_b, product_code must contains parameter_c, I also updated my question Commented Feb 28, 2021 at 14:20
  • @PatrickArtner I would like to check out If project_code contains parameter_a and product_code contains parameter_b, product_code must contain parameter_c, I also updated my question Commented Feb 28, 2021 at 14:21
  • @SayandipDutta Hi, I imitated your code, but I don't know whether it's correct or not, could you please help me have a look? Thanks so much, I would like to check out If project_code contains parameter_a and product_code contains parameter_b, product_code must contain parameter_c Commented Feb 28, 2021 at 14:24

0

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.