为了账号安全,请及时绑定邮箱和手机立即绑定

从列表的存储/库中查找给定的列表

从列表的存储/库中查找给定的列表

慕的地8271018 2021-09-02 19:25:54
我正在 python 3.7 上尝试一个小项目,但似乎无法让它工作。我想找到存储在包含许多列表的对象中的给定列表。我确定我的编码很差,因为我在这方面几乎是新手!my_choice = ["a", "b", "c"]reciepe1 = [["a", "b", "c"], "d", "e", "f"]reciepe2 = ["x", "y", "z"]menu = [reciepe1, reciepe2]for my_choice in menu:    if my_choice in reciepe1:        print(reciepe1)    elif my_choice in reciepe2:        print(reciepe2)  
查看完整描述

2 回答

?
慕桂英3389331

TA贡献2036条经验 获得超8个赞

您的逻辑几乎是正确的,您只是弄乱了变量,实际上并不需要elif:


my_choice = ["a", "b", "c"]

recipe1 = [["a", "b", "c"], "d", "e", "f"]

recipe2 = ["x", "y", "z"]

menu = [recipe1, recipe2]

for recipe in menu:

    if my_choice in recipe:

        print(recipe)

产出


[['a', 'b', 'c'], 'd', 'e', 'f']


查看完整回答
反对 回复 2021-09-02
  • 2 回答
  • 0 关注
  • 150 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信