logo

كيفية التكرار من خلال قائمة في بايثون

تعد القوائم إحدى هياكل البيانات الأكثر استخدامًا في بايثون. نحن نستمر في استخدام القوائم في العديد من التطبيقات المختلفة، بدءًا من حل المشكلات البسيطة وحتى المشكلات المعقدة. في بايثون، تحل القوائم محل المصفوفات بمزايا مثل:

  1. ديناميكية في الحجم
  2. يمكن تخزين عناصر من أنواع بيانات مختلفة في قائمة واحدة

يمكننا الوصول إلى البيانات ببساطة من القوائم حسب الطلب؛ على عكس المجموعات، ستكون البيانات غير مرتبة. للوصول إلى البيانات، يمكننا استخدام عدة طرق للتكرار من خلال كل عنصر داخل القائمة. يغطي هذا البرنامج التعليمي جميع الطرق مع الأمثلة.

1. الحلقات

    باستخدام أثناء الحلقة:
 list1 = [3, 5, 7, 2, 4] count = 0 while (count <len(list1)): 1 print (list1 [count]) count="count" + < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/python-tutorial/43/how-iterate-through-list-python.webp" alt="How to Iterate through a List in Python"> <p> <strong>Understanding:</strong> </p> <p>We created a list with a few elements. Initially, count = 0. We&apos;re printing the element at the &apos;count&apos; index and incrementing the count in the while loop. When the count reaches the length of the list, the loop will be terminated, and all the elements will already be accessed.</p> <p> <strong>Mechanism:</strong> </p> <table class="table"> <tr> <td>count = 0</td> <td>list1 [0]</td> <td>3</td> </tr> <tr> <td>count = 1</td> <td>list1 [1]</td> <td>5</td> </tr> <tr> <td>count = 2</td> <td>list1 [2]</td> <td>7</td> </tr> <tr> <td>count = 3</td> <td>list1 [3]</td> <td>2</td> </tr> <tr> <td>count = 4</td> <td>list1 [4]</td> <td>4</td> </tr> <tr> <td>count = 5 = len (list1)</td> <td>-</td> <td>-</td> </tr> </table> <ul> <tr><td>Using for loop:</td>  </tr></ul> <pre> list1 = [3, 5, 7, 2, 4] for i in list1: print (i) </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/python-tutorial/43/how-iterate-through-list-python-2.webp" alt="How to Iterate through a List in Python"> <p> <strong>Understanding:</strong> </p> <p>Using for-in, we accessed all the i&apos;s, the elements inside the list.</p> <ul> <tr><td>Using for and range:</td>  </tr></ul> <pre> list1 = [3, 5, 7, 2, 4] length = len (list1) for i in range (0, len (list1)): print (list1 [i]) </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/python-tutorial/43/how-iterate-through-list-python-3.webp" alt="How to Iterate through a List in Python"> <p> <strong>Understanding:</strong> </p> <p>The range function helps the &apos;for&apos; loop to iterate from 0 to the given list&apos;s length.</p> <p> <strong>Mechanism:</strong> </p> <table class="table"> <tr> <td>the range gives - 0</td> <td>list1 [0]</td> <td>3</td> </tr> <tr> <td>the range gives - 1</td> <td>list1 [1]</td> <td>5</td> </tr> <tr> <td>the range gives - 2</td> <td>list1 [2]</td> <td>7</td> </tr> <tr> <td>the range gives - 3</td> <td>list1 [3]</td> <td>2</td> </tr> <tr> <td>the range gives - 4</td> <td>list1 [4]</td> <td>4</td> </tr> </table> <ul> <li>The range function doesn&apos;t give the last element specified - len (list1) = 5 is not given.</li> </ul> <h2>2. Using List Comprehension</h2> <p>This is the simple and suggested way to iterate through a list in Python.</p> <p> <strong>Code:</strong> </p> <pre> list1 = [3, 5, 7, 2, 4] [print (i) for i in list1] print (&apos;
