尝试搜索我创建的名为location\uhw\u map的字典,我希望它能够在字符串“testString”中搜索其中一个单词,找到后将返回位置。你知道吗
例如,使用testString它应该打印出'lounge'的值
我的代码搜索它并找到“123456789”,但我似乎无法让它打印“lounge”!你知道吗
我相信这是一个简单的解决办法,但我似乎找不到答案!你知道吗
泰铢 马特。你知道吗
我也在这里放了一份副本;http://pythonfiddle.com/python-find-string-in-dictionary
#map hardware ID to location
location_hw_map = {'285A9282300F1' : 'outside1',
'123456789' : 'lounge',
'987654321' : 'kitchen'}
testString = "uyrfr-abcdefgh/123456789/foobar"
if any(z in testString for z in location_hw_map):
print "found" #found the HW ID in testString
#neither of the below work!!
#print location_hw_map[testString] #print the location
#print location_hw_map[z]
不要使用
any()
检查测试字符串是否在字典的键中,而是循环检查字典的键:印刷品:
相关问题 更多 >
编程相关推荐