#map hardware ID to location location_hw_map = {'285A9282300F1' : 'outside1', '123456789' : 'lounge', '987654321' : 'kitchen'} testString = "uyrfr-abcdefgh//foobar/123456789" #if any(z in testString for z in location_hw_map): # print "found HW ID in string" #found the HW ID in testString #neither of the below work!! #print location_hw_map[testString] #print the location #print location_hw_map[z] # This fixed it, http://stackoverflow.com/questions/17480652/searching-dictionary-for-string-and-returning-other-value for i in location_hw_map: # Loops through every key in the dictionary if i in testString: # If the key is in the test string (if "123456789" is in "uyrfr..." print location_hw_map[i] # Print the value of the key break # We break out of the loop incase of multiple keys that are in the test string
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question