&apos;) [print (list1 [i]) for i in range (0, len (list1))] print (&apos;
&apos;) </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/python-tutorial/43/how-iterate-through-list-python-4.webp" alt="How to Iterate through a List in Python"> <p> <strong>Understanding:</strong> </p> <p>We can use for loops inside a list comprehension. We used the same for loops we used in the above examples but inside a list in a single line. This way, we can reduce the length of the code and also list comprehension is a very subtle and efficient way to put loops in lists.</p> <h2>3. Using enumerate():</h2> <p>The enumerate function converts the given list into a list of tuples. Another important fact about this function is that it keeps count of the iterations. This is a built-in function in Python.</p> <p> <strong>Code:</strong> </p> <pre> list1 = [3, 5, 7, 2, 4] for i, j in enumerate (list1): print (&apos;index = &apos;, i, &apos;value: &apos;, j) </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/python-tutorial/43/how-iterate-through-list-python-5.webp" alt="How to Iterate through a List in Python"> <h2>4. Using lambda function and map():</h2> <p>These are anonymous functions. There is a function map () in Python that can accept a function as an argument, and it calls the function with every element in the iterable, and a new list with all the elements from the iterable will be returned.</p> <p> <strong>Code:</strong> </p> <pre> list1 = [3, 6, 1, 8, 7] result = list (map (lambda num: num, list1)) print (result) </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/python-tutorial/43/how-iterate-through-list-python-6.webp" alt="How to Iterate through a List in Python"> <p> <strong>Understanding:</strong> </p> <p>The lambda num: num is given as an input to the map function along with the list. The function will take every single element in the list, accept it, and then return it. The map () function will pass the list elements one by one to the lambda function to return the elements.</p> <h2>What if we want to Iterate Multi-dimensional Lists?</h2> <p>There is an inbuilt module in Python designed to perform operations on multi-dimensional lists.</p> <p> <strong>1. To get numpy:</strong> </p> <p>Check if Python and pip are installed by opening the cmd via search and typing the commands:</p> <p> <strong>Python -version</strong> </p> <p> <strong>Pip --version</strong> </p> <p>If both Python and PIP are present in our system, it is now time to install our library:</p> <p> <strong>2. Open cmd from the start menu</strong> </p> <p> <strong>3. Type the command</strong> </p> <h3>pip install numpy</h3> <img src="//techcodeview.com/img/python-tutorial/43/how-iterate-through-list-python.webp" alt="How to Iterate through a List in Python"> <p>All the library packages, data, and sub-packages will be installed one after the other.</p> <p> <strong>Code:</strong> </p> <pre> import numpy list1 = numpy. arange (9) list1 = list1. reshape (3, 3) for x in numpy. nditer (list1): print (x) </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/python-tutorial/43/how-iterate-through-list-python-7.webp" alt="How to Iterate through a List in Python"> <p> <strong>Understanding:</strong> </p> <p>We imported the numpy module. Using the arrange method, we created an array with 9 elements. We accessed the list by reshaping it to 3 * 3 (rows * columns) using the reshape. Using the nditer function, we printed each element in the list.</p> <hr></len(list1)):>

انتاج:

كيفية التكرار من خلال قائمة في بايثون

فهم:

باستخدام for-in، تمكنا من الوصول إلى جميع العناصر الموجودة داخل القائمة.

    استخدام ل والمدى:
 list1 = [3, 5, 7, 2, 4] length = len (list1) for i in range (0, len (list1)): print (list1 [i]) 

انتاج:

كيفية التكرار من خلال قائمة في بايثون

فهم:

تساعد دالة النطاق الحلقة 'for' على التكرار من 0 إلى طول القائمة المحددة.

منطقي في ج

آلية:

النطاق يعطي - 0 ورقة 1 [0] 3
النطاق يعطي - 1 ورقة 1 [1] 5
النطاق يعطي - 2 الورقة 1 [2] 7
النطاق يعطي - 3 ورقة 1 [3] 2
النطاق يعطي - 4 الورقة 1 [4] 4
  • لا تعطي دالة النطاق العنصر الأخير المحدد - len (list1) = 5 غير محدد.

2. استخدام فهم القائمة

هذه هي الطريقة البسيطة والمقترحة للتكرار من خلال قائمة في بايثون.

شفرة:

 list1 = [3, 5, 7, 2, 4] [print (i) for i in list1] print (&apos;
&apos;) [print (list1 [i]) for i in range (0, len (list1))] print (&apos;
&apos;) 

انتاج:

كيفية التكرار من خلال قائمة في بايثون

فهم:

إذا كان ملخص روديارد كيبلينج

يمكننا استخدام حلقات for داخل قائمة الفهم. استخدمنا نفس حلقات for التي استخدمناها في الأمثلة المذكورة أعلاه ولكن داخل قائمة في سطر واحد. بهذه الطريقة، يمكننا تقليل طول الكود وأيضًا فهم القائمة هو طريقة دقيقة وفعالة للغاية لوضع الحلقات في القوائم.

3. استخدام التعداد ():

تقوم وظيفة التعداد بتحويل القائمة المحددة إلى قائمة من الصفوف. حقيقة أخرى مهمة حول هذه الوظيفة هي أنها تقوم بإحصاء التكرارات. هذه وظيفة مدمجة في بايثون.

شفرة:

 list1 = [3, 5, 7, 2, 4] for i, j in enumerate (list1): print (&apos;index = &apos;, i, &apos;value: &apos;, j) 

انتاج:

كيفية التكرار من خلال قائمة في بايثون

4. استخدام دالة لامدا والخريطة ():

هذه وظائف مجهولة. توجد خريطة وظيفية () في بايثون يمكنها قبول دالة كوسيطة، وتقوم باستدعاء الوظيفة مع كل عنصر في العنصر القابل للتكرار، وسيتم إرجاع قائمة جديدة بجميع العناصر من العنصر القابل للتكرار.

شفرة:

 list1 = [3, 6, 1, 8, 7] result = list (map (lambda num: num, list1)) print (result) 

انتاج:

كيفية التكرار من خلال قائمة في بايثون

فهم:

يتم تقديم lambda num: num كمدخل لوظيفة الخريطة مع القائمة. ستأخذ الوظيفة كل عنصر في القائمة، وتقبله، ثم تعيده. ستقوم وظيفة الخريطة () بتمرير عناصر القائمة واحدًا تلو الآخر إلى وظيفة لامدا لإرجاع العناصر.

ماذا لو أردنا تكرار القوائم متعددة الأبعاد؟

توجد وحدة مدمجة في لغة Python مصممة لتنفيذ العمليات على قوائم متعددة الأبعاد.

1. للحصول على الخدر:

حلقة لا نهائية

تحقق من تثبيت Python وpip عن طريق فتح cmd عبر البحث وكتابة الأوامر:

نسخة بايثون

النقطة - الإصدار

إذا كان كل من Python وPIP موجودين في نظامنا، فقد حان الوقت الآن لتثبيت مكتبتنا:

2. افتح cmd من قائمة البداية

3. اكتب الأمر

نقطة تثبيت numpy

كيفية التكرار من خلال قائمة في بايثون

سيتم تثبيت كافة حزم المكتبة والبيانات والحزم الفرعية واحدة تلو الأخرى.

شفرة:

 import numpy list1 = numpy. arange (9) list1 = list1. reshape (3, 3) for x in numpy. nditer (list1): print (x) 

انتاج:

كيفية التكرار من خلال قائمة في بايثون

فهم:

لقد قمنا باستيراد الوحدة النمطية numpy. باستخدام طريقة الترتيب، أنشأنا مصفوفة تحتوي على 9 عناصر. لقد وصلنا إلى القائمة عن طريق إعادة تشكيلها إلى 3 * 3 (صفوف * أعمدة) باستخدام إعادة التشكيل. باستخدام الدالة nditer، قمنا بطباعة كل عنصر في القائمة.