我有两个列表和一个字典:
list_1 = ['the world', 'abc', 'bcd', 'want a car', 'hell', 'you rock']
list_2 = ['the world is big', 'i want a car', 'puppies are best', 'you rock the world']
dict_1 = {'a car':'i want a car', 'champ':'i am champ', 'you know':'you rock the world'}
现在,我想检查 dict_1
部分 中的键是否与 list_1
中的元素匹配,同时检查 {{1} 中的值是否匹配} 部分匹配来自dict_1
的元素,那么它是一个有效匹配,我们必须打印来自list_2
的匹配元素。
例如:
list_1
同时
dict_1.key('a car') matches 'want a car' from list_1
因此,这成为有效匹配。
到目前为止我尝试过的:
dict_1.value('i want') matches 'i want a car' from list_2
这会打印:
out = [ele_1 for key, value in dict_1.iteritems() for ele_1 in list_1 if key in ele_1 for elem_2 in list_2 if value in elem_2]
print list(set(out))
但我不相信这是我采取的最佳方法,我想了解我是否可以提高我的技能。
答案 0 :(得分:0)
这个答案可能对你有帮助
#Let there are two list list1 and list2 and a dictonary dict
list1=[2,5,6]
list2=[3,4,5]
dict={}
dict[2]=5
dict[3]=4
#The below given code checks whether the element from list1 matches dict.key and if it matches it will print "Key is present" else "Key is not present"
if dict.get(2)!=None:
print("Key is present")
else:
print("Key is not present")
#The below given code checks whether the element from list2 matches dict.value and if it matches it will print "Item is present" else "Item is not present"
if 4 in dict.values():
print("Item is present in dict.values")
else:
print("Item is not present in dict.values")