Python - 使用多个列表

时间:2018-03-14 04:37:49

标签: python for-loop if-statement

为什么这个程序不起作用?

sorry we dont have these toppings

,输出

date

6 个答案:

答案 0 :(得分:3)

您想要检查requested_toppings列表是available_toppings的子集。

您可以使用set.issubset()功能。

available_toppings = ["mushrooms", "olives", "green peppers", "pepperoni", "pineapple", "extra cheese"]

requested_toppings = ['mushrooms', 'olives', 'extra cheese']

if set(requested_toppings).issubset(available_toppings):
    for requested_topping in requested_toppings:
        print("Adding " + requested_topping.title() + ".")
    print("Finalising your order.")

else:
    print("sorry we dont have these toppings")

这会导致

Adding Mushrooms.
Adding Olives.
Adding Extra Cheese.
Finalising your order.

如果您在olives中将shrimps替换为requested_toppings,那么

sorry we dont have these toppings

正如所料。

答案 1 :(得分:1)

看起来您切换了for循环和if条件的顺序。也许你想要以下内容:

  • 对于每个请求的顶部,检查它是否在可用的浇头

您可以尝试以下方法,而不是检查整个请求列表是否在其他可用列表中:

available_toppings = ["mushrooms", "olives", "green peppers", "pepperoni", "pineapple", "extra cheese"]

requested_toppings = ['mushrooms', 'olives', 'extra cheese']

for requested_topping in requested_toppings:
    if requested_topping in available_toppings:
        print("Adding " + requested_topping.title() + ".")
    else:
        print("sorry we dont have these toppings")

print("Finalising your order.")

答案 2 :(得分:1)

python中有两个漂亮的函数,all()any()。尝试使用all()

available_toppings = ["mushrooms", "olives", "green peppers", "pepperoni", "pineapple", "extra cheese"]

requested_toppings = ['mushrooms', 'olives', 'extra cheese']

if all(topping in available_toppings for topping in requested_toppings):
    for requested_topping in requested_toppings:
        print("Adding " + requested_topping.title() + ".")
    print("Finalising your order.")

else:
    print("sorry we dont have these toppings")

您的代码有什么问题?您检查列表是否是另一个列表的元素,例如:

>>> [1,2] in [1,2,3]
False
>>> [1,2] in [[1,2],3]
True

答案 3 :(得分:0)

试试这个:

available_toppings = ["mushrooms", "olives", "green peppers", "pepperoni", "pineapple", "extra cheese"]

requested_toppings = ['mushrooms', 'olives', 'extra cheese']
hasItem = True
for requested_topping in requested_toppings:
    if requested_topping not in available_toppings:   
        hasItem = False   
if hasItem:
    for requested_topping in requested_toppings:
        print("Adding " + requested_topping.title() + ".")
    print("Finalising your order.") 
else:
        print("sorry we dont have these toppings") 

答案 4 :(得分:0)

不完全是你想要的,但很容易适应。

available_toppings = ["mushrooms", "olives", "green peppers", "pepperoni", "pineapple", "extra cheese"]
requested_toppings = ["mushrooms", "olives", "extra cheese", "onions"]
RQ = []
for requested in requested_toppings:
    RQ.append(requested)
for available in available_toppings:
    for R in RQ:
        if R in available:print "We have :",R
if R not in available:print "Do not Have : ",R 
RESULTS:
We have : mushrooms
We have : olives
We have : extra cheese
Do not Have :  onions

答案 5 :(得分:0)

对于另一种方法 - 可能不是更好而不是现有的答案,实际上可能更糟 - 但它确实以简单的方式展示了一些有趣的想法。

try:
    output = []
    # Notice that we're not pre-checking here. So this will
    # work even if requested_toppings is a one-shot iterator.
    for requested_topping in requested_toppings:
        if requested_topping in available_toppings:
            # Don't actually print, because that's irreversible;
            # do it "off to the side" or "in a transaction" that we
            # can "commit" or "rollback".
            output.append("Adding " + requested_topping.title() + ".")
        else:
            # Here's where we "rollback"
            raise KeyError(requested_topping)
except KeyError:
    # Discard the "transaction" by just not doing anything with it.
    # And now we're out of the "pure" section of the code, so we
    # can do I/O without worrying.
    print("sorry we dont have these toppings")
else:
    # "Commit" by just printing the output.
    print(*output, sep='\n')
    # And again, we're out of the pure code now, so just print
    print("Finalising your order.")

再解释一下:

这个问题唯一棘手的部分是,如果你不拥有所有这些问题,你就不想开始添加配料(因为那时你会给顾客一个错误的希望 - 而你可能要扔掉一整块披萨。)

显而易见的解决方案是通过使用子集测试(如在Dmitri Chubarov's answer中)或all循环(如在Amaro Vita's中)来检查是否预先提供了所有配件或只是for声明。在这种情况下,这样做很好,这就是你应该做的事情。

但是有一些问题你无法做到。也许requested_toppings是一个迭代器,你要么不能重复,要么重复得非常昂贵。或许提前测试非常困难,你所能做的就是尝试所有的操作,看看其中一个操作是否失败。对于那些问题,你需要一些方法来避免做昂贵和不可逆转的事情,比如添加披萨配料或发射导弹。当你使用像这样的解决方案时就是这样。

当然你可以做同样的事情,没有例外,只需使用break进行回滚,else进行for子句进行提交,但似乎更多人发现for ... else比例外情况更令人困惑。

最后一件事:如果您将available_toppings转换为集合而不是列表,所有解决方案(包括我的解决方案)都会更好一些。你唯一能做的就是in测试,以及那些是为什么设置的。 (而且它不仅仅是一个概念上的区别,而是一个性能问题 - 您可以在一个固定时间内对一组进行in测试,而对于一个列表,它必须检查每个测试的所有值。)