【问题标题】:Matching set of keywords to a string variable parsed from json将一组关键字与从 json 解析的字符串变量匹配
【发布时间】:2017-04-13 03:52:39
【问题描述】:

所以我有一个关键字数组,例如

keyword = ['Bob','hello','boot']

我解析了 json 并抓取了名为 title 的字符串值并输入到 name。

标题包含诸如“Bob hey, hello boot”之类的字符串

我正在尝试使程序仅在 Title/name 包含关键字数组列表中的所有单词时才返回 true。

目前,我有这个代码

keyword = ['Bob','hello','boot']
name=str(item[u'title'].encode('ascii','ignore')) #grab Title and input into name
found_a_string = True
for word in keyword:
    if not word in name.lower():
         found_a_string = False
    if found_a_string:
         ID=str(item[u'id'])
         print name, ID, 'found' #would output full title and then assoicated ID from json.

但只要关键字中的一个单词匹配,代码就会返回 true。

任何帮助将不胜感激。

谢谢

【问题讨论】:

标签: json python-2.7


【解决方案1】:

你可以全部使用:

keyword = ['Bob','hello','boot']
name = "Bob hey, hello boot"
if all(x in name for x in keyword):
    ID=str(item[u'id'])
    print name, ID, 'found'

【讨论】:

    猜你喜欢
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    • 2019-01-12
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    • 2014-06-20
    • 1970-01-01
    相关资源
    最近更新 更多