مقدمة إلى القوائم (Lists)
حان وقت التعرف على نوع قوي جديد من القيم يسمى القوائم (Lists). إليك مثالًا:
words = ['This', 'is', 'a', 'list']
for word in words:
print(word)
القائمة هي تسلسل، أي مجموعة مرتبة يمكن أن تحتوي على أي عدد من القيم. وتسمى القيم الموجودة داخلها غالبًا عناصر (Elements).
يمكن أن تكون العناصر أي شيء: أعدادًا، نصوصًا، قيمًا منطقية، وحتى قوائم أخرى. ويمكن أيضًا أن تجمع القائمة عناصر من أنواع مختلفة.
لإنشاء قائمة مباشرة كما في المثال السابق:
- اكتب قوسين مربعين:
[]. - إذا لم تكن تريد قائمة فارغة، فاكتب التعبيرات التي ستكون عناصر داخل القوسين.
- ضع فاصلة
,بين العناصر للفصل بينها.
إليك مثالًا آخر لإنشاء قائمة:
x = 1
things = ['Hello', x, x + 3]
print(things)
كما رأيت، القوائم قابلة للتكرار عليها (Iterable)، أي يمكنك المرور على عناصرها باستخدام حلقة for. إليك برنامجًا يجمع كل الأعداد الموجودة داخل قائمة:
numbers = [3, 1, 4, 1, 5, 9]
total = 0
for number in numbers:
total += number
print(total)
عدّل البرنامج الآن بحيث يستطيع جمع قائمة من النصوص بدلًا من الأعداد. مثلًا، إذا كانت:
words = ['This', 'is', 'a', 'list'] فيجب أن يطبع:
Thisisalist
This is very similar to the exercises you've done building up strings character by character.
The solution is very similar to the program that adds numbers.
In fact, what happens if you try running that program with a list of strings?
The problem is that 0. You can't add 0 to a string because numbers and strings are incompatible.
Is there a similar concept among strings to 0? A blank initial value?
ممتاز!
يمكنك المتابعة إلى الدرس التالي الآن إذا أردت.
أما التحدي الإضافي الاختياري فهو توسيع البرنامج بحيث يضع نصًا فاصلًا بين كل كلمتين. فإذا كانت القيم:
words = ['This', 'is', 'a', 'list'] separator = ' - ' فيجب أن يكون الناتج:
This - is - a - list
This is similar to the previous exercise. You can start with your solution from that.
This exercise doesn't require anything fancy and the final solution can be quite simple. But it's tricky to get it right and you need to think about the approach carefully.
In each iteration, in addition to a word in the list, you also have to add the separator.
But you don't want to add the separator after adding the last word in the list.
Unfortunately there is no "subtraction" with strings; you can't add the last separator then remove it.
Let's back up. The final result should contain each word, and
n - 1separators, wherenis the number of words.So you want to add a separator in every iteration except one.
You can skip adding the separator in one particular iteration using an
ifstatement.Later on you will learn a way to iterate over a list and check if you're in the last iteration, but right now you have no way of doing that.
However, the iteration you skip doesn't have to be the last one!
You can write a program that checks if you're in the first iteration of a loop.
Just make a boolean variable to keep track of this. No need for any comparison operators or numbers.
We looked at programs that did something like this here.
So if you only skip adding the separator in the first iteration, you will have
n - 1separators. Now you just need to think carefully about how to make sure the separators are in the right place.Forgetting the loop for a moment, you need to add the following to the string in this order: the first word, the separator, the second word, the separator, the third word, etc.
That means that in the first iteration, you just add the first word. In the second iteration, you add the separator, then the second word. In the third iteration, you add the separator, then the third word. And so on.
So inside your loop, add the separator first, add the word after.
Skip adding the separator in the first iteration by checking a boolean variable.
Create the boolean variable before the loop, then change it inside the loop.
Only change it in the loop after checking it, or you won't be able to skip the first iteration.
تهانينا! كان هذا التمرين صعبًا فعلًا. أحد الحلول الممكنة:
words = ['This', 'is', 'a', 'list'] separator = ' - ' total = '' not_first = False
for word in words: if not_first: total += separator total += word not_first = True
print(total)