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))
)
project_codecontainsparameter_aandproduct_codecontainsparameter_b,product_codemust containparameter_c, I also updated my questionproject_codecontainsparameter_aandproduct_codecontainsparameter_b,product_codemust containparameter_c