• 1
    إدخال وإخراج البيانات
    • المهام
  • 2
    الشروط
    • المهام
  • 3
    حلقة for
    • المهام
  • 4
    السلاسل النصية
    • المهام
  • 5
    حلقة while
    • المهام
  • 6
    القوائم
    • المهام
  • 7
    مصفوفات ثنائية الأبعاد
    • المهام
  • 8
    القواميس
    • المهام
  • 9
    المجموعات
    • المهام
  • 10
    الدوال والتكرار
    • المهام
  • к

درس 9. Sets

الصعوبة:

иконка человека красный иконка человека белая иконка человека зеленая Pythonlib

مسألة«تحليل القاعدة»

💻 Python
يريد مركز اللياقة البدنية تحليل قاعدة عملائه من أجل تحديد العملاء الذين يستوفون معايير معينة للإعلانات المستهدفة. لديك بيانات عن العملاء مقدمة في شكل مجموعة من الخصائص. من الضروري تطوير برنامج يسمح لك بالعثور على العملاء الذين يستوفون عدة شروط محددة في نفس الوقت.

تنسيق الإدخال

في السطر الأول الرقم & nbsp; n & nbsp;- إجمالي عدد العملاء.
بعد ذلك، لكل عميل (إجمالي & nbsp; n & nbsp; سطر) يتم إدخال معلومات عنه بالتنسيق التالي: & nbsp; id age gender subscription interest1 interest2 ... interests .
id & nbsp;- معرف فريد للعميل (عدد صحيح).
age & nbsp;- عمر العميل (عدد صحيح).
gender & nbsp;- جنس العميل ('m' للذكر، 'f' للأنثى).
subscription & nbsp;- وجود اشتراك ('y' لنعم، 'n' للا).
interest1 interest2 ... interests & nbsp;- قائمة اهتمامات العميل (سلاسل مفصولة بمسافات). يمكن أن يختلف عدد الاهتمامات لكل عميل.

بعد إدخال بيانات العملاء، يتم إدخال الرقم & nbsp; m & nbsp;- عدد معايير البحث.
بعد ذلك، لكل معيار (إجمالي & nbsp; m & nbsp; سطر) يتم إدخال المعيار بالتنسيق & nbsp; type value . حيث & nbsp; type & nbsp;- نوع المعيار (واحد مما يلي: & nbsp; age , & nbsp; gender , & nbsp; subscription , & nbsp; interest ).
value & nbsp;- قيمة المعيار. للنوع & nbsp; age & nbsp; هو عدد صحيح، للنوع & nbsp; gender & nbsp;- 'm' أو 'f'، للنوع & nbsp; subscription & nbsp;- 'y' أو 'n'، للنوع & nbsp; interest & nbsp;- سلسلة نصية.

تنسيق الإخراج

عرض قائمة بمعرفات العملاء التي تستوفي جميع المعايير المحددة، بطريقة مرتبة مفصولة بمسافات. إذا لم يكن هناك عميل يستوفي المعايير، اعرض "none"

مثال

إدخال

5
1 25 m Y لياقة سباحة
2 30 f n يوجا بيلاتس
3 25 f Y رقص يوجا
4 40 m y لياقة ملاكمة
5 30 m n سباحة ملاكمة 3
العمر 25
بول f
الاهتمام يوجا

إخراج

3

تلميح

Comparing sets

Subset

A set A is a subset of set B if all elements of set A, without exception, are also contained in set B. The issubset() method is used to check this condition. and the operator .

A useful tip: The <= operator checks for a "non-strict" subset. This means that any set is a subset of itself (set1<= set1 returns True). If you need to check that the set A is a subset of B, but it is not equal to it, use the strict inequality operator .

set1 = {1, 2}
set2 = {1, 2, 3}
set3 = {1, 2}

# Checking for a subset
print(set1.issubset(set2))  # Output: True
print(set1 <= set2) # Output: True

# Checking for a strict subset
print(set1 < set2) # Output: True
print(set3< set1) # Output: False, since the sets are equal

Superset

A set A is a superset of set B if it contains all the elements that are in set B. The issuperset() method is used for verification. and the operator =.

