Your condition is wrong. if i == k
compares the list element with the dictionary key, but you want to compare with the value, which is v
. And you don't need an exact match, you're looking for a substring, so you should be using in
rather than ==
. Since you seem to allow either string to be a substring of the other, use if v in i or i in v:
And the value you assign in the result is supposed to be the list element, not the dictionary value.
But you also want to use the value "none"
if no match is found. So you need to break out of the loop when you find a match, and use else:
to assign a value if the loop completes without breaking.
n = {}
for k,v in dic.items():
for i in lis:
if v in i or i in v:
n[k] = i
break
else:
n[k] = "none"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…