logo

الصفوف المتداخلة في بايثون

الصف المتداخل هو صف بايثون تم وضعه داخل صف آخر. دعونا نلقي نظرة على الصف التالي المكون من 8 عناصر.

 tuple = (12, 23, 36, 20, 51, 40, (200, 240, 100)) 

يُعرف هذا العنصر الأخير، والذي يتكون من ثلاثة عناصر محاطة بين قوسين، باسم الصف المتداخل لأنه موجود داخل صف آخر. يمكن استخدام اسم الصف الرئيسي مع قيمة الفهرس، tuple[index]، للحصول على الصف المتداخل، ويمكننا الوصول إلى كل عنصر من الصف المتداخل باستخدام tuple[index-1][index-2].

مثال على Tuple المتداخلة

شفرة

المصفوفات في جافا
 # Python program to create a nested tuple # Creating a nested tuple of one element only employee = ((10, 'Itika', 13000),) print(employee) # Creating a multiple-value nested tuple employee = ((10, 'Itika', 13000), (24, 'Harry', 15294), (15, 'Naill', 20001), (40, 'Peter', 16395)) print(employee) 

انتاج:

 ((10, 'Itika', 13000),) ((10, 'Itika', 13000), (24, 'Harry', 15294), (15, 'Naill', 20001), (40, 'Peter', 16395)) 

بعض عمليات الصفوف المتداخلة

سوف نرى عمليتين ضروريتين للصفوف المتداخلة.

تسلسل الصفوف إلى الصفوف المتداخلة

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

arraylist.sort

استخدام عامل التشغيل + و'،' أثناء التهيئة

في هذه التقنية، نضيف أعضاء صف كما نفعل، ولكن عند تهيئة المجموعات، نضيف فاصلة بعد كل صف لمنع التسطيح أثناء عملية الجمع.

شفرة

 # Python program to concatenate tuples to make a nested tuple # initializing the tuples tup1 = (5, 4), tup2 = (1, 6), # printing the original tuples print('Tuple 1 : ' + str(tup1)) print('Tuple 2 : ' + str(tup2)) # Concatenating the two tuples to a nested tuple using the + operator nested = tup1 + tup2 # printing the result print('The nested tuple : ' + str(nested)) 

انتاج:

 Tuple 1 : ((5, 4),) Tuple 2 : ((1, 6),) The nested tuple : ((5, 4), (1, 6)) 

باستخدام '،' المشغل

يمكن تنفيذ هذه المهمة عن طريق تطبيق عامل التشغيل '،' أثناء التسلسل. يمكنه إجراء تسلسل آمن.

شفرة

 # Python program to concatenate tuples to make a nested tuple # initializing the tuples tup1 = (5, 4) tup2 = (1, 6) # printing the original tuples print('Tuple 1 : ' + str(tup1)) print('Tuple 2 : ' + str(tup2)) # Concatenating the tuples by using the ', 'operator after tuples nested = ((tup1, ) + (tup2, )) # printing result print('The nested tuple ' + str(nested)) 

انتاج:

 Tuple 1 : (5, 4) Tuple 2 : (1, 6) The nested tuple ((5, 4), (1, 6)) 

فرز الصفوف المتداخلة

يمكننا استخدام طريقةsorted() لفرز صف معين. بشكل افتراضي، تقوم هذه الطريقة بفرز الصف بترتيب تصاعدي. على سبيل المثال، ستقوم print(sorted(employee)) بترتيب الصف 'employee' وفقًا لرقم التعريف الذي يظهر كعضو 0 في جميع المجموعات المتداخلة. يمكننا استخدام دالة lambda لفرز الصف الخاص بنا اعتمادًا على العناصر الأخرى في الصف المتداخل، مثل اسم الموظف أو العدد، وهو العضو الأول والثاني في الصف المتداخل: print(sorted(employee, key = lambda س: س[1])).

نواة ميكروليثيك

في هذه الحالة، يخبر المفتاح الدالةsorted()‎، وفقًا للعناصر التي يجب علينا فرز الصف. تعبير لامدا: لامدا x: x[1] يعني أن المفتاح، وهو عنصر الفهرس الأول، يجب أن يؤخذ في الاعتبار عند الفرز. يمكننا كتابة تعبير لامدا بالشكل lambda x: x[2] لفرز صفنا وفقًا لعدد الكلمات.

شفرة

 # Python program to sort the nested tuple using the sorted() function # Creating a nested tuple employee = ((10, 'Itika', 13000), (24, 'Harry', 15294), (15, 'Naill', 20001), (40, 'Peter', 16395)) # Sorting the tuple by default on the id print(sorted(employee)) # Sorting the tuple on id in reverse order print(sorted(employee, reverse = True)) # Sorting the tuple on name using lambda function print(sorted(employee, key = lambda x: x[1])) # Sorting the tuple on the name in reverse order print(sorted(employee, key = lambda x: x[1], reverse = True)) # Sorting the tuple on the word count print(sorted(employee, key = lambda x: x[2])) # Sorting the tuple on the word count in reverse print(sorted(employee, key = lambda x: x[2], reverse = True)) 

انتاج:

 [(10, 'Itika', 13000), (15, 'Naill', 20001), (24, 'Harry', 15294), (40, 'Peter', 16395)] [(40, 'Peter', 16395), (24, 'Harry', 15294), (15, 'Naill', 20001), (10, 'Itika', 13000)] [(24, 'Harry', 15294), (10, 'Itika', 13000), (15, 'Naill', 20001), (40, 'Peter', 16395)] [(40, 'Peter', 16395), (15, 'Naill', 20001), (10, 'Itika', 13000), (24, 'Harry', 15294)] [(10, 'Itika', 13000), (24, 'Harry', 15294), (40, 'Peter', 16395), (15, 'Naill', 20001)] [(15, 'Naill', 20001), (40, 'Peter', 16395), (24, 'Harry', 15294), (10, 'Itika', 13000)]