A useful tip: Similarly to subsets, the = operator checks for a "non-strict" superset. To check for a strict superset (A contains all the elements of B and at least one additional one), use the operator.

set1 = {1, 2, 3}
set2 = {1, 2}

# Checking for a superset
print(set1.issuperset(set2))  # Output: True
print(set1 >= set2) # Output: True

# Checking for a strict superset
print(set1 > set2) # Output: True
print(set1 > {1, 2, 3}) # Conclusion: False, since the sets are equal

Equality of sets

Sets are considered equal if they contain exactly the same set of elements. The order of the elements does not matter at all. The == operator is used for verification.

set1 = {1, 2, 3}
set2 = {3, 2, 1}
set3 = {1, 2, 3, 3} # The duplicate will be ignored when creating a set.

print(set1 == set2) # Output: True
print(set1 == set3) # Output: True

Other set methods

Copying a set

The copy() method returns a surface copy of the set.

Important advice: Do not confuse copying with assignment. If you write set2 = set1, then set2 will not be a new set, but only another reference to set1. Changing set2 will change set1. Use copy() to create an independent array.

set1 = {1, 2, 3}

# Correct copying
set2 = set1.copy()
set2.add(4)
print(f"Original set1: {set1}")  # Output: Original set1: {1, 2, 3}
print(f"Copy of set2: {set2}")    # Output: A copy of set2: {1, 2, 3, 4}

# Incorrect copying (link assignment)
set3 = set1
set3.add(5)
print(f"Original set1 after changing set3: {set1}") # Output: Original set1 after changing set3: {1, 2, 3, 5}
print(f"Reference set3: {set3}")                       # Output: set3 link: {1, 2, 3, 5}

Updating the set

The update() method adds to the current set all the elements from another set or any other iterable object (for example, a list or a tuple). The method modifies the original set and returns nothing (None).

set1 = {1, 2, 3}
set2 = {3, 4, 5}
list1 = [5, 6, 7]

set1.update(set2) # Duplicate {3} will be ignored
print(set1) # Output: {1, 2, 3, 4, 5}

set1.update(list1) # You can update it from the list
print(set1) # Output: {1, 2, 3, 4, 5, 6, 7}

Sets and their applications

Arrays are extremely useful in various tasks due to their speed and unique properties. They are ideal for removing duplicates, quickly checking for an element, and performing mathematical operations (union, intersection, difference).

Removing duplicates from the list

This is one of the most common uses of arrays. Converting a list to a set automatically removes all duplicates. Then, if necessary, the set can be converted back to a list.

It is important to remember: With this transformation, the original order of the list items is not preserved.

numbers = [1, 2, 2, 3, 4, 4, 5, 1]
unique_numbers = list(set(numbers))
print(unique_numbers) # The output can be [1, 2, 3, 4, 5] ( the order is not guaranteed)

Search for common items in two lists

To find the elements that are present in both collections, you can transform them into sets and use the intersection operator &.

list1 = [1, 2, 3, 4]
list2 = [3, 4, 5, 6]
common_elements = list(set(list1) & set(list2))
print(common_elements)  # Output: [3, 4] (order is not guaranteed)

Search for unique elements (symmetric difference)

To find the elements that are in one of the lists, but not both at once, use the symmetric difference operator ^.

list1 = [1, 2, 3, 4]
list2 = [3, 4, 5, 6]
unique_to_each_list = list(set(list1) ^ set(list2))
print(unique_to_each_list) # Output: [1, 2, 5, 6] ( the order is not guaranteed)
main.py
اختبار 1
اختبار 2
اختبار 3
اختبار 4
اختبار 5
اختبار 6
اختبار 7
اختبار 8
اختبار 9
اختبار 10
حل المطور

🎉 تهانينا! 🎉

لقد حللت المهمة ببراعة! كان صعبًا لكنك وجدت الحل الصحيح. أنت خطوة أقرب إلى الاحتراف! استمر، كل مرحلة تجعلك أقوى.

AD

إعلان

red-snake blue-snake green-snake

جارٍ تشغيل الكود...

مساعد الذكاء الاصطناعي

مرحباً! أنا مساعدك في البرمجة. اسألني أي شيء عن Python — يمكنني شرح الدوال والطرق ومساعدتك في المهمة الحالية!