将字典中的值与嵌套列表中的元素进行比较

时间:2018-11-09 16:48:01

标签: python-3.x list dictionary

我有字典和清单。我正在尝试将canteen1中的list1 [0]与'halal'的值进行比较。之后,我想在声明中打印答案

方案1:

canteen1 = {'price':[ ],'halal':['western','noodles']}

list1 = [['korean','japanese','western'],[ ],[ ]]

输出:您可以在canteen1尝试清真西部食品!

方案2:

canteen1 = {'price':[ ],'halal':['western','noodles']}

list1 = [['korean','japanese','western','noodles'],[ ],[ ]]

输出:您可以在canteen1尝试清真的西式面条!

P.S。花了很长时间在这里寻找答案,但找不到答案。对于编程这个问题,我真的很陌生,为此道歉。

1 个答案:

答案 0 :(得分:0)

canteen1 = {'price':[ ],'halal':['western','noodles']}

list1 = [['korean','japanese','western', 'noodles'],[ ],[ ]]

halal = canteen1['halal']

result = list(set(halal) & set(list1[0]))

if result:
        print("You could try the halal", ', '.join(result) ,"at canteen1!")    
else :
        print("You could not try the halal at canteen1!") 

在else(false)语句中,它不能显示与if(true)语句相同的结果,因为没有相同的元素,因此您只能说“您不能在canteen1尝试清真食品!”希望这会有所帮助:)