التكرار على القوائم المتداخلة
يمكنك استخدام حلقة متداخلة للمرور على كل عنصر وكل عنصر داخلي في قائمة متداخلة. انظر مثلًا إلى هذه القائمة:
numbers = [[1, 2, 3], [4, 5], [6], []]
انسخ القائمة إلى المحرر، ثم اكتب الحلقة المتداخلة التالية:
for sublist in numbers:
for num in sublist:
print(num)
print('---')
تأمل الكود جيدًا. الحلقة الخارجية تنشئ المتغير sublist، ثم الحلقة الداخلية تمر على العناصر الموجودة في المتغير نفسه. هذا نمط شائع جدًا. شغّل الكود الآن.
تتبّع تنفيذ الحلقة المتداخلة خطوة بخطوة. راقب كيف تتغير قيمة sublist مع كل دورة من الحلقة الخارجية، وكيف تمر الحلقة الداخلية على عناصر القائمة الفرعية الحالية قبل أن تنتقل الحلقة الخارجية إلى القائمة الفرعية التالية.
لنحل الآن بعض المسائل باستخدام هذا النوع من الحلقات.
لنفترض أن لدينا قائمة متداخلة من النصوص مثل الآتية، ونريد البحث عن كلمة محددة word في أعماق القائمة:
strings = [
[
"hello there",
"how are you",
],
[
"goodbye world",
"hello world",
]
]
word = "hello"
يمكنك تخيل أن strings تمثل كتابًا، وكل قائمة فرعية صفحة، وكل نص داخلها سطرًا في الصفحة. ويمكن أيضًا أن تمثل مكتبة، حيث كل قائمة كتاب وكل نص صفحة.
اكتب برنامجًا يطبع كل نص يحتوي على word. يجب أن يعمل لأي قيمتي word وstrings. في المثال السابق يجب أن يطبع:
hello there hello world تذكّر أن هناك طريقة محددة للتحقق مما إذا كان نص يحتوي على نص آخر.
How do you check if a string contains a word?
Make sure to check whether the string contains the word, not the sublist.
How can you access each string in each sublist of a nested list?
You need to use a nested loop.
The loops should follow the same pattern as the example at the beginning of the page.
جميل!
لنغيّر المسألة قليلًا. هذه المرة نريد أن يخبرنا الناتج أي القوائم الفرعية تحتوي على word، بدل طباعة النصوص الداخلية نفسها. اطبع قيمة منطقية لكل قائمة فرعية: True إذا كانت الكلمة موجودة في أي نص داخلها، وFalse إذا لم تظهر في أي نص.
مع المدخلات التالية:
strings = [
[
"hello there",
"how are you",
],
[
"goodbye world",
"hello world",
]
]
word = "goodbye"
يجب أن يطبع البرنامج:
False
True
لاحظ أن word in sublist لن يحل المسألة. مثلًا التعبير "hello" in ["hello there", "how are you"] يعطي False لأن "hello" ليست مساوية لأي عنصر كامل في القائمة، رغم أنها جزء من أحد النصوص.
For each sublist, define a boolean.
Go through a sublist, update the boolean accordingly.
Only print the boolean once for each sublist.
What should be the initial value for the boolean?
What if one of the sublists is empty? What should you print for that sublist?
If you find the word in a string, the boolean should be set to
True.What if a string doesn't contain the word?
Doesn't matter! It doesn't change whether any other string might contain the word.
In other words, don't set the boolean to
Falseexcept at the beginning.
أحسنت!
الآن اطبع قيمة منطقية واحدة فقط تخبرنا هل word موجودة في أي نص في القائمة المتداخلة كلها. مثلًا إذا كانت القيم:
strings = [
[
"hello there",
"how are you",
],
[
"goodbye world",
"hello world",
]
]
word = "Python"
فيجب أن يطبع البرنامج False.
This is very similar to the previous exercise.
When should you print the boolean?
Remember you want to print it only once.
Instead of defining a boolean for each sublist, define only one boolean for the entire list.
When and how should you modify the boolean?
في جزء سابق من الدورة كان هناك تمرين لطباعة نصين رأسيًا جنبًا إلى جنب، مثل:
H W e o l r l l o d الآن سنعمم الفكرة على قائمة من النصوص بدل نصين فقط.
في هذا التمرين تحصل على قائمة من النصوص متساوية الطول. اكتب برنامجًا يطبع الحرف الأول من كل نص في سطر واحد، ثم الحرف الثاني من كل نص في السطر التالي، وهكذا. مثلًا إذا كانت:
strings = ["abc", "def", "ghi"] فيجب أن يطبع:
adg beh cfi يجب أن يعمل البرنامج لأي قائمة تحقق هذا الشرط. وإذا استخدمت القائمة التالية فستكتشف رسالة مخفية من Zen of Python:
strings = [" b n", "f ete", "liths", "astat", "t ene", " r d"]
هذه المرة لا تضف مسافات بين الحروف في الناتج.
This is NOT similar to the previous exercises on this page.
Think about the solution when there's just two strings. How can you generalize it to a list of strings?
You'll need to go through the first letters, then the second letters, and so on.
You'll have to use a loop, but how long should the loop take?
تذكر أن strings in the list have equal lengths.
For each position (first, second etc.) define a new string.
What should that string be initially?
For each position (first, second etc.) you'll have to go through each string in the list.
You'll need another loop inside the one you have.
أحسنت! يمكنك الانتقال إلى الصفحة التالية الآن، أو حل تحدٍّ إضافي.
لنطوّر التمرين السابق بحيث يعمل حتى عندما تكون النصوص داخل القائمة مختلفة الأطوال.
مرّ على المواضع من الأول حتى طول أطول نص. في كل موضع، أضف حرف النص إذا كان ذلك النص يحتوي على حرف في هذا الموضع، وإلا أضف مسافة. ثم اطبع السطر الناتج. بهذه الطريقة تحافظ على محاذاة النصوص حتى عندما ينتهي بعضها قبل الآخر.
Since the strings can have different lengths, this is a bit tricky.
For how long should your outer loop go this time?
Before you start handling the strings, it might be a good idea to find the longest string length first.
The rest is very similar to the previous exercise.
The only difference is that now you have to determine whether to add a letter from a string, or a space.
أتقنت القوائم المتداخلة وكيفية دمجها مع الحلقات المتداخلة. لديك الآن أدوات قوية جدًا لبناء برامج أكثر تعقيدًا.