首页 > 解决方案 > loop multiple lists if exist and check for match (Python)

问题描述

What would be the best way to loop multiple lists if exist, then check each element in each list for a match and non match, then create a list for matching and non matching elements corresponding to that list.

I currently have a for loop for only 'ing1', but is there a more practical way to achieve this?

        ing1 = request.POST.getlist('ingredients1')
        ing2 = request.POST.getlist('ingredients2')
        ing3 = request.POST.getlist('ingredients3')


        for ele in ing1[1:]:
            ele_split = ele.split(",")
            for ele2 in ele_split:
                ing1list.append(ele2.strip().lower())
        print(ing1list)

标签: python

解决方案


尝试创建一个包含

lst_of_entries = [ing1, ing2, ing3]

然后通过迭代对列表中的所有元素执行您当前在 ing1 上的操作。这还需要您创建另一个输出变量名称列表,以便您可以附加它们。


推荐阅读