logo

Ligzdotie bloki Python

Ligzdots kortežs ir Python kortežs, kas ir ievietots citā kortežā. Apskatīsim šādu 8 elementu virkni.

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

Šis pēdējais elements, kas sastāv no trim iekavās ievietotiem vienumiem, ir pazīstams kā ligzdots virkne, jo tas atrodas citā kortežā. Ligzdotā kortedža iegūšanai var izmantot galvenā kortedža nosaukumu ar indeksa vērtību kortežs[indekss], un mēs varam piekļūt katram ligzdotā kortedža vienumam, izmantojot korte [index-1][index-2].

Ligzdotas kopas piemērs

Kods

myvecricket iekšā
 # 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) 

Izvade:

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

Dažas ligzdoto kopu darbības

Mēs redzēsim divas nepieciešamās operācijas ar ligzdotiem kortežiem.

Korpusu savienošana ar ligzdotiem korektoriem

Strādājot ar korejām, dažkārt atsevišķi ieraksti ir jāpārvērš ligzdotā grupā, vienlaikus saglabājot tos kā neatkarīgus elementus. Korpusi bieži tiek pievienoti, pievienojot saturu, kas saplacina iegūto trauku, kas parasti nav vēlams. Parunāsim par dažām pieejām šīs problēmas risināšanai.

salīdziniet java virkni

Izmantojot operatoru + un ',' inicializācijas laikā

Šajā tehnikā mēs pievienojam kortežas dalībniekus tāpat kā mēs, bet, inicializējot korteņus, mēs pievienojam komatu aiz katra kortedža, lai novērstu saplacināšanu pievienošanas laikā.

Kods

 # 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)) 

Izvade:

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

Izmantojot operatoru ','

Šo uzdevumu var veikt, savienošanas laikā izmantojot operatoru ','. Tas var veikt drošu savienošanu.

Kods

 # 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)) 

Izvade:

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

Šķirot ligzdotas rindas

Mēs varam izmantot sorted() metodi, lai sakārtotu doto korteži. Pēc noklusējuma šī metode kārto korešu augošā secībā. Piemēram, print(sorted(darbinieks)) sakārtos virkni 'darbinieks' atbilstoši identifikācijas numuram, kas parādās kā 0. dalībnieks no visiem ligzdotajiem kortežiem. Mēs varam izmantot lambda funkciju, lai kārtotu mūsu kortei atkarībā no citiem ligzdotā kortedža elementiem, piemēram, darbinieka vārda vai skaita, kas ir pirmais un otrais ligzdoto kortežu dalībnieks: print(sorted(darbinieks, atslēga = lambda x: x[1])).

manuāla pārbaude

Šajā gadījumā atslēga norāda funkcijai sorted(), pēc kuriem elementiem mums ir jākārto kortežs. Lambda izteiksme: lambda x: x[1] nozīmē, ka kārtošanā ir jāņem vērā atslēga, kas ir indeksa viens elements. Mēs varam rakstīt lambda izteiksmi kā lambda x: x[2], lai sakārtotu mūsu korteču atbilstoši vārdu skaitam.

Kods

 # 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)) 

Izvade:

 [(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)]