python-3.x - 如何检查列表的所有元素是否都在字符串列表中
问题描述
我正在解析一个网站以捕捉可用的产品和尺寸。加载了 3 个产品。有一个名为“find_id_1”的列表,其中包含 3 个元素,每个元素都有产品名称及其变体 ID。我制作了另外 2 个列表,一个命名为关键字,一个命名为否定。关键字列表包含我想要的产品标题应具有的关键字。如果负面清单中的任何元素在产品标题中,那么我不想要该产品。
found_product = []
keywords = ['YEEZY','BOOST','700']
negative = ['INFANTS','KIDS']
find_id_1 = ['{"id":2069103968384,"title":
"\nYEEZY BOOST 700 V2","handle":**"yeezy-boost-700-v2-vanta-june-6"**,
[{"id":19434310238336,"parent_id":2069103968384,"available":true,
"sku":"193093889925","featured_image":null,"public_title":null,
"requires_shipping":true,"price":30000,"options"',
'{"id":2069103935616,"title":"\nYEEZY BOOST 700 V2 KIDS","handle":
"yeezy-boost-700-v2-vanta-kids-june-6",`
["10.5k"],"option1":"10.5k","option2":"",
`"option3":"","option4":""},{"id":19434309845120,"parent_id":2069103935616,
"available":false,"sku":"193093893625","featured_image":null,
"public_title":null,"requires_shipping":true,"price":18000,"options"',
'{"id":2069104001152,"title":"\nYEEZY BOOST 700 V2 INFANTS",
"handle":**"yeezy-boost-700-v2-vanta-infants-june-6"***,`
["4K"],"option1":"4k","option2":"",`
"option3":"","option4":""},{"id":161803398876,"parent_id":2069104001152,
"available":false,"sku":"193093893724",
"featured_image":null,"public_title":null,
"requires_shipping":true,"price":15000,"options"']
我尝试使用 for 循环遍历 find_info_1 中的每个元素,然后创建另一个循环遍历关键字和否定中的每个元素,但我得到了错误的产品。这是我的代码:
for product in find_id_1:
for key in keywords:
for neg in negative:
if key in product:
if neg not in product:
found_product = product
它打印以下内容:
'{"id":2069104001152,"title":"\nYEEZY BOOST 700 V2 INFANTS",
"handle":"yeezy-boost-700-v2-vanta-infants-june-6,`
["4K"],"option1":"4k","option2":"",`
"option3":"","option4":""},
{"id":161803398876,"parent_id":2069104001152,
"available":false,"sku":"193093893724",
"featured_image":null,"public_title":null,
"requires_shipping":true,"price":15000,"options"']
我试图让它从 find_info_1 返回元素 0,因为那是唯一一个没有列表中的任何元素的元素。使用 for 循环会是遍历我的列表的最佳和最快的方法吗?谢谢!欢迎任何帮助!
解决方案
首先,您不应该将 json 数据视为字符串。只需使用 json 库解析 json,这样您就可以只检查产品的标题。随着产品列表和每个产品的规格越来越大,迭代所花费的时间也会增加。
要回答你的问题,你可以简单地做
for product in find_id_1:
if any(key in product for key in keywords):
if not any(neg in product for neg in negative):
found_product.append(product)
这将根据您的规范为您提供元素。但是我对您的数据进行了一些更改,只是为了使其成为有效的 python 代码..
found_product = []
keywords = ['YEEZY','BOOST','700']
negative = ['INFANTS','KIDS']
find_id_1 = [""""'{"id":2069103968384,"title":
"\nYEEZY BOOST 700 V2","handle":**"yeezy-boost-700-v2-vanta-june-6"**,
[{"id":19434310238336,"parent_id":2069103968384,"available":true,
"sku":"193093889925","featured_image":null,"public_title":null,
"requires_shipping":true,"price":30000,"options"'""",
""""'{"id":2069103935616,"title":"\nYEEZY BOOST 700 V2 KIDS","handle":
"yeezy-boost-700-v2-vanta-kids-june-6",`
["10.5k"],"option1":"10.5k","option2":"",
`"option3":"","option4":""},{"id":19434309845120,"parent_id":2069103935616,
"available":false,"sku":"193093893625","featured_image":null,
"public_title":null,"requires_shipping":true,"price":18000,"options"'""",
""""'{"id":2069104001152,"title":"\nYEEZY BOOST 700 V2 INFANTS",
"handle":**"yeezy-boost-700-v2-vanta-infants-june-6"***,`
["4K"],"option1":"4k","option2":"",`
"option3":"","option4":""},{"id":161803398876,"parent_id":2069104001152,
"available":false,"sku":"193093893724",
"featured_image":null,"public_title":null,
"requires_shipping":true,"price":15000,"options"'"""]
for product in find_id_1:
if any(key in product for key in keywords):
if not any(neg in product for neg in negative):
found_product.append(product)
print(found_product)
推荐阅读
- azure - 如何将对象从触发器传递到编排和活动函数
- node.js - JSON 文件无法修改
- java - Twilio SMS api 不支持返回内容类型
- xslt - 如果其中包含特定的子字符串,则对 httpsamples 进行分组
- python - 十六进制输出总是乱码
- python-3.x - 如何在 Python 3.x 中查询 GCP 日志查看器并获取 json 结果(如 gcloud 日志读取)
- pandas - 熊猫按周分组并获得一天
- javascript - 如何获取 LG-WebOS 智能电视的 MAC 地址?
- python - 使用 beautifulsoup 抓取元素列表
- java - 如果在 if 中调用方法,则方法不执行任何操作,如果直接从 on 事件调用,则它可以工作