logo

Tuples מקוננים בפייתון

טאפל מקונן הוא טאפל פייתון שהונח בתוך טופל אחר. בואו נסתכל על ה-tuple בן 8 האלמנטים הבא.

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

אלמנט אחרון זה, המורכב משלושה פריטים סגורים בסוגריים, ידוע בתור tuple מקונן מכיוון שהוא כלול בתוך tuple אחר. ניתן להשתמש בשם ה-tuple הראשי עם ערך האינדקס, tuple[index], כדי להשיג את ה-tuple המקונן, ונוכל לגשת לכל פריט של tuple המקונן באמצעות tuple[index-1][index-2].

דוגמה של Tuple Nested

קוד

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

כמה פעולות של Tuples Nested

נראה שתי פעולות הכרחיות של tuples מקוננות.

שרשור טופלים לטופלים מקוננים

כאשר מתמודדים עם tuples, מדי פעם יש צורך להפוך רשומות נפרדות לקבוצה מקוננת תוך שמירה עליהם כאלמנטים עצמאיים. לעתים קרובות מוסיפים טפלים על ידי הוספת התוכן, מה שמשטח את המיכל המתקבל, וזה בדרך כלל לא רצוי. בואו נדבר על כמה גישות לפתרון בעיה זו.

שימוש באופרטור + ו-',' במהלך האתחול

בטכניקה זו, אנו מוסיפים איברי tuple כפי שאנו עושים, אך בעת אתחול tuples, אנו מוסיפים פסיק אחרי כל tuple כדי למנוע השטחה במהלך ההוספה.

קוד

 # 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() כדי למיין tuple נתון. כברירת מחדל, שיטה זו ממיין את הטפול בסדר עולה. לדוגמה, print(sorted(employee)) יסדר את ה-tuple 'עובד' לפי מספר הזיהוי המופיע בתור האיבר ה-0 מבין כל ה-tuples המקוננים. אנו יכולים להשתמש בפונקציית למבדה כדי למיין את ה-tuple שלנו בהתאם לרכיבים האחרים של ה-tuple המקונן, כמו שם העובד או הספירה, שהיא האיבר הראשון והשני ב-tuples המקונן: print(sorted(employee, key = lambda) x: x[1])).

מחרוזת לצ'אט

במקרה זה, המפתח אומר לפונקציה sorted() לפי אילו אלמנטים עלינו למיין את ה-tuple. הביטוי למבדה: lambda x: x[1] מרמז שיש לשקול את המפתח, שהוא האלמנט index one, למיון. נוכל לכתוב את ביטוי הלמבדה בתור למבדה 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)]