是否有方法搜索字典值以匹配python 2.7中的特定单词。我试着使用any()命令,但这没有返回任何有用的内容,我是否在正确的轨道上?
dict = {'1' : 'see john run'}
test = 'run'
if any (x in test for x in Dict.values()):
print "true"发布于 2016-02-01 01:45:44
您有正确的想法,但您需要切换in测试中的参数
data = {'1' : 'see john run'}
test = 'run'
if any(test in x for x in data.values()):
print "true"发布于 2016-02-01 01:45:40
只需尝试以下几点:
print test in mydict.values()[0].split()>>> mydict = {'1' : 'see john run'}
>>> test = 'run'
>>> print test in mydict.values()[0].split()
True
>>> 另外,我建议将变量名dict更改为其他内容(我使用了mydict),因为dict隐藏了内置的内容。
https://stackoverflow.com/questions/35121861
复制相似问题