[英]How to search multiple variables in a class and print another? Is It possible to make this simpler?
我想编写一个程序,使我可以使用多个过滤器和不同的数据类型搜索一组数据。 在这里,我正在使用早餐食品,我希望能够指定一些卡路里,我希望每个卡路里都小于卡路里,然后选择红色,棕色,黄色还是它们的混合物。 然后,应打印出符合我的条件的每个项目的名称。
但是,例如,如果我要吃红色的食物,我也不介意它也是棕色和/或黄色的。 我还希望它打印出符合颜色规格且等于或小于我选择的卡路里数量的物品名称。
我希望能够搜索15种不同的颜色,但是有没有更简单的方法呢?
到目前为止,我已经编写了一个运行良好的程序,输入了很多卡路里,然后选择了我想要的三种颜色。 但是,这涉及为所有可能的输入编写说明。 我想有15种可能的颜色,这意味着要输入大量的代码。 另外,如果以后我想添加或删除颜色选项,这也将花费很长时间。
#Class definition
class Breakfast:
def __init__(self,name,number,is_red,is_brown,is_yellow):
self.name=name
self.number=number
self.is_red=is_red
self.is_brown=is_brown
self.is_yellow=is_yellow
#Food objects
Food_choices = [
Breakfast("Beans",300,True,False,False),
Breakfast("Sausage",400,False,True,False),
Breakfast("Bacon",500,True,True,False),
Breakfast("Toast",350,False,True,True),
Breakfast("Tomato",800,True,False,False),
Breakfast("Eggs",600,False,False,True),
Breakfast("Mushrooms",150,False,True,False)
]
#User input
calories = input("Enter calories ")
colour_a_input = input("Is is red? ")
colour_b_input = input("Is it brown? ")
colour_c_input = input("Is it yellow? ")
#Defining variables
coloura = ""
colourb = ""
colourc = ""
#Conveting input to Boolean values
if colour_a_input == "Yes":
coloura = True
else:
coloura = False
if colour_b_input == "Yes":
colourb = True
else:
colourb = False
if colour_c_input == "Yes":
colourc = True
else:
colourc = False
#Search function
for Breakfast in Food_choices:
if coloura is True:
if colourb is True:
if colourc is True:
if coloura == Breakfast.is_red and colourb == Breakfast.is_brown and colourc == Breakfast.is_yellow:
if float(calories) >= Breakfast.number:
print(Breakfast.name)
elif colourc is not True:
if coloura == Breakfast.is_red and colourb == Breakfast.is_brown:
if float(calories) >= Breakfast.number:
print(Breakfast.name)
elif colourb is not True:
if colourc is True:
if coloura == Breakfast.is_red and colourc == Breakfast.is_yellow:
if float(calories) >= Breakfast.number:
print(Breakfast.name)
elif colourc is not True:
if coloura == Breakfast.is_red:
if float(calories) >= Breakfast.number:
print(Breakfast.name)
elif coloura is not True:
if colourb is True:
if colourc is True:
if colourb == Breakfast.is_brown and colourc == Breakfast.is_yellow:
if float(calories) >= Breakfast.number:
print(Breakfast.name)
elif colourc is not True:
if colourb == Breakfast.is_brown:
if float(calories) >= Breakfast.number:
print(Breakfast.name)
elif colourb is not True:
if colourc is True:
if Breakfast.is_yellow is True:
if float(calories) >= Breakfast.number:
print(Breakfast.name)
elif colourc is not True:
print(Breakfast.name)
很抱歉发布整个文件,但是如果没有所有文件,我无法解决如何显示问题。
我能想到的最面向对象的方法是在早餐中添加一个方法,使您可以比较不同的实例,您可以覆盖运算符==
或仅实现isEqual()
方法。 然后,您可以使用从用户获得的数据创建早餐的新实例,并最终简单地使用如下for循环:
to_match = Breakfast("*", calories, coloura, colourb, colourc)
matches = []
for food in Food_choices:
if food.isEqual(to_match):
matches.append(to_match)
您可以通过defualt选择将*作为匹配所有字符的名称。 使用相同的逻辑,您还可以实现>
, >=
, <
, <=
运算符,这些运算符可以检查卡路里。
希望能帮助到你。
您是否尝试过将变量is_colour
更改为颜色列表? 它可以包含布尔值或字符串的列表/数组。 假设此示例的字符串列表。 那你可以改变你的
colour_a_input = input("Is is red? ")
colour_b_input = input("Is it brown? ")
colour_c_input = input("Is it yellow? ")
对于一个输入,要求给出颜色,并用(例如)逗号隔开,如下所示:
input = input("In what colours is it?")
输入应如下所示:
red, brown, yellow
并通过https://docs.python.org/3/library/stdtypes.html#str.split进行拆分,以创建新的输入颜色列表。
然后,您可以使用in
运算符(例如3 in [1, 2, 3]
返回true
,因为[1, 2, 3]
包含3)来制作这样的辅助函数(我从检查列表是否为子列表中得到 ) :
def sublist(lst1, lst2):
ls1 = [element for element in lst1 if element in lst2]
ls2 = [element for element in lst2 if element in lst1]
return ls1 == ls2
或检查输入(在split()
)是否为某些食物的颜色列表的子列表的方法。
最后,它看起来不是很大的if
语句,而是看起来像这样:
for breakfast in Food_choices:
if sublist(input, breakfast.colours) print(breakfast.name)
解决这个问题的方法可能更好,但我认为它看起来比仅使用if
语句检查所有可能的结果要好得多,而且还可以节省很多时间,因为您不必键入所有这些内容如果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.