我有一个从 Excel 表中填写的字典。然后,我想知道字典的值中是否存在一个字符串。每个键的值是一个列表。
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"
我的问题是: - 有没有办法让值只是值而不是值列表? - 该函数返回“nop”,即使我要查找的字符串确实存在于其中一个列表中,为什么?
谢谢!
最佳答案
您可以只使用 in
运算符:
if look_for in value:
关于python - 在字典中查找值为列表的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28875256/