مراجعة: المجموعات في بايثون - “عالم العناصر الفريدة” 
العربية
:
هل سبق لك أن كنت في حفلة ووجدت نفسك تبحث عن الأشخاص الفريدين والمميزين؟ في بايثون، المجموعات (Sets) هي مثل تلك الحفلة، حيث تحتوي المجموعة على عناصر فريدة ولا يُسمح بتكرارها!
ما هي المجموعات (Sets)؟
- المجموعات (Sets) هي هيكل بيانات غير مرتبة من العناصر الفريدة.
- الفريدة (Unique): لا يمكن أن تحتوي المجموعات على عناصر مكررة.
- غير مرتبة (Unordered): لا يوجد ترتيب للعناصر داخل المجموعة، وبالتالي لا يمكنك الوصول إلى عنصر بناءً على موقعه (index).
مثال بسيط:
# إنشاء مجموعة بسيطة
فواكه = {"تفاح", "برتقال", "موز", "تفاح"} # محاولة تكرار "تفاح"
print(فواكه) # النتيجة: {'تفاح', 'برتقال', 'موز'}
كما ترى، التكرار غير مسموح به! المجموعة احتفظت فقط بالعناصر الفريدة.
كيفية إضافة وإزالة العناصر في المجموعات؟
- يمكنك إضافة عناصر باستخدام
add()
أو إزالة عناصر باستخدامremove()
وdiscard()
.
إضافة وإزالة مثال:
# إضافة عنصر جديد
فواكه.add("عنب")
print(فواكه) # النتيجة: {'تفاح', 'برتقال', 'موز', 'عنب'}
# إزالة عنصر باستخدام remove
فواكه.remove("برتقال")
print(فواكه) # النتيجة: {'تفاح', 'موز', 'عنب'}
# إزالة عنصر باستخدام discard (بدون خطأ إذا لم يكن العنصر موجودًا)
فواكه.discard("موز")
print(فواكه) # النتيجة: {'تفاح', 'عنب'}
التأكد من وجود عنصر معين
- للتحقق مما إذا كان عنصر معين موجودًا في المجموعة، يمكنك استخدام المشغل
in
.
مثال:
if "تفاح" in فواكه:
print("تفاح موجود!")
else:
print("تفاح غير موجود!")
العمليات الرياضية على المجموعات
تتيح لك المجموعات إجراء عمليات رياضية مثل الاتحاد (Union)، التقاطع (Intersection)، و الفرق (Difference). هذه العمليات مفيدة جدًا عند التعامل مع مجموعات كبيرة من البيانات.
مثال على العمليات الرياضية:
مجموعة_أ = {1, 2, 3, 4}
مجموعة_ب = {3, 4, 5, 6}
# الاتحاد
الاتحاد = مجموعة_أ.union(مجموعة_ب)
print(الاتحاد) # النتيجة: {1, 2, 3, 4, 5, 6}
# التقاطع
التقاطع = مجموعة_أ.intersection(مجموعة_ب)
print(التقاطع) # النتيجة: {3, 4}
# الفرق
الفرق = مجموعة_أ.difference(مجموعة_ب)
print(الفرق) # النتيجة: {1, 2}
متى تستخدم المجموعات (Sets)؟
- التفرد: عندما تحتاج إلى مجموعة من العناصر بدون تكرار.
- عمليات رياضية: إذا كنت بحاجة إلى تنفيذ عمليات رياضية على مجموعات من البيانات مثل الاتحاد أو التقاطع.
- الفحص السريع: المجموعات أسرع في الفحص إذا كنت بحاجة إلى معرفة ما إذا كان عنصر معين موجودًا في المجموعة.
مثال لاستخدام المجموعات في الفحص السريع:
الطلاب = {"أحمد", "سارة", "محمد", "ليلى"}
if "محمد" in الطلاب:
print("محمد مسجل في الفصل.")
بعض الخصائص المفيدة للمجموعات:
- len(): لمعرفة عدد العناصر في المجموعة.
- clear(): لحذف جميع العناصر من المجموعة.
- copy(): لنسخ مجموعة جديدة من المجموعة الحالية.
مثال:
print(len(الطلاب)) # النتيجة: 4
طلاب_جدد = الطلاب.copy()
print(طلاب_جدد) # نسخة من المجموعة الأصلية
طلاب_جدد.clear()
print(طلاب_جدد) # النتيجة: مجموعة فارغة set()
الخلاصة:
المجموعات في بايثون هي أدوات قوية للحفاظ على تفرد البيانات، وإجراء العمليات الرياضية، والتأكد من وجود العناصر بسرعة كبيرة. إذا كنت بحاجة إلى التعامل مع بيانات فريدة أو تحتاج إلى عمليات مثل التقاطع أو الاتحاد، فالمجموعات هي الخيار المثالي!
English Version:
Today’s Review: Sets in Python - “The World of Unique Elements” 
Have you ever been at a party looking for unique and interesting people? In Python, Sets are like that party, where only unique elements are allowed, and no duplicates!
What are Sets?
- Sets are an unordered collection of unique elements.
- Unique: A set cannot contain duplicate items.
- Unordered: There is no order to the elements in a set, so you can’t access an element by index.
Simple Example:
# Creating a simple set
fruits = {"apple", "orange", "banana", "apple"} # Trying to repeat "apple"
print(fruits) # Output: {'apple', 'orange', 'banana'}
As you can see, duplicates are not allowed! The set kept only the unique elements.
How to Add and Remove Elements in Sets?
- You can add elements using
add()
or remove elements usingremove()
anddiscard()
.
Adding and Removing Example:
# Adding a new element
fruits.add("grape")
print(fruits) # Output: {'apple', 'orange', 'banana', 'grape'}
# Removing an element using remove
fruits.remove("orange")
print(fruits) # Output: {'apple', 'banana', 'grape'}
# Removing an element using discard (no error if the element is not present)
fruits.discard("banana")
print(fruits) # Output: {'apple', 'grape'}
Checking for the Existence of an Element
- To check if a certain element is present in the set, you can use the
in
operator.
Example:
if "apple" in fruits:
print("Apple is in the set!")
else:
print("Apple is not in the set!")
Mathematical Operations on Sets
Sets allow you to perform mathematical operations like Union, Intersection, and Difference. These operations are very useful when dealing with large sets of data.
Example of Mathematical Operations:
set_a = {1, 2, 3, 4}
set_b = {3, 4, 5, 6}
# Union
union = set_a.union(set_b)
print(union) # Output: {1, 2, 3, 4, 5, 6}
# Intersection
intersection = set_a.intersection(set_b)
print(intersection) # Output: {3, 4}
# Difference
difference = set_a.difference(set_b)
print(difference) # Output: {1, 2}
When to Use Sets?
- Uniqueness: When you need a collection of elements with no duplicates.
- Mathematical Operations: When you need to perform operations like union or intersection on sets of data.
- Fast Membership Check: Sets are faster when you need to check if an element exists in the collection.
Example of Fast Membership Check:
students = {"Ahmed", "Sara", "Mohammed", "Laila"}
if "Mohammed" in students:
print("Mohammed is enrolled in the class.")
Some Useful Set Properties:
- len(): To find the number of elements in the set.
- clear(): To remove all elements from the set.
- copy(): To make a copy of the current set.
Example:
print(len(students)) # Output: 4
new_students = students.copy()
print(new_students) # Copy of the original set
new_students.clear()
print(new_students) # Output: set()
Conclusion:
Sets in Python are powerful tools for maintaining unique data, performing mathematical operations, and checking for the presence of elements quickly. If you need to work with unique data or perform operations like intersection or union, sets are the perfect choice!