المجموعات
مُقدَّمفي هذا الدرس
| English | المعنى |
|---|---|
| list/lɪst/ | قائمة |
| dictionary/ˈdɪkʃənəri/ | مفردات |
| key/kiː/ | مفتاح |
| tuple/ˈtuːpl/ | زوجة |
| set/set/ | مجموعة |
| aliasing/ˈeɪlɪəsɪŋ/ | استخدام اسم مستعار |
Which drink sells best?
- A café till records every order:
"tea","latte","tea"… 200 times a day. One variable per order would be useless. You need one name that holds many values. - Python has four built-in collections, and each one suits a different job. Choosing the right one often makes the rest of the program short.
A list keeps items in order
- A list 列表 is an ordered sequence that you can change:
marks = [72, 55, 90, 48, 81]. Positions are counted from 0, somarks[0]is72andmarks[-1], the last item, is81. - A slice takes a part:
marks[1:3]is[55, 90], from position 1 up to, but not including, position 3.len(marks)is5.
مع s = [10, 20, 30, 40, 50]، ما هي قيمة s[1:4]؟
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
يحتل الموقع 1 القيمة 20 (بالعد بدءًا من 0)، ويتوقف الشق قبل الموقع 4، لذا يأخذ المواقع 1 و2 و3.
Changing a list
marks.append(66)adds to the end;marks.remove(48)deletes the first48;marks[1] = 58replaces one item.marks.sort()sorts the list in place and returnsNone. To keep the original and get a sorted copy, writesorted(marks).
nums = [3, 1, 2]
result = nums.sort()
print(result, nums) # None [1, 2, 3]
بعد nums = [3, 1, 2] وnums = nums.sort()، ما هي nums؟
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
sort() يرتب القائمة محلياً ويعيد None، وهذا None هو ما يتم تخزينه في nums. اكتب nums.sort() بمفرده، أو nums = sorted(nums).
A dictionary looks things up by key
- A dictionary 字典 maps each key 键 to a value:
stock = {"tea": 40, "latte": 25}.stock["tea"]is40. - A missing key raises a
KeyError.stock.get("mocha", 0)returns the default0instead, so it is safe for counting.
- Count how many times each drink was ordered.
- Start with
tally = {}. For eachitemin the orders, writetally[item] = tally.get(item, 0) + 1. - For
["tea", "flat white", "tea", "latte", "tea"]the result is{'tea': 3, 'flat white': 1, 'latte': 1}. The first sighting of a drink gets0 + 1; later sightings add one more.
المجموع الكلي (Tally) عبارة عن قاموس
كل مفتاح يمثل مشروبًا وكل قيمة تمثل عدد الطلبات. مر على الأزواج؛ والبحث عن tally["tea"] يؤدي مباشرة إلى قيمته دون فحص البقية.
حلقة العد تتكرر عبر ["tea", "latte", "tea", "mocha", "tea", "latte"]. ما هي قيمة tally["tea"] في النهاية؟
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
"شاي" تظهر ثلاث مرات. في المرة الأولى tally.get("tea", 0) + 1 تعطي 1؛ كل ظهور لاحق يضيف واحداً أكثر.
Tuples and sets
- A tuple 元组 is a fixed group, such as a point
p = (3, 4). You can unpack it,x, y = p, butp[0] = 5raises aTypeError: it cannot change. - A set 集合 holds each value once, in no fixed order:
{"tea", "latte", "tea"}has only2items. Use it to remove duplicates or to test membership quickly.
Choose the collection by the job
- Order matters, or you need positions → list. Look up a value by a name or code → dictionary.
- A fixed group of parts, such as a date or a point → tuple. Only "is it there?" and no duplicates → set.
طابق كل وظيفة بأفضل مجموعة.
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
الترتيب → قائمة؛ البحث بالمفتاح → قاموس؛ الأجزاء الثابتة → زوج مرتب؛ التميز → مجموعة.
Two names, one list
b = adoes not copy a list. It puts a second name on the same list, sob.append(2)changes whatashows too. This sharing is called aliasing 别名.- When you need an independent copy, make one:
b = a[:]orb = list(a).

بعد a = [1, 2]، b = a، b.append(3)، ما هو a؟
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
b = a وضع اسماً ثانياً على نفس القائمة، لذا فإن التغيير الذي تم عبر b يظهر عبر a. مع b = a[:]، سيبقى a [1, 2].
Don't remove items while you loop over them
- Removing from a list while a
forloop walks through it skips items: each removal shifts the later items left, but the loop still moves on to the next position. - With
m = [40, 45, 70, 30], removing every mark below 50 inside the loop leaves[45, 70], and45is missed. Build a new list instead:[x for x in m if x >= 50]gives[70].
إزالة كل عنصر أقل من 50 داخل for x in m: تترك دائماً فقط عناصر 50 أو أكثر.
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
كل إزالة تحرك العناصر اللاحقة لليسار، مما يجعل الحلقة تفوت عنصراً واحداً. [40, 45, 70, 30] ينتهي كـ [45, 70]. قم ببناء قائمة جديدة باستخدام [x for x in m if x >= 50].
Marks that slip away
- Counting from 1. The first item is
[0]; the last is[len(x) - 1]or[-1]. x = x.sort().sort()returnsNone, soxis lost. Callx.sort()alone, or usesorted(x).d[key]for a key that may be missing. Used.get(key, default)or testif key in d:first.- Thinking
b = acopies. It shares; copy witha[:]when you need two lists.
d = {"tea": 2}. أي الأسطر تعمل دون رفع خطأ؟ حدد جميع الخيارات المناسبة.
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
قراءة مفتاح مفقود مع d["mocha"] يرفع KeyError. .get يعيد القيمة الافتراضية، in يعيد False، وتعيين مفتاح جديد يضيفه ببساطة.
You've got it
- list: ordered and changeable, positions from 0, slices
[a:b]stop beforeb. dictionary: key → value,.get(key, default)never raises. - tuple: a fixed group you can unpack. set: unique values, fast membership tests.
| list | dict | tuple | set | |
|---|---|---|---|---|
| Ordered | yes | yes | yes | no |
| Can change | yes | yes | no | yes |
| Repeats | yes | in values | yes | no |
b = ashares one list (aliasing); copy witha[:]. Never remove from a list you are looping over; build a new one.
ما المشكلة؟ لا يرى ذلك سوى فريق الموقع.