အခန်း 12 · animation 3 ခု

Chapter 11: Nested Tuple များ (Nested Tuples)

808 Coder မှ တင်ဆက်သည်

ဒီအခန်းပြီးရင်...

  • Nested tuple ဆိုတာ ဘာလဲ ရှင်းပြနိုင်ပြီး ကိုယ်တိုင် ဖန်တီးနိုင်မယ်
  • Chained indexing (nested[1][0]) နဲ့ အတွင်းဆုံး တန်ဖိုးတွေကို ခေါ်ထုတ်နိုင်မယ်
  • အပြင် slicing နဲ့ အတွင်း slicing တို့ကို ခွဲခြားသုံးနိုင်မယ်
  • isinstance(item, tuple) guard နဲ့ nested loop သုံးပြီး ပတ်နိုင်မယ်
  • Tuple ထဲက list ကို ပြင်ဆင်လို့ရတဲ့ immutability workaround ကို နားလည်မယ်
  • count() / index() ကို nested tuple မှာ သုံးနိုင်မယ်
  • Concatenation နဲ့ tuple ထဲ "အသစ်ထည့်တဲ့" နည်းကို သုံးနိုင်မယ်
  • Nested tuple နဲ့ ကျောင်းသား database program အသေး ရေးနိုင်မယ်
A nested tuple is a tree of tuples (1, (2, 3), 4) 1 (2, 3) 4 inner tuple 2 3 tuples can hold other tuples, as deep as you like
nested tuple ကို သစ်ပင်ပုံစံနဲ့ ပြခြင်း

Nested Tuple ဆိုတာဘာလဲ (What is a Nested Tuple?)

Nested tuple ဆိုတာ သူ့ရဲ့ element တွေထဲမှာ တခြား tuple တွေ (ဒါမှမဟုတ် တခြား data structure တွေ) ပါဝင်နေတဲ့ tuple ကို ခေါ်ပါတယ်။ ဒီလို ထပ်ထည့်ထားတာကြောင့် tuple တွေကို အလွှာလိုက် ထပ်ပြီး ရှုပ်ထွေးတဲ့ အချက်အလက် ဖွဲ့စည်းပုံတွေ တည်ဆောက်နိုင်ပါတယ်။

# ရိုးရှင်းတဲ့ nested tuple
nested_tuple = (1, 2, (3, 4, 5), 6)
print(nested_tuple)

Output:

(1, 2, (3, 4, 5), 6)

အပြင် tuple မှာ element ၄ ခု ရှိပြီး တတိယမြောက် element ကိုယ်တိုင်က (3, 4, 5) ဆိုတဲ့ tuple တစ်ခု ဖြစ်နေပါတယ်။

လက်တွေ့မှာ nested tuple ကို ဘယ်လို သုံးလဲဆိုတာ ကျောင်းသား မှတ်တမ်းနဲ့ ကြည့်ရအောင်။

# ကျောင်းသားတစ်ယောက်ချင်းစီ: (အမည်, အသက်, (အမှတ်တွေ))
students = (
    ("May", 20, (85, 90, 88)),
    ("Hla", 22, (78, 80, 92))
)
print(students)

Output:

(('May', 20, (85, 90, 88)), ('Hla', 22, (78, 80, 92)))

ကျောင်းသား တစ်ယောက်ချင်းစီရဲ့ အချက်အလက်အစုအဝေးက tuple တစ်ခုဖြစ်ပြီး၊ အဲဒီထဲမှာ အမှတ်တွေရဲ့ tuple တစ်ခု ထပ်ပါနေပါတယ်။ အလွှာ နှစ်ထပ် (တချို့ဆို သုံးထပ်) ဖွဲ့စည်းပုံမျိုးပေါ့။

မှတ်ချက်: Nested list (အခန်း ၉) နဲ့ အယူအဆ အတူတူပါပဲ — ကွာခြားတာက tuple ဖြစ်တဲ့အတွက် ပြင်ဆင်လို့ မရတာပါ။ "မပြောင်းသင့်တဲ့ စနစ်တကျ အချက်အလက်" ဆိုရင် nested tuple ကို ရွေးပါ။


