【发布时间】:2019-01-16 18:24:02
【问题描述】:
我想搜索以标题为键、http 链接作为分配给该键的值的字典。我想要通过字典搜索的函数,搜索包含我放入函数中的所有关键字的键,如果它没有找到任何带有关键字的键,它不会返回任何内容。这是字典:
我已经尝试过 if 和 in 语句,但目前还没有。
dict = {
'adidas originals yung-1 - core black / white':
'https://kith.com/products/adidas-originals-yung-1-core-black-white',
'adidas originals yung-1 - grey one / white':
'https://kith.com/products/adidas-originals-yung-1-grey-one-white',
'hoka one tor ultra high 2 wp boot - black':
'https://kith.com/products/hoka-one-tor-ultra-high-2-wp-black'}
假设我想搜索 black 和 ultra,该函数将返回字典中的第三项,因为 hoka one tor ultra high 2 wp boot - black' 包含关键字 black 和 ultra。如果它不包含我输入的所有关键字,它不会在字典中返回任何内容。
【问题讨论】:
-
发布你的尝试
-
听起来你想要代码。这是一些伪代码。将字典键存储在列表中。然后对于键列表中的每个键,使用 in 查看键项是否包含所有需要的关键字。如果是,请保存密钥并继续。最后,查看您保存的密钥列表并获取与每个密钥关联的 url。
标签: python dictionary keyword-search