首页 > 解决方案 > 如何检查列表的所有元素是否都在字符串列表中

问题描述

我正在解析一个网站以捕捉可用的产品和尺寸。加载了 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 循环会是遍历我的列表的最佳和最快的方法吗?谢谢!欢迎任何帮助!

标签: python-3.xstringlist

解决方案


首先,您不应该将 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)

推荐阅读