0
的Python 3.4檢查字典值是一個字符串,輸出=鍵
比方說,我有一本字典:
dict = { "one" : ["abcdefg", "hij"], "two" : ["klmnopq"], "three" : ["rstuvwx", "zyx"] }
而且我也得到了一個字符串:
string = "XXXabcdefgXXXhijXXXXXklmnopqXXXXXXzyxXXXXzyxXXXXX"
...其中「X」代表其他不重要的字母。
我想檢查字典的VALUE是否在字符串中(例如,如果可以在字符串中找到「hij」),並且如果是,那麼我想要一個輸出來創建所有的在字符串中找到不同的項目。
事情是這樣的:
list = []
if dict.value in string:
list.append(dict.key)
print(list) --> ["one", "one", "two", "three", "three"]
什麼會得到這種結果的最佳方式?