-
手掌心
你可以使用字典。例如:have = {"milk": 2, "eggs": 3, "flour": 0.5}need = {"milk": 1, "eggs": 5, "flour": 2.5}ingredients = {i:"Yes" if have[i] >= need[i] else "No" for i in have.keys()}输出:print(ingredients){'milk': 'Yes', 'eggs': 'No', 'flour': 'No'}如果你想要一个函数来告诉你用你拥有的原料可以做多少蛋糕,你可以使用以下代码:def how_many(need, have): results = {i:have[i]//need[i] for i in have.keys()} return min(results.values())
-
慕桂英546537
假设输入如下milk=200eggs=10flour=1000milk_reqd=100eggs_reqd=5flour_reqd=2000have=[milk, eggs, flour]need=[milk_reqd, eggs_reqd, flour_reqd]解决方案import numpy as nphave=np.array(have)need=np.array(need)现在您可以执行所有操作,例如need>have或者need<=have或者need-have获取可以制作的蛋糕数量n_cakes=int(min(have/need))
-
莫回无
如果你需要比较的数量列表,你可以使用单行列表理解(只能比较==,>,<否则如果你使用>=和<=和==它们将是重叠操作) -milk_have = 10eggs_have = 20flour_have = 30milk_need = 10eggs_need = 25flour_need = 3have = [milk_have, eggs_have, flour_have]need = [milk_need, eggs_need, flour_need]['==' if i[0]==i[1] else '>' if i[0]>i[1] else '<' for i in zip(have, need)]['==', '<', '>']
-
ibeautiful
have = ('milk', 'eggs', 'flour',"k")need = ('milk', 'eggs', 'flour',"l")incredients = {}for i in range(len(have)): count = 0 if have[i] == need[i]: count +=1 incredients[have[i]] = count输出:{'eggs': 1, 'flour': 1, 'k': 0, 'milk': 1}