python - 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)
解决方案
尝试创建一个包含
lst_of_entries = [ing1, ing2, ing3]
然后通过迭代对列表中的所有元素执行您当前在 ing1 上的操作。这还需要您创建另一个输出变量名称列表,以便您可以附加它们。
推荐阅读
- javascript - 如何将背景颜色更改为用户在 HTML 中输入的内容
- r - R - 平均大矩阵
- reactjs - 在另一个函数中访问当前状态
- emoji - 如何使用 RegExr 在字符串中的特定短语之后提取所有 18 个字符条目的列表?
- java - 更新 Firebase 实时数据库中的值(离线) - Android
- haskell - Haskell HDBC 库:有没有一种优雅的方式在 [SqlValue] 和记录之间进行转换?
- html - 无法用鼠标选择密码字段!PHP网站问题
- postgresql - DBeaver + Postgres:调试功能打印到输出/控制台
- python - 调用 PutObject 操作 s3 boto 时的 BadDigest
- docker - 下载 ubuntu 包时 Docker 安装失败