gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:30:08 25 4
gpt4 key购买 nike

我正在 python 3.7 上尝试一个小 pep 项目,但似乎无法让它工作。我想找到一个存储在包含许多列表的对象中的给定列表。我确信我的编码还很糟糕,因为我在这方面几乎是新手!

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)

最佳答案

你的逻辑几乎是正确的,你只是搞乱了变量,而且你实际上并不需要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']

关于python - 从存储/列表库中查找给定列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53569476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com