Chained Indexing နှင့် Slicing

Nested tuple ထဲက တန်ဖိုးတွေကို ရောက်ဖို့ index ကို အလွှာလိုက် ဆက်သုံးပါတယ် (chained indexing)။

nested = (10, (20, 30, 40), 50)

# အပြင် element တွေ
print(nested[0])    # 10
print(nested[2])    # 50

# အတွင်း element တွေ
print(nested[1][0])   # အတွင်း tuple ရဲ့ ပထမဆုံး: 20
print(nested[1][2])   # အတွင်း tuple ရဲ့ တတိယ: 40

Output:

10
50
20
40

ကျောင်းသား မှတ်တမ်း နမူနာနဲ့ ဆက်ကြည့်ရအောင်။

students = (
    ("May", 20, (85, 90, 88)),
    ("Hla", 22, (78, 80, 92))
)

# Hla ရဲ့ ဒုတိယ အမှတ်: အပြင် index 1 -> အတွင်း index 2 -> အမှတ် index 1
print(students[1][2][1])

Output:

80

students[1] က ("Hla", 22, (78, 80, 92))၊ [2] က (78, 80, 92)၊ [1] က 80 — အလွှာသုံးထပ် ဆင်းသွားတာပါ။

အပြင် Slicing နှင့် အတွင်း Slicing

nested = (1, 2, (3, 4, 5), 6, 7)

# အပြင် tuple ကို slice
print(nested[1:4])

# အတွင်း tuple ကို slice (အရင် အတွင်းကို ရောက်အောင် သွား)
print(nested[2][1:])

Output:

(2, (3, 4, 5), 6)
(4, 5)

nested[1:4] က အပြင် အလွှာက element ၁ ကနေ ၃ အထိ ဖြတ်ယူတာပါ — အတွင်းက (3, 4, 5) tuple ကြီးကို အရာတစ်ခုအနေနဲ့ အပြည့်အဝ ယူသွားပါတယ်။ nested[2][1:] က အရင် အတွင်း tuple ကို ရောက်အောင် သွားပြီးမှ slice လုပ်တာပါ။

သတိပြုရန်: အတွင်း element က tuple မဟုတ်ဘဲ ကိန်းဂဏန်း သာမန်တန်ဖိုးဆိုရင် ထပ်ပြီး [ ] သုံးလို့ မရပါဘူး။ ဥပမာ nested[0][1] လို့ ရေးရင် TypeError တက်မယ် — nested[0] က 1 ဆိုတဲ့ int သက်သက်မို့လို့ပါ။


Nested Tuple ကို ပတ်ကြည့်ခြင်း (Iteration)

Nested tuple ကို for loop နဲ့ ပတ်တဲ့အခါ အပြင် element တိုင်းကို တစ်ခေါက်ချင်း ရပါတယ်။

nested = (5, (10, 15), (20, 25, 30))

# အပြင် loop သက်သက်
for item in nested:
    print(item)

Output:

5
(10, 15)
(20, 25, 30)

အတွင်းဆုံး တန်ဖိုးတွေအထိ ရောက်ချင်ရင်တော့ nested loop သုံးပြီး၊ item က tuple ဟုတ်မဟုတ် isinstance() နဲ့ စစ်ရပါတယ်။

for item in nested:
    if isinstance(item, tuple):
        for x in item:
            print(x)
    else:
        print(item)

Output:

5
10
15
20
25
30

isinstance(item, tuple) က "ဒီ item က tuple အမျိုးအစား ဟုတ်လား?" လို့ မေးတာပါ။ ဟုတ်ရင် အတွင်းကို ထပ်ပတ်၊ မဟုတ်ရင် တိုက်ရိုက် print ထုတ်ပါတယ်။ ဒီ guard မပါဘဲ int တန်ဖိုးကို ထပ်ပတ်မိရင် TypeError တက်ပါလိမ့်မယ်။

မှတ်ချက်: isinstance() ကို အခန်း ၇ (string methods) မှာလည်း တွေ့ခဲ့ရမှာပါ။ အမျိုးအစား စစ်ဆေးဖို့ Python ရဲ့ စံစနစ်ဖြစ်တဲ့ function ပါ။


Immutability နှင့် Workaround (မပြင်ရပေမဲ့ ခြွင်းချက်)

Tuple တွေက immutable — element တွေကို ပြောင်းလို့ မရဘူးဆိုတာ အခန်း ၁၀ မှာ သင်ခဲ့ပြီးပါပြီ။ ဒါပေမဲ့ ခြွင်းချက်တစ်ခု ရှိပါတယ်: tuple ထဲမှာ list လို mutable အရာဝတ္ထု ပါနေရင် အဲဒီ list ရဲ့ အတွင်းပိုင်းကိုတော့ ပြင်ဆင်လို့ရပါတယ်။

record = ("May", [85, 90], 20)

# tuple ထဲက list ရဲ့ အတွင်းတန်ဖိုးကို ပြင်တာ — ရတယ်!
record[1][0] = 95
print(record)

Output:

('May', [95, 90], 20)

ဘာလို့ ရတာလဲ? Tuple က တားထားတာက "element ကို အစားထိုးတာ" ပါ — record[1] ဆိုတဲ့ list ကြီးကိုယ်တိုင်က အတူတူပဲ (object တစ်ခုတည်း)၊ သူ့ရဲ့ အတွင်းက တန်ဖိုးပဲ ပြောင်းသွားတာပါ။ Tuple က "ဘယ် object ကို ညွှန်းထားလဲ" ဆိုတာကိုပဲ ထိန်းထားပြီး၊ အဲဒီ object ရဲ့ အတွင်းရေးကို ဝင်မစွက်ဖက်ပါဘူး။

ဒါပေမဲ့ element ကိုယ်တိုင်ကို အစားထိုးဖို့ ကြိုးစားရင်တော့ ငြင်းပါလိမ့်မယ်။

# record[0] = "Hla"   # ဒီလိုင်းကို ဖွင့်ကြည့်ရင် TypeError တက်မယ်

သတိပြုရန်: ဒီ workaround ကို အလွဲသုံးစား မလုပ်ပါနဲ့။ "မပြောင်းသင့်တဲ့" အချက်အလက်ကို tuple ထဲ ထည့်ထားရင် အတွင်းမှာ list ထည့်ပြီး ပြင်နေတာက tuple သုံးရတဲ့ ရည်ရွယ်ချက်ကို ပျက်စေပါတယ်။ တကယ် ပြင်ချင်တဲ့ အစိတ်အပိုင်းကို list သက်သက် ခွဲထားတာ ပိုသင့်တော်ပါတယ်။


count() နှင့် index() (Nested Tuple Methods)

count() နဲ့ index() က nested tuple မှာလည်း အလုပ်လုပ်ပါတယ် — ဒါပေမဲ့ အပြင် အလွှာမှာပဲ ရှာတာဆိုတာ သတိထားပါ။

nested = (2, (3, 4), (5, 6), 2)

print(nested.count(2))        # 2 ဘယ်နှစ်ခါ ပါလဲ
print(nested.index((5, 6)))   # (5, 6) tuple ဘယ် index မှာလဲ

Output:

2
2

nested.index((5, 6)) မှာ argument ကိုယ်တိုင်က tuple တစ်ခု ဖြစ်နေတာကို သတိထားပါ — အပြင် အလွှာထဲမှာ (5, 6) နဲ့ တူညီတဲ့ element ကို ရှာတာပါ။ အတွင်းထဲက 5 သက်သက်ကို ရှာချင်ရင် အရင် nested[2] လို့ အတွင်းကို ရောက်အောင် သွားပြီးမှ index(5) ခေါ်ရပါမယ်။

Method အလုပ် သတိထားရန်
count(x) အပြင် အလွှာမှာ x ဘယ်နှစ်ခါ ပါလဲ အတွင်း tuple ထဲက တန်ဖိုးတွေ မရေဘူး
index(x) အပြင် အလွှာမှာ x ပထမဆုံး ဘယ် index လဲ x ကိုယ်တိုင် tuple ဖြစ်နိုင်တယ်

Tuple ထဲ "အသစ်ထည့်ခြင်း" (Appending via Concatenation)

Tuple က immutable ဖြစ်တဲ့အတွက် append() method လုံးဝ မရှိပါဘူး။ ဒါပေမဲ့ + concatenation သုံးပြီး element အသစ်ပါတဲ့ tuple အသစ် တစ်ခု ဖန်တီးလို့ရပါတယ် — ရလဒ်အနေနဲ့က "ထည့်လိုက်သလို" ဖြစ်သွားတာပေါ့။

students = (
    ("May", 20, (85, 90, 88)),
    ("Hla", 22, (78, 80, 92))
)

# ကျောင်းသားအသစ် တစ်ယောက် "ထည့်တာ"
new_student = ("Zaw", 21, (88, 92, 85))
students = students + (new_student,)

print(f"စုစုပေါင်း ကျောင်းသား: {len(students)} ယောက်")
for s in students:
    print(s[0])

Output:

စုစုပေါင်း ကျောင်းသား: 3 ယောက်
May
Hla
Zaw

အရေးကြီးတဲ့ အချက် နှစ်ချက်:

  1. (new_student,) လို့ ကော်မာနဲ့ ထုပ်ရပါတယ် — + ရဲ့ ညာဘက်က tuple ဖြစ်ရမယ်။ (new_student) လို့ ကော်မာမပါဘဲ ရေးရင် tuple မဟုတ်ဘဲ error တက်မယ်။
  2. မူလ students tuple ကို ပြင်တာ မဟုတ်ပါဘူး — students + (new_student,) က tuple အသစ် ဖန်တီးပေးပြီး အဲဒါကို students နာမည်နဲ့ ပြန်မှတ်လိုက်တာပါ။ မူလ tuple က ပျောက်သွားတာပါ (garbage collected)။

မှတ်ချက်: အချက်အလက် ခဏခဏ ထည့်/ဖျက် လုပ်နေရမယ်ဆိုရင် tuple အစား list ကို သုံးတာ ပိုသင့်တော်ပါတယ်။ Concatenation တစ်ခါလုပ်တိုင်း tuple အသစ် ဖန်တီးနေရတာကြောင့် အကြိမ်ရေ များရင် စွမ်းဆောင်ရည် ကျတတ်ပါတယ်။


လက်တွေ့ နမူနာ: Matrix နှင့် ကျောင်းသား မှတ်တမ်း

Matrix ကို Tuple-of-Tuples နဲ့ ကိုယ်စားပြုခြင်း

Nested list လိုပဲ matrix ကို tuple တွေနဲ့လည်း ကိုယ်စားပြုနိုင်ပါတယ်။ Matrix ရဲ့ တန်ဖိုးတွေ မပြောင်းသင့်ဘူးဆိုရင် tuple က ပိုသင့်တော်ပါတယ်။

matrix = (
    (1, 2, 3),
    (4, 5, 6),
    (7, 8, 9)
)

# ဒုတိယ row (index 1), တတိယ column (index 2)
print(matrix[1][2])

Output:

6

ကျောင်းသား Database (အဖြေပါတဲ့ လေ့ကျင့်ခန်း)

Nested tuple ရဲ့ အသုံးဝင်ပုံကို အပြည့်အဝ ပြတဲ့ နမူနာအဖြစ် ကျောင်းသား database လေး တည်ဆောက်ကြည့်ရအောင်။ ကျောင်းသားတစ်ယောက်ချင်းစီ: (အမည်, အသက်, (ဘာသာရပ်တွေ), (အမှတ်တွေ)) ပုံစံပါ။

အဆင့် ၁: မှတ်တမ်းအားလုံး ပြသခြင်း

students = (
    ("May", 20, ("Math", "Science", "English"), (85, 90, 88)),
    ("Hla", 22, ("Math", "Science", "English"), (78, 80, 92)),
    ("Aung", 19, ("Math", "Science", "English"), (90, 85, 95))
)

print("--- All Student Records ---")
for s in students:
    print(f"Name: {s[0]}, Age: {s[1]}, Subjects: {s[2]}, Marks: {s[3]}")

Output:

--- All Student Records ---
Name: May, Age: 20, Subjects: ('Math', 'Science', 'English'), Marks: (85, 90, 88)
Name: Hla, Age: 22, Subjects: ('Math', 'Science', 'English'), Marks: (78, 80, 92)
Name: Aung, Age: 19, Subjects: ('Math', 'Science', 'English'), Marks: (90, 85, 95)

အဆင့် ၂: ကျောင်းသားတစ်ယောက်ရဲ့ စုစုပေါင်းနှင့် ပျမ်းမျှ

name = "May"
for s in students:
    if s[0].lower() == name.lower():
        total = sum(s[3])
        average = total / len(s[3])
        print(f"{name}'s Total Marks: {total}")
        print(f"{name}'s Average Marks: {average:.2f}")
        break

Output:

May's Total Marks: 263
May's Average Marks: 87.67

s[3] က အမှတ်တွေရဲ့ tuple ပါ။ sum() နဲ့ စုစုပေါင်းယူ၊ len() နဲ့ ဘာသာအရေအတွက် စားပြီး ပျမ်းမျှတွက်တာပါ။ :.2f က ဒဿမ နှစ်နေရာပဲ ပြစေတာပါ။

အဆင့် ၃: ဘာသာရပ်အလိုက် အမှတ်အမြင့်ဆုံး ကျောင်းသား

subject = "Science"
top_student = ""
highest_mark = -1

for s in students:
    if subject in s[2]:
        i = s[2].index(subject)      # ဘာသာရပ်ရဲ့ index ကို ရှာ
        if s[3][i] > highest_mark:   # အဲဒီ index က အမှတ်ကို နှိုင်း
            highest_mark = s[3][i]
            top_student = s[0]

if top_student:
    print(f"Top Scorer in {subject}: {top_student} ({highest_mark} marks)")

Output:

Top Scorer in Science: May (90 marks)

ဒီနည်းရဲ့ လှည့်ကွက်က s[2].index(subject) ပါ — ဘာသာရပ်အမည်ကနေ သူ့ရဲ့ index ကို ရှာပြီး အဲဒီ index တူနဲ့ အမှတ် tuple ထဲက အမှတ်ကို ဆွဲထုတ်တာပါ။ Subject tuple နဲ့ marks tuple ရဲ့ အစဉ်က တစ်ထပ်တည်း ကျရမယ်ဆိုတာ အရေးကြီးပါတယ်။

အဆင့် ၄: ကျောင်းသားအသစ် ထည့်ခြင်း

new_student = ("Zaw", 21, ("Math", "Science", "English"), (88, 92, 85))
students = students + (new_student,)

print("--- Updated Student Records ---")
for s in students:
    print(f"Name: {s[0]}, Age: {s[1]}, Marks: {s[3]}")
print(f"စုစုပေါင်း: {len(students)} ယောက်")

Output:

--- Updated Student Records ---
Name: May, Age: 20, Marks: (85, 90, 88)
Name: Hla, Age: 22, Marks: (78, 80, 92)
Name: Aung, Age: 19, Marks: (90, 85, 95)
Name: Zaw, Age: 21, Marks: (88, 92, 85)
စုစုပေါင်း: 4 ယောက်
Reading a nested student record "May" 20 "Math" "English" 85 90, 88 s[0] s[2] s[3] name subjects marks s[3][1] = 90 second mark
student record ထဲက s[0], s[2], s[3][1] တို့ ညွှန်းဆိုပုံ

Tuple-of-Tuples Matrix: Transpose

အခန်း ၉ မှာ nested list နဲ့ matrix ပေါင်းတာ လေ့လာခဲ့ပါတယ်။ Tuple-of-tuples matrix နဲ့လည်း အလားတူ လုပ်လို့ရပါတယ်။ ဥပမာအနေနဲ့ matrix ရဲ့ transpose (row နဲ့ column လဲတာ) ကို ကြည့်ရအောင်။

m = ((1, 2, 3),
     (4, 5, 6))

rows = []
for c in range(len(m[0])):      # column တိုင်းအတွက်
    new_row = []
    for r in range(len(m)):     # row တိုင်းကနေ တန်ဖိုး ဆွဲထုတ်
        new_row.append(m[r][c])
    rows.append(tuple(new_row)) # row ကို tuple အဖြစ် ပြောင်း

transposed = tuple(rows)
print(transposed)

Output:

((1, 4), (2, 5), (3, 6))

မူလ matrix က 2 row × 3 column ဖြစ်ပြီး transpose လုပ်ပြီးတော့ 3 row × 2 column ဖြစ်သွားပါတယ်။ အလယ်မှာ list တွေနဲ့ တည်ဆောက်ပြီး အဆုံးမှာ tuple() နဲ့ ပြန်ထုပ်ပိုးတာက tuple နဲ့ အလုပ်လုပ်ရတဲ့ ပုံမှန် workflow ပါ — တည်ဆောက်တဲ့အခါ list၊ သိမ်းတဲ့အခါ tuple။


Tuple ကို ပြင်ချင်ရင် တရားဝင်နည်း

"Immutability Workaround" အပိုင်းမှာ tuple ထဲက list ကို ပြင်လို့ရတယ်ဆိုတာ တွေ့ခဲ့ရပါတယ်။ ဒါပေမဲ့ tuple ကိုယ်တိုင်ရဲ့ တန်ဖိုးကို တကယ် ပြောင်းချင်တယ်ဆိုရင် သန့်ရှင်းတဲ့ နည်းလမ်း ရှိပါတယ် — list အဖြစ် ပြောင်း → ပြင် → tuple ပြန်ထုပ်။

t = (1, 2, 3)

# အဆင့် ၁: list အဖြစ် ပြောင်း
temp = list(t)

# အဆင့် ၂: list မှာ ပြင်
temp[0] = 99
temp.append(4)

# အဆင့် ၃: tuple ပြန်ထုပ်
t = tuple(temp)
print(t)

Output:

(99, 2, 3, 4)

ဒီနည်းက "မူလ tuple ကို ပြင်တာ" မဟုတ်ဘဲ tuple အသစ် ဖန်တီးတာဆိုတာ သတိရပါ — immutable သဘောတရားကို မချိုးဖောက်ပါဘူး။ အချက်အလက် ခဏခဏ ပြင်နေရမယ်ဆိုရင်တော့ အစကတည်းက list သုံးတာ ပိုသင့်တော်တယ်ဆိုတာ မမေ့ပါနဲ့။

သတိပြုရန်: ဘယ်နည်းကို ရွေးရမလဲ? — အတွင်းက list တစ်ခုတည်းကို ပြင်ရုံဆိုရင် workaround (အပိုင်း ၄) က လုံလောက်ပါတယ်။ Tuple ရဲ့ ဖွဲ့စည်းပုံကိုယ်တိုင် (အရာအရေအတွက်၊ အစဉ်) ပြောင်းချင်ရင်တော့ ဒီ list-ပြောင်း-ပြန်-ထုပ်နည်းကို သုံးပါ။

How to 'change' a tuple: convert, edit, convert back tuple list() modify tuple() new tuple convert edit freely convert back original untouched
tuple ပြင်ဆင်တဲ့ အဆင့် ၄ ဆင့်

အရှည်မညီတဲ့ Nested Tuple များ (Jagged Nested Tuples)

အခန်း ၉ မှာလိုပဲ nested tuple ရဲ့ အတွင်း tuple တိုင်းက အရှည်တူဖို့ မလိုပါဘူး။ အရှည်မညီတဲ့ nested tuple ကို ပတ်တဲ့အခါ isinstance() guard နဲ့ len() ကို တွဲသုံးရပါတယ်။

jagged = (("Aung", 85),
          ("May", 90, "A+"),
          ("Zaw",))

for item in jagged:
    if isinstance(item, tuple):
        print(f"အရာ {len(item)} ခု:", end=" ")
        for x in item:
            print(x, end=" ")
        print()

Output:

အရာ 2 ခု: Aung 85
အရာ 3 ခု: May 90 A+
အရာ 1 ခု: Zaw

အတွင်း tuple ရဲ့ အရှည်ကို ကြိုမသိတဲ့အတွက် index ကို တိုက်ရိုက် ခန့်မှန်းပြီး သုံးလို့ မရပါဘူး — for x in item လို for-each ပုံစံ ဒါမှမဟုတ် range(len(item)) နဲ့ ပတ်တာက အန္တရာယ် ကင်းပါတယ်။

Nested Tuple မှာ ရှာဖွေခြင်း

တန်ဖိုးတစ်ခုကို nested tuple ထဲမှာ ရှာတာလည်း အလားတူ guard ပုံစံနဲ့ပဲ လုပ်ပါတယ်။

data = (10, (20, 30), (40, 50, 60))
target = 50
found = False

for item in data:
    if isinstance(item, tuple):
        if target in item:
            print(f"{target} ကို {item} ထဲမှာ တွေ့ပြီ")
            found = True
            break
    elif item == target:
        print(f"{target} ကို အပြင်အလွှာမှာ တွေ့ပြီ")
        found = True
        break

if not found:
    print(f"{target} မတွေ့ပါ")

Output:

50 ကို (40, 50, 60) ထဲမှာ တွေ့ပြီ

target in item က အတွင်း tuple ထဲမှာ membership စစ်တာပါ — အခန်း ၁၀ ရဲ့ in operator ကို nested အခြေအနေမှာ ပြန်သုံးထားတာပါ။

မှတ်ချက်: Nested data structure တွေနဲ့ အလုပ်လုပ်တဲ့အခါ "အခု ကိုင်နေတဲ့ အရာက ဘာအမျိုးအစားလဲ?" လို့ အမြဲ မေးပါ။ isinstance() က အဲဒီမေးခွန်းရဲ့ အဖြေကို ပေးတဲ့ tool ပါ။ ဒီအလေ့အကျင့်က အခန်း ၁၂ (Set) နဲ့ ၁၃ (Dictionary) မှာလည်း ဆက်အသုံးဝင်ပါလိမ့်မယ်။


လေ့ကျင့်ခန်းများ

  1. (လွယ်) data = (10, (20, 30), (40, 50, 60)) ထဲက 30 နဲ့ 60 ကို chained indexing နဲ့ ခေါ်ထုတ်ပြီး print ထုတ်ပါ။

  2. (အလတ်) nested = (1, 2, (3, 4, 5), 6, 7) — အပြင် slice nested[1:4] နဲ့ အတွင်း slice nested[2][1:] တို့ကို print ထုတ်ပြီး ရလဒ်ကို ရှင်းပြပါ။

  3. (အလတ်) info = ("Yangon", [100, 200], "Myanmar") — tuple ထဲက list ရဲ့ ပထမတန်ဖိုးကို 150 ဖြစ်အောင် ပြင်ပါ။ ပြီးရင် info[0] = "Mandalay" လို့ လုပ်ကြည့်ရင် ဘာဖြစ်မလဲ စမ်းကြည့်ပြီး အကြောင်းရင်း ရေးပါ။

  4. (ခက်) အထက်က ကျောင်းသား database ကို အခြေခံပြီး "Math မှာ အမှတ်အနိမ့်ဆုံး ကျောင်းသား" ကို ရှာထုတ်တဲ့ code ရေးပါ။

  5. (စိန်ခေါ်မှု) records = (("Aung", (80, 85)), ("May", (90, 95)), ("Zaw", (70, 75))) — ကျောင်းသားတိုင်းရဲ့ ပျမ်းမျှအမှတ်ကို တွက်ပြီး ပျမ်းမျှ အမြင့်ဆုံး ကျောင်းသားရဲ့ အမည်နဲ့ ပျမ်းမျှကို print ထုတ်ပါ။ (အကြံပြု: nested loop + unpacking တွဲသုံးပါ)