【问题标题】:How to check if all element of a list is inside of a list of strings如何检查列表的所有元素是否都在字符串列表中
【发布时间】:2019-06-07 01:29:03
【问题描述】:

我正在解析网站以获取可用产品和尺寸。加载了 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 循环遍历关键字和否定中的每个元素,但我得到了错误的产品。这是我的代码:

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.x string list


    【解决方案1】:

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

    【讨论】:

    • 非常感谢这个工作。我试图通过使用 json 来解析它,但是在我能够将它归结为这一小块信息之前,我解析出了太多垃圾
    猜你喜欢
    • 2015-10-03
    • 2018-06-24
    • 2019-12-22
    • 2016-09-18
    • 2018-02-09
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多