یک مجموعه، مجموعهای تغییرپذیر و نامرتب از اشیای هشپذیر است.
اعضای مجموعه باید متمایز باشند. عناصر تکراری مجاز نیستند.
میتوانند چندین نوع دادهی متفاوت و حتی ساختارهای تودرتو مانند یک tuple از tuples را در خود نگه دارند، به شرطی که بتوان همهی عناصر را هش کرد.
مجموعهها نسخهی تغییرناپذیر frozensets هم دارند.
مجموعهها بیشتر برای حذف سریع تکراریها از سایر ساختارهای داده یا از گروههایی از عناصر به کار میروند. از آنها همچنین برای مقایسههای کارآمد استفاده میشود، زمانی که ترتیبدهی و ردیابی تکراریها لازم نیست.
sets هم مانند سایر انواع مجموعهای (دیکشنریها، لیستها، تاپلها) از این موارد پشتیبانی میکنند:
for item in <set>
in و not in
len()
copy()
sets از این موارد پشتیبانی نمیکنند:
+
بررسی عضویت در یک set بهطور میانگین پیچیدگی زمانی ثابت دارد، در حالی که پیچیدگی زمانی بررسی عضویت در یک list یا string با افزایش طول داده بیشتر میشود.
متدهایی مانند <set>.union()، <set>.intersection() یا <set>.difference() هم بهطور میانگین پیچیدگی زمانی ثابت دارند.
یک set را میتوان مستقیماً بهصورت یک لیترال مجموعه، با آکولادهای {} و کاما میان عناصر نوشت.
تکراریها بیصدا حذف میشوند:
>>> one_element = {'➕'}
{'➕'}
>>> multiple_elements = {'➕', '🔻', '🔹', '🔆'}
{'➕', '🔻', '🔹', '🔆'}
>>> multiple_duplicates = {'Hello!', 'Hello!', 'Hello!',
'¡Hola!','Привіт!', 'こんにちは!',
'¡Hola!','Привіт!', 'こんにちは!'}
{'こんにちは!', '¡Hola!', 'Hello!', 'Привіт!'}
لیترالهای مجموعه از همان آکولادهای لیترال dict استفاده میکنند، یعنی برای ساختن یک set خالی باید از set() استفاده کنید.
set() (سازندهی کلاس set) را میتوان با هر iterableای که بهعنوان آرگومان داده میشود به کار برد.
عناصر iterable یکییکی پیمایش و به set اضافه میشوند.
ترتیب عناصر حفظ نمیشود و تکراریها بیصدا حذف میشوند:
# To create an empty set, the constructor must be used.
>>> no_elements = set()
set()
# The tuple is unpacked & each element is added.
# Duplicates are removed.
>>> elements_from_tuple = set(("Parrot", "Bird",
334782, "Bird", "Parrot"))
{334782, 'Bird', 'Parrot'}
# The list is unpacked & each element is added.
# Duplicates are removed.
>>> elements_from_list = set([2, 3, 2, 3, 3, 3, 5,
7, 11, 7, 11, 13, 13])
{2, 3, 5, 7, 11, 13}
به دلیل رفتار «بازگشایی» آن، ممکن است استفاده از set() با یک رشته غافلگیرکننده باشد:
# String elements (Unicode code points) are
# iterated through and added *individually*.
>>> elements_string = set("Timbuktu")
{'T', 'b', 'i', 'k', 'm', 't', 'u'}
# Unicode separators and positioning code points
# are also added *individually*.
>>> multiple_code_points_string = set('अभ्यास')
{'अ', 'भ', 'य', 'स', 'ा', '्'}
مجموعهها میتوانند انواع دادههای مختلف و انواع دادهی تودرتو را در خود نگه دارند، اما همهی عناصر set باید هشپذیر باشند:
# Attempting to use a list for a set member throws a TypeError
>>> lists_as_elements = {['🌈','💦'],
['☁️','⭐️','🌍'],
['⛵️', '🚲', '🚀']}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
# Standard sets are mutable, so they cannot be hashed.
>>> sets_as_elements = {{'🌈','💦'},
{'☁️','⭐️','🌍'},
{'⛵️', '🚲', '🚀'}}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'set'
مجموعهها متدهایی دارند که عموماً از عملیات مجموعهای ریاضی تقلید میکنند.
بیشتر این متدها (نه همه) معادل عملگر دارند.
متدها عموماً هر iterableای را بهعنوان آرگومان میپذیرند، در حالی که عملگرها لازم دارند که هر دو طرف عملیات sets یا frozensets باشند.
از متد <set>.isdisjoint(<other_collection>) برای آزمودن اینکه آیا عناصر یک set با عناصر set دیگری همپوشانی دارند استفاده میشود.
این متد هر iterable یا setای را بهعنوان آرگومان میپذیرد.
اگر دو مجموعه هیچ عنصر مشترکی نداشته باشند True و اگر عناصری مشترک داشته باشند False برمیگرداند.
معادل عملگری ندارد:
# Both mammals and additional_animals are lists.
>>> mammals = ['squirrel','dog','cat','cow', 'tiger', 'elephant']
>>> additional_animals = ['pangolin', 'panda', 'parrot',
'lemur', 'tiger', 'pangolin']
# Animals is a dict.
>>> animals = {'chicken': 'white',
'sparrow': 'grey',
'eagle': 'brown and white',
'albatross': 'grey and white',
'crow': 'black',
'elephant': 'grey',
'dog': 'rust',
'cow': 'black and white',
'tiger': 'orange and black',
'cat': 'grey',
'squirrel': 'black'}
# Birds is a set.
>>> birds = {'crow','sparrow','eagle','chicken', 'albatross'}
# Mammals and birds don't share any elements.
>>> birds.isdisjoint(mammals)
True
# There are also no shared elements between
# additional_animals and birds.
>>> birds.isdisjoint(additional_animals)
True
# Animals and mammals have shared elements.
# **Note** The first object needs to be a set or converted to a set
# since .isdisjoint() is a set method.
>>> set(animals).isdisjoint(mammals)
False
از <set>.issubset(<other_collection>) برای بررسی اینکه آیا هر عنصر <set> در <other_collection> هم هست استفاده میشود.
شکل عملگری آن <set> <= <other_set> است:
# Both mammals and additional_animals are lists.
>>> mammals = ['squirrel','dog','cat','cow', 'tiger', 'elephant']
>>> additional_animals = ['pangolin', 'panda', 'parrot',
'lemur', 'tiger', 'pangolin']
# Animals is a dict.
>>> animals = {'chicken': 'white',
'sparrow': 'grey',
'eagle': 'brown and white',
'albatross': 'grey and white',
'crow': 'black',
'elephant': 'grey',
'dog': 'rust',
'cow': 'black and white',
'tiger': 'orange and black',
'cat': 'grey',
'squirrel': 'black'}
# Birds is a set.
>>> birds = {'crow','sparrow','eagle','chicken', 'albatross'}
# Set methods will take any iterable as an argument.
# All members of birds are also members of animals.
>>> birds.issubset(animals)
True
# All members of mammals also appear in animals.
# **Note** The first object needs to be a set or converted to a set
# since .issubset() is a set method.
>>> set(mammals).issubset(animals)
True
# Both objects need to be sets to use a set operator
>>> birds <= set(mammals)
False
# A set is always a loose subset of itself.
>>> set(additional_animals) <= set(additional_animals)
True
<set>.issuperset(<other_collection>) معکوس .issubset() است.
از آن برای بررسی اینکه آیا هر عنصر <other_collection> در <set> هم هست استفاده میشود.
شکل عملگری آن <set> >= <other_set> است:
# All members of mammals also appear in animals.
# **Note** The first object needs to be a set or converted to a set
# since .issuperset() is a set method.
>>> set(animals).issuperset(mammals)
True
# All members of animals do not show up as members of birds.
>>> birds.issuperset(animals)
False
# Both objects need to be sets to use a set operator
>>> birds >= set(mammals)
False
# A set is always a loose superset of itself.
>>> set(animals) >= set(animals)
True
<set>.intersection(*<other iterables>) یک set جدید برمیگرداند که عناصر مشترک میان set اصلی و همهی <others> را دارد (به عبارت دیگر، setی که در آن همهچیز اشتراک دارد).
نسخهی عملگری این متد <set> & <other set> & <other set 2> & ... <other set n> است:
>>> perennials = {'Annatto','Asafetida','Asparagus','Azalea',
'Winter Savory', 'Broccoli','Curry Leaf','Fennel',
'Kaffir Lime','Kale','Lavender','Mint','Oranges',
'Oregano', 'Tarragon', 'Wild Bergamot'}
>>> annuals = {'Corn', 'Zucchini', 'Sweet Peas', 'Marjoram',
'Summer Squash', 'Okra','Shallots', 'Basil',
'Cilantro', 'Cumin', 'Sunflower', 'Chervil',
'Summer Savory'}
>>> herbs = ['Annatto','Asafetida','Basil','Chervil','Cilantro',
'Curry Leaf','Fennel','Kaffir Lime','Lavender',
'Marjoram','Mint','Oregano','Summer Savory',
'Tarragon','Wild Bergamot','Wild Celery',
'Winter Savory']
# Methods will take any iterable as an argument.
>>> perennial_herbs = perennials.intersection(herbs)
{'Annatto', 'Asafetida', 'Curry Leaf', 'Fennel', 'Kaffir Lime',
'Lavender', 'Mint', 'Oregano', 'Wild Bergamot','Winter Savory'}
# Operators require both groups be sets.
>>> annuals & set(herbs)
{'Basil', 'Chervil', 'Marjoram', 'Cilantro'}
<set>.union(*<other iterables>) یک set جدید برمیگرداند که شامل عناصر <set> و همهی <other iterables> است.
شکل عملگری این متد <set> | <other set 1> | <other set 2> | ... | <other set n> است:
>>> perennials = {'Asparagus', 'Broccoli', 'Sweet Potato', 'Kale'}
>>> annuals = {'Corn', 'Zucchini', 'Sweet Peas', 'Summer Squash'}
>>> more_perennials = ['Radicchio', 'Rhubarb',
'Spinach', 'Watercress']
# Methods will take any iterable as an argument.
>>> perennials.union(more_perennials)
{'Asparagus','Broccoli','Kale','Radicchio','Rhubarb',
'Spinach','Sweet Potato','Watercress'}
# Operators require sets.
>>> set(more_perennials) | perennials
{'Asparagus',
'Broccoli',
'Kale',
'Radicchio',
'Rhubarb',
'Spinach',
'Sweet Potato',
'Watercress'}
<set>.difference(*<other iterables>) یک set جدید برمیگرداند که شامل عناصری از set اصلی است که در <others> نیستند.
نسخهی عملگری این متد <set> - <other set 1> - <other set 2> - ...<other set n> است.
>>> berries_and_veggies = {'Asparagus',
'Broccoli',
'Watercress',
'Goji Berries',
'Goose Berries',
'Ramps',
'Walking Onions',
'Blackberries',
'Strawberries',
'Rhubarb',
'Kale',
'Artichokes',
'Currants'}
>>> veggies = ('Asparagus', 'Broccoli', 'Watercress', 'Ramps',
'Walking Onions', 'Rhubarb', 'Kale', 'Artichokes')
# Methods will take any iterable as an argument.
>>> berries = berries_and_veggies.difference(veggies)
{'Blackberries','Currants','Goji Berries',
'Goose Berries', 'Strawberries'}
# Operators require sets.
>>> berries_and_veggies - berries
{'Artichokes','Asparagus','Broccoli','Kale',
'Ramps','Rhubarb','Walking Onions','Watercress'}
<set>.symmetric_difference(<other iterable>) یک set جدید برمیگرداند که عناصری را در بر میگیرد که در <set> یا <other> هستند، اما نه در هر دو.
نسخهی عملگری این متد <set> ^ <other set> است:
>>> plants_1 = {'🌲','🍈','🌵', '🥑','🌴', '🥭'}
>>> plants_2 = ('🌸','🌴', '🌺', '🌲', '🌻', '🌵')
# Methods will take any iterable as an argument.
>>> fruit_and_flowers = plants_1.symmetric_difference(plants_2)
>>> fruit_and_flowers
{'🌸', '🌺', '🍈', '🥑', '🥭','🌻' }
# Operators require both groups be sets.
>>> fruit_and_flowers ^ plants_1
{'🌲', '🌸', '🌴', '🌵','🌺', '🌻'}
>>> fruit_and_flowers ^ set(plants_2)
{'🥭', '🌴', '🌵', '🍈', '🌲', '🥑'}
تفاضل متقارن بیش از دو مجموعه به setی میرسد که هم عناصر یکتای هر set و هم عناصری را در بر میگیرد که میان بیش از دو مجموعه در آن سری مشترکاند (جزئیات در مقالهی ویکیپدیا دربارهی تفاضل متقارن).
برای بهدست آوردن فقط عناصر یکتای هر set در آن سری، باید اشتراکهای میان همهی ترکیبهای دوتایی مجموعهها را در مرحلهای جداگانه جمع و سپس حذف کرد:
>>> one = {'black pepper','breadcrumbs','celeriac','chickpea flour',
'flour','lemon','parsley','salt','soy sauce',
'sunflower oil','water'}
>>> two = {'black pepper','cornstarch','garlic','ginger',
'lemon juice','lemon zest','salt','soy sauce','sugar',
'tofu','vegetable oil','vegetable stock','water'}
>>> three = {'black pepper','garlic','lemon juice','mixed herbs',
'nutritional yeast', 'olive oil','salt','silken tofu',
'smoked tofu','soy sauce','spaghetti','turmeric'}
>>> four = {'barley malt','bell pepper','cashews','flour',
'fresh basil','garlic','garlic powder', 'honey',
'mushrooms','nutritional yeast','olive oil','oregano',
'red onion', 'red pepper flakes','rosemary','salt',
'sugar','tomatoes','water','yeast'}
>>> intersections = (one & two | one & three | one & four |
two & three | two & four | three & four)
...
{'black pepper','flour','garlic','lemon juice','nutritional yeast',
'olive oil','salt','soy sauce', 'sugar','water'}
# The ^ operation will include some of the items in intersections,
# which means it is not a "clean" symmetric difference - there
# are overlapping members.
>>> (one ^ two ^ three ^ four) & intersections
{'black pepper', 'garlic', 'soy sauce', 'water'}
# Overlapping members need to be removed in a separate step
# when there are more than two sets that need symmetric difference.
>>> (one ^ two ^ three ^ four) - intersections
...
{'barley malt','bell pepper','breadcrumbs', 'cashews','celeriac',
'chickpea flour','cornstarch','fresh basil', 'garlic powder',
'ginger','honey','lemon','lemon zest','mixed herbs','mushrooms',
'oregano','parsley','red onion','red pepper flakes','rosemary',
'silken tofu','smoked tofu','spaghetti','sunflower oil', 'tofu',
'tomatoes','turmeric','vegetable oil','vegetable stock','yeast'}
شما و شرکای تجاریتان یک شرکت پذیرایی کوچک را اداره میکنید. همین حالا پذیرفتهاید که برای یک باشگاه آشپزی محلی که غذاهای «محبوب باشگاه» را ارائه میدهد، رویدادی برگزار کنید. این باشگاه در میزبانی از رویدادهای بزرگ تجربه ندارد و برای سازماندهی، خرید، آمادهسازی و سرو به کمک نیاز دارد. تصمیم گرفتهاید چند اسکریپت کوچک Python بنویسید تا کل فرایند برنامهریزی را سرعت ببخشید.
دستورپختهای این رویداد از منابع گوناگونی اضافه شدهاند و به نظر میرسد مواد اولیهشان ورودیهای تکراری (یا بیشتر) داشته باشند. شما نمیخواهید در نهایت مواد اضافی بخرید! پیش از آنکه خرید و آشپزی آغاز شود، فهرست مواد اولیهی هر غذا باید «پاکسازی» شود.
تابع clean_ingredients(<dish_name>, <dish_ingredients>) را پیادهسازی کنید که اسم یک غذا و یک list از مواد اولیه را میگیرد.
این تابع باید یک tuple برگرداند که اولین عنصرش اسم غذا است و پس از آن set بدون تکراری مواد اولیه قرار دارد.
>>> clean_ingredients('Punjabi-Style Chole', ['onions', 'tomatoes', 'ginger paste', 'garlic paste', 'ginger paste', 'vegetable oil', 'bay leaves', 'cloves', 'cardamom', 'cilantro', 'peppercorns', 'cumin powder', 'chickpeas', 'coriander powder', 'red chili powder', 'ground turmeric', 'garam masala', 'chickpeas', 'ginger', 'cilantro'])
>>> ('Punjabi-Style Chole', {'garam masala', 'bay leaves', 'ground turmeric', 'ginger', 'garlic paste', 'peppercorns', 'ginger paste', 'red chili powder', 'cardamom', 'chickpeas', 'cumin powder', 'vegetable oil', 'tomatoes', 'coriander powder', 'onions', 'cilantro', 'cloves'})
این رویداد هم Cocktail و هم «Mocktail» را در بر میگیرد، یعنی نوشیدنیهای ترکیبی بدون الکل. باید مطمئن شوید که نوشیدنیهای Mocktail واقعاً بدون الکلاند و Cocktailها واقعاً الکل دارند.
تابع check_drinks(<drink_name>, <drink_ingredients>) را پیادهسازی کنید که اسم یک نوشیدنی و یک list از مواد اولیه را میگیرد.
تابع باید اسم نوشیدنی را همراه با «Mocktail» برگرداند اگر نوشیدنی هیچ مادهی الکلداری ندارد، و اسم نوشیدنی را همراه با «Cocktail» اگر نوشیدنی الکل دارد.
در این تمرین، Cocktailها فقط الکلهای موجود در ثابت ALCOHOLS در sets_categories_data.py را در بر میگیرند:
>>> from sets_categories_data import ALCOHOLS
>>> check_drinks('Honeydew Cucumber', ['honeydew', 'coconut water', 'mint leaves', 'lime juice', 'salt', 'english cucumber'])
...
'Honeydew Cucumber Mocktail'
>>> check_drinks('Shirley Tonic', ['cinnamon stick', 'scotch', 'whole cloves', 'ginger', 'pomegranate juice', 'sugar', 'club soda'])
...
'Shirley Tonic Cocktail'
فهرست مهمانان شامل افرادی با نیازهای غذایی متفاوت است و کارکنانتان باید غذاها را در دستههای Vegan، Vegetarian، Paleo، Keto و Omnivore جدا کنند. یک غذا تنها زمانی به یک دسته تعلق دارد که همهی مواد اولیهاش در مجموعهی مواد اولیهی آن دسته حاضر باشند.
تابع categorize_dish(<dish_name>, <dish_ingredients>) را پیادهسازی کنید که اسم یک غذا و یک set از مواد اولیهی آن غذا را میگیرد.
تابع باید رشتهای به شکل dish name: <CATEGORY> برگرداند (اینکه غذا به کدام دستهی وعده تعلق دارد).
همهی غذاهای دادهشده در یکی از دستههای واردشده از sets_categories_data.py جای میگیرند (VEGAN، VEGETARIAN، PALEO، KETO یا OMNIVORE).
>>> from sets_categories_data import VEGAN, VEGETARIAN, PALEO, KETO, OMNIVORE
>>> categorize_dish('Sticky Lemon Tofu', {'tofu', 'soy sauce', 'salt', 'black pepper', 'cornstarch', 'vegetable oil', 'garlic', 'ginger', 'water', 'vegetable stock', 'lemon juice', 'lemon zest', 'sugar'})
...
'Sticky Lemon Tofu: VEGAN'
>>> categorize_dish('Shrimp Bacon and Crispy Chickpea Tacos with Salsa de Guacamole', {'shrimp', 'bacon', 'avocado', 'chickpeas', 'fresh tortillas', 'sea salt', 'guajillo chile', 'slivered almonds', 'olive oil', 'butter', 'black pepper', 'garlic', 'onion'})
...
'Shrimp Bacon and Crispy Chickpea Tacos with Salsa de Guacamole: OMNIVORE'
برخی مهمانان آلرژی و محدودیتهای غذایی دیگری دارند. این مواد اولیه باید برای هر غذا برچسبگذاری/یادداشت شوند تا مشکلی پیش نیاید.
تابع tag_special_ingredients(<dish>) را پیادهسازی کنید که یک tuple میگیرد که در جایگاه اول اسم غذا و در جایگاه دوم یک list یا set از مواد اولیهی آن غذا قرار دارد.
اسم غذا را همراه با set موادی برگردانید که به یادداشتی ویژه در توضیحات غذا نیاز دارند.
مواد اولیهی یک غذا درون یک list ممکن است تکراری باشند یا نباشند.
برای اهداف این تمرین، همهی آلرژنها یا مواد اولیهی ویژهای که باید برچسبگذاری شوند در ثابت SPECIAL_INGREDIENTS واردشده از sets_categories_data.py قرار دارند.
>>> from sets_categories_data import SPECIAL_INGREDIENTS
>>> tag_special_ingredients(('Ginger Glazed Tofu Cutlets', ['tofu', 'soy sauce', 'ginger', 'corn starch', 'garlic', 'brown sugar', 'sesame seeds', 'lemon juice']))
...
('Ginger Glazed Tofu Cutlets', {'garlic','soy sauce','tofu'})
>>> tag_special_ingredients(('Arugula and Roasted Pork Salad', ['pork tenderloin', 'arugula', 'pears', 'blue cheese', 'pine nuts', 'balsamic vinegar', 'onions', 'black pepper']))
...
('Arugula and Roasted Pork Salad', {'pork tenderloin', 'blue cheese', 'pine nuts', 'onions'})
برای آماده شدن برای سفارش و خرید، باید یک «فهرست اصلی» از مواد اولیهی همهی غذاهای روی منو تهیه کنید (مقدارها بعداً پر میشوند).
تابع compile_ingredients(<dishes>) را پیادهسازی کنید که یک list از غذاها میگیرد و یک مجموعه از همهی مواد اولیهی همهی غذاهای فهرستشده را برمیگرداند.
هر غذا بهصورت set مواد اولیهاش نمایش داده میشود.
dishes = [ {'tofu', 'soy sauce', 'ginger', 'corn starch', 'garlic', 'brown sugar', 'sesame seeds', 'lemon juice'},
{'pork tenderloin', 'arugula', 'pears', 'blue cheese', 'pine nuts',
'balsamic vinegar', 'onions', 'black pepper'},
{'honeydew', 'coconut water', 'mint leaves', 'lime juice', 'salt', 'english cucumber'}]
>>> compile_ingredients(dishes)
...
{'arugula', 'brown sugar', 'honeydew', 'coconut water', 'english cucumber', 'balsamic vinegar', 'mint leaves', 'pears', 'pork tenderloin', 'ginger', 'blue cheese', 'soy sauce', 'sesame seeds', 'black pepper', 'garlic', 'lime juice', 'corn starch', 'pine nuts', 'lemon juice', 'onions', 'salt', 'tofu'}
میزبانان فهرستی از غذاها به شما دادهاند که میخواهند بهشکل پیشغذاهای «اندازهی لقمهای» آماده و در سینی سرو شوند. باید اینها را از فهرست اصلی غذاهایی که بهعنوان وعدههای بزرگتر آماده میشوند جدا کنید.
تابع separate_appetizers(<dishes>, <appetizers>) را پیادهسازی کنید که یک list از اسم غذاها و یک list از اسم پیشغذاها را میگیرد.
تابع باید list اسم غذاها را با حذف اسم پیشغذاها برگرداند.
list مربوط به <dishes> یا <appetizers> ممکن است تکراری داشته باشد و نیاز به حذف تکراریها پیدا کند.
dishes = ['Avocado Deviled Eggs','Flank Steak with Chimichurri and Asparagus', 'Kingfish Lettuce Cups',
'Grilled Flank Steak with Caesar Salad','Vegetarian Khoresh Bademjan','Avocado Deviled Eggs',
'Barley Risotto','Kingfish Lettuce Cups']
appetizers = ['Kingfish Lettuce Cups','Avocado Deviled Eggs','Satay Steak Skewers',
'Dahi Puri with Black Chickpeas','Avocado Deviled Eggs','Asparagus Puffs',
'Asparagus Puffs']
>>> separate_appetizers(dishes, appetizers)
...
['Vegetarian Khoresh Bademjan', 'Barley Risotto', 'Flank Steak with Chimichurri and Asparagus',
'Grilled Flank Steak with Caesar Salad']
در هر دسته (Vegan، Vegetarian، Paleo، Keto، Omnivore)، میخواهید موادی را جدا کنید که فقط در یک غذا ظاهر میشوند. به این مواد «تکنمونه» یک خریدار ویژه اختصاص داده میشود تا مطمئن شوید در شلوغی انجام بقیهی کارها فراموش نمیشوند.
تابع singleton_ingredients(<dishes>, <INTERSECTIONS>) را پیادهسازی کنید که یک list از غذاها و یک ثابت <CATEGORY>_INTERSECTIONS برای همان دسته را میگیرد.
هر غذا بهصورت یک set از مواد اولیهاش نمایش داده میشود.
هر <CATEGORY>_INTERSECTIONS یک set از موادی است که در بیش از یک غذای آن دسته ظاهر میشوند.
تابع شما باید با استفاده از عملگرهای مجموعه، یک set از مواد «تکنمونه» برگرداند (موادی که فقط در یک غذای آن دسته ظاهر میشوند).
from sets_categories_data import example_dishes, EXAMPLE_INTERSECTION
>>> singleton_ingredients(example_dishes, EXAMPLE_INTERSECTION)
...
{'garlic powder', 'sunflower oil', 'mixed herbs', 'cornstarch', 'celeriac', 'honey', 'mushrooms', 'bell pepper', 'rosemary', 'parsley', 'lemon', 'yeast', 'vegetable oil', 'vegetable stock', 'silken tofu', 'tofu', 'cashews', 'lemon zest', 'smoked tofu', 'spaghetti', 'ginger', 'breadcrumbs', 'tomatoes', 'barley malt', 'red pepper flakes', 'oregano', 'red onion', 'fresh basil'}