在dict中查找值为列表的项目

Amy*_*ria 1 python excel dictionary list

我有一个我从excel表填写的词典.然后,我想知道dict的值中是否存在字符串.每个键的值是一个列表.

import xlrd
wb=xlrd.open_workbook('C:\\Inputs_UNF_newForm.xlsx')
p=wb.sheet_by_name('Products')

def isProduct(look_for):
    Product = {}
    Keep = []

    (start,end)= rowMatrix("Products")
    number_of_columns=p.ncols
    for row in range(start,end):
        key = str(p.cell(row,0).value)
        Keep=[]
        for col in range(1,number_of_columns):
            value  = str((p.cell(row,col).value))
            if value !="":
                Keep.append(value)
        Product[key] = Keep
    print(Product)
    for value in Product.values(): #Doesn't work :(
        if look_for == value:
            return "yey"
    return "nop"
Run Code Online (Sandbox Code Playgroud)

我的问题是: - 有没有办法让价值只是价值而不是价值清单? - 即使我正在寻找的字符串确实存在于其中一个列表中,该函数返回"nop",为什么?

谢谢!

Kla*_* D. 6

你可以使用in运营商:

if look_for in value:
Run Code Online (Sandbox Code Playgroud)