logo

كيفية تهيئة قائمة في بايثون؟

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

تهيئة القوائم باستخدام الأقواس المربعة

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

شفرة

 # Python program to show how to initialize a list using square brackets # Initializing an empty list list_ = [] print('An empty list: ', list_) # Initializing a list with some values list_ = [1, 3, 5, 7] print('A non-Empty list: ', list_) 

انتاج:

 An empty list: [] A non-Empty list: [1, 3, 5, 7] 

استخدام وظيفة القائمة () المضمنة لتهيئة القائمة

تقوم الدالة list() في بايثون بإنشاء القائمة، وهي كائن قابل للتكرار. لذلك، هذه طريقة أخرى لإنشاء قائمة بايثون فارغة بدون أي بيانات في لغة الترميز هذه.

كائن التكرار، أو التسلسل الذي يمكّن التكرار، أو الحاوية يمكن أن تكون جميعها قابلة للتكرار. يتم إنشاء قائمة فارغة جديدة إذا لم يتم تقديم أي مدخلات.

متغير جافا سكريبت العالمي

شفرة

 # Python program to show how to initialize a list using the built-in list function # Initializing an empty list list_ = list() print('An empty list: ', list_) # Initializing a non-empty list list_ = list([1, 2, 3]) print('A non-empty list: ', list_) 

انتاج:

 An empty list: [] A non-empty list: [1, 2, 3] 

تُفضل طريقة القوس المربع على وظيفة list() المضمنة لأنها أكثر وضوحًا وتوضيحًا.

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

يمكننا استخدام أسلوب فهم القائمة لتعيين المعلمات الافتراضية للقائمة. وهي تشتمل على تعبير محاط بين قوسين مربعين، وعبارة for، وعبارة if اختيارية قد تتبعها أو لا تتبعها. يمكن كتابة أي عنصر نرغب في إضافته إلى القائمة كتعبير. سيكون التعبير 0 إذا قام المستخدم بتهيئة القائمة بالأصفار.

تنسيق السلسلة في جافا

يعد فهم القائمة طريقة أنيقة ومباشرة ومعروفة لإنشاء قائمة مبنية على مكرر.

شفرة

 # Python program to show how to initialize a list using list comprehension # Initializing a list list_ = [item for item in range(10)] print('The list was created using list comprehension: ', list_) 

انتاج:

 The list was created using list comprehension: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 

تعمل هذه التقنية على تهيئة القوائم بشكل أسرع بكثير من حلقات for و while الخاصة ببايثون.

تهيئة قائمة بايثون باستخدام عامل التشغيل *

هناك طريقة أخرى لتهيئة قائمة في بايثون وهي استخدام عامل التشغيل *. يقوم بإنشاء قائمة ذات قيم متعددة. بناء جملة استخدام هذا العامل هو [element] * n. هنا n هو عدد المرات التي نريد فيها تكرار العنصر الموجود في القائمة.

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

شفرة

 # Python program to show how to use the * operator to initialize a list list_ = [5]*10 print (list) 

انتاج:

 [5, 5, 5, 5, 5, 5, 5, 5, 5] 

هذه الطريقة فعالة للغاية وهي أسرع طريقة لإنشاء القائمة. سنقوم بمقارنة الوقت الذي تستغرقه الطرق لاحقًا في هذا البرنامج التعليمي.

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

تم فرز قائمة الصفيف

باستخدام حلقة للوإلحاق ()

سنقوم بإنشاء قائمة فارغة وتشغيل حلقة لإضافة عناصر باستخدام وظيفة الإلحاق () للقائمة.

شفرة

 # Python program to show how to use a for loop to initialize a list arr = [] for i in range(1000): arr.append(0) 

استخدام حلقة while لتهيئة القائمة

يمكننا استخدام حلقة while تمامًا كما استخدمنا حلقة for لتهيئة القائمة.

شفرة

 # Python program to initialize a list using a while loop # Creating an empty list array = [] # Declaring counter variables i = 0 # Starting a while loop while(i <10): array.append(0) i +="1" print(array) < pre> <p> <strong>Output:</strong> </p> <pre> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] </pre> <h2>Time Complexity</h2> <p>Let us now see how long each of the described approaches will take. We will initialize a list of 100000 elements 1000 times. We will calculate the average time each method takes to perform this task.</p> <p> <strong>Code</strong> </p> <pre> # Python program to see the time taken by various methods to initialize a list # importing the time module to calculate the time taken by a chunk of code import time # initializing the lists for various methods forLoop = [] whileLoop = [] listComprehension = [] starOperator = [] # repeating the process of generating a list of 100000 elements 500 times # Then calculate the average time taken by the methods for i in range(1000): # starting time of the execution begin = time.time() # declaring an empty list list_ = [] # running a for loop and iterating it 100000 times for i in range(100000): list_.append(0) # stoping time of the execution end = time.time() forLoop.append(end - begin) # starting time of the execution begin = time.time() # declaring an empty list list_ = [] i = 0 # COunter variable # running a while loop and iterating it 100000 times while i <100000: 100000 list_.append(0) i +="1" end="time.time()" whileloop.append(end - begin) begin="time.time()" # using a list comprehension to initialize the for in range(100000)] listcomprehension.append(end astrick (*) operator * staroperator.append(end print('the average execution time of loop is: ', sum(forloop) 1000) while sum(whileloop) sum(listcomprehension) taken operator: sum(staroperator) < pre> <p> <strong>Output:</strong> </p> <pre> The average execution time of for loop is: 0.01166958212852478 The average execution time of the while loop is: 0.01916465663909912 The average execution time of list comprehension is: 0.005084690093994141 The average execution time was taken of * operator: 0.00028331947326660156 </pre> <p>We can see that for and while loops take almost the same execution time. However, for loop is a little better than the while loop.</p> <p>List comprehension shows much better performance than the for and while loops. It is 2-3 times faster than the loops. Thus, list comprehension is much more efficient than the append() function of the lists.</p> <p>The * operator has shown the best performance out of all the four methods.</p> <hr></100000:></pre></10):>

تعقيد الوقت

دعونا نرى الآن كم من الوقت سيستغرق كل من الأساليب الموصوفة. سنقوم بتهيئة قائمة تضم 100000 عنصر 1000 مرة. سنقوم بحساب متوسط ​​الوقت الذي تستغرقه كل طريقة لتنفيذ هذه المهمة.

شفرة

 # Python program to see the time taken by various methods to initialize a list # importing the time module to calculate the time taken by a chunk of code import time # initializing the lists for various methods forLoop = [] whileLoop = [] listComprehension = [] starOperator = [] # repeating the process of generating a list of 100000 elements 500 times # Then calculate the average time taken by the methods for i in range(1000): # starting time of the execution begin = time.time() # declaring an empty list list_ = [] # running a for loop and iterating it 100000 times for i in range(100000): list_.append(0) # stoping time of the execution end = time.time() forLoop.append(end - begin) # starting time of the execution begin = time.time() # declaring an empty list list_ = [] i = 0 # COunter variable # running a while loop and iterating it 100000 times while i <100000: 100000 list_.append(0) i +="1" end="time.time()" whileloop.append(end - begin) begin="time.time()" # using a list comprehension to initialize the for in range(100000)] listcomprehension.append(end astrick (*) operator * staroperator.append(end print(\'the average execution time of loop is: \', sum(forloop) 1000) while sum(whileloop) sum(listcomprehension) taken operator: sum(staroperator) < pre> <p> <strong>Output:</strong> </p> <pre> The average execution time of for loop is: 0.01166958212852478 The average execution time of the while loop is: 0.01916465663909912 The average execution time of list comprehension is: 0.005084690093994141 The average execution time was taken of * operator: 0.00028331947326660156 </pre> <p>We can see that for and while loops take almost the same execution time. However, for loop is a little better than the while loop.</p> <p>List comprehension shows much better performance than the for and while loops. It is 2-3 times faster than the loops. Thus, list comprehension is much more efficient than the append() function of the lists.</p> <p>The * operator has shown the best performance out of all the four methods.</p> <hr></100000:>

يمكننا أن نرى أن حلقات for و while تستغرق تقريبًا نفس وقت التنفيذ. ومع ذلك، فإن حلقة for أفضل قليلاً من حلقة while.

يُظهر فهم القائمة أداءً أفضل بكثير من حلقات for و while. إنه أسرع 2-3 مرات من الحلقات. وبالتالي، فإن فهم القائمة يكون أكثر كفاءة من وظيفة الإلحاق () للقوائم.

لقد أظهر المشغل * أفضل أداء من بين جميع الطرق الأربع.