按多个特征对项目进行排序

2024-05-18 10:17:21 发布

您现在位置:Python中文网/ 问答频道 /正文

Given a conveyor belt (list) full of assorted Fruit instances (all Apple or Orange),
your job is to sort them into 3 bins (lists): apples, oranges, and rejected.
Test size ('S', 'M', 'L') and quality according to the given attributes;
Reject all fruit that is small (S), medium (M) if less than 90 percent quality
or large (L) if less than 75 percent quality
Return a tuple of lists in apples, oranges, rejected order
    :param assorted_fruits: list of fruits
    :return: tuple of three lists -- apples, oranges, rejected

我知道如何根据单个特征将它们排序到列表中,比如代码中看到的水果种类,但我不确定如何使用多个特征。任何帮助都将不胜感激

apples = []
oranges = []
rejected = []

apples = [item for item in assorted_fruits if isinstance(item, Apple)]
oranges = [item for item in assorted_fruits if isinstance(item, Orange)]
rejected = [item for item in assorted_fruits if isinstance(item, Rejected)]

Tags: ofinforifallitemlistslist
1条回答
网友
1楼 · 发布于 2024-05-18 10:17:21
apples = []
oranges = []
rejected = []

for item in assorted_fruits:
    if  not (item.size == 'L' and item.quality >= 75) and not (item.size == 'M' and item.quality >= 90):
        rejected.append(item)
    elif isinstance(item,Apple):
        apple.append(item)
    elif isinstance(item,Orange):
        oranged.append(item)

相关问题 更多 >