أساليب متقدمة للتعامل مع القوائم
إليك بعض الدوال والأساليب المفيدة الأخرى.
sorted: تستقبل شيئًا قابلًا للتكرار عليه وتُرجع قائمة بالعناصر مرتبة من الأصغر إلى الأكبر، بالصيغة:
sorted(some_list) جرّب مثلًا هذا السطر في الطرفية:
sorted([2, 9, 1, 8, 5, 6])
in: معامل مقارنة يتحقق مما إذا كانت قيمة موجودة داخل قائمة، بالصيغة:
value in some_list شغّل المثال التالي:
nums = [2, 9, 1, 8, 5, 64]
print(7 in nums)
print(2 in nums)
sum: تجمع عناصر شيء قابل للتكرار يحتوي على أعداد، بالصيغة:
sum(some_list) شغّل المثال التالي:
sum([5, 3, 4])
count: تُرجع عدد مرات ظهور قيمة معينة داخل القائمة، بالصيغة:
some_list.count(value) جرّب هذا السطر في الطرفية:
[1, 2, 3, 2, 7, 2, 5].count(2)
قد تتعرف على بعض هذه الأفكار من التمارين السابقة. تلك التمارين لم تكن بلا فائدة؛ فقد تعلمت من خلالها مهارات أساسية مهمة. مثلًا، يمكنك استخدام in للتحقق بسهولة من وجود العدد 5 في قائمة، لكن لا توجد صيغة بسيطة مماثلة تسأل مباشرة: «هل يوجد عدد أكبر من 5؟».
ستحل الآن أربعة تمارين جديدة على المفاهيم السابقة. مرة أخرى، توجد أسطر صحيحة وخاطئة مختلطة، وعليك اختيار السطر المناسب من القائمة:
sum(len(x)) sum(range(x)) sum(range(len(x))) sum(len(range(x))) sum(range(x)) + 1 sum(range(x + 1)) sum(x) / len(x) sum(x) / range(x) sum(x) / range(len(x)) sum(x) / len(range(x)) sorted(x)[1] sorted(x)[2] sorted(x)[-1] sorted(x)[-2] x.count(1) >= 0 x.count(1) > 0 x.count(1) > 1 إليك البرنامج:
x = [1, 2, 0, 3]
y = 1 in x
print(y)
استبدل الجزء 1 in x، مع إبقاء y = ، بسطر واحد من القائمة السابقة يؤدي الوظيفة نفسها.
Your solution should have exactly three statements:
x = ['a', 'b', 'c'],y =followed by one line copied exactly from the list, andprint(y).When is
1 in xTrue?When
1is inx!Could be that
1is inxonce, or twice, or three times......but not zero times!
عمل ممتاز! في التمرين التالي ابدأ بهذا البرنامج غير المكتمل:
x = [15, 12, -6, 3]
y = (insert_one_line_from_above)
print(y)
استبدل الجزء الذي يأتي بعد y = بسطر واحد من القائمة السابقة. يجب أن يطبع البرنامج النهائي متوسط الأعداد في x، أو ما يسمى رياضيًا بالمتوسط الحسابي (Mean).
Your solution should have exactly three statements:
x = [15, 12, -6, 3],y =followed by one line copied exactly from the list, andprint(y).If you're not sure, look up how to calculate the average/mean.
To calculate the average of numbers in
xwe need two things.Which two functions/methods give you those two things?
How do you combine those two things to calculate the average?
أحسنت! في التمرين التالي ابدأ بهذا البرنامج:
x = 100
y = (insert_one_line_from_above)
print(y)
استبدل الجزء بعد y = بسطر واحد من القائمة السابقة. يجب أن يطبع البرنامج مجموع جميع الأعداد من 1 إلى x شاملًا، أي 1 + 2 + 3 + ... + x.
Your solution should have exactly three statements:
x = 100,y =followed by one line copied exactly from the list, andprint(y).What function/method can be used to add up things?
Which function/method gives us the numbers
1, 2, 3, ..., x?You have to make a small tweak, otherwise that last number
xwill be left out.
ممتاز. وهذا هو التمرين الأخير:
x = [12, -6, 2, -1, 3]
y = (insert_one_line_from_above)
print(y)
استبدل الجزء بعد y = بسطر من القائمة السابقة. يجب أن يطبع البرنامج ثاني أصغر قيمة في x.
Your solution should have exactly three statements:
x = [12, -6, 2, -1, 3],y =followed by one line copied exactly from the list, andprint(y).The numbers in
xseem to be all out of order. Can you do something about that?If you figured that part out, try using that function in the shell to play around with it.
How would you use that function to get the smallest value in a list? What about the biggest?
After that, how can you get the second smallest value?
تهانينا! أصبحت الآن متمكنًا من أهم دوال وأساليب القوائم.