အခန်း 13 · animation 3 ခု

Chapter 12: Set များ (Sets)

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

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

  • set ဆိုတာ ဘာလဲ၊ list နဲ့ tuple နဲ့ ဘယ်လို ကွာခြားသလဲ ရှင်းပြနိုင်မယ်
  • set အသစ်တစ်ခု ဖန်တီးနိုင်မယ် (အလွတ် set ဖန်တီးနည်းအပါအဝင်)
  • add(), update(), remove(), discard(), pop(), clear() method တွေကို သုံးတတ်မယ်
  • union, intersection, difference, symmetric difference operation တွေကို operator ရော method ရော သုံးတတ်မယ်
  • membership testing သုံးပြီး လက်တွေ့ program တွေ ရေးနိုင်မယ်

Set ဆိုတာဘာလဲ

အရင်အခန်းတွေမှာ list နဲ့ tuple ကို လေ့လာပြီးပြီ။ သူတို့နှစ်ခုလုံးက အစဉ်လိုက် (ordered) သိမ်းတဲ့ collection တွေပါ။ အခု လေ့လာမယ့် set ကတော့ သဘောတရားနည်းနည်း ကွဲပါတယ်။

Set ဆိုတာ Python ရဲ့ built-in data structure တစ်ခုဖြစ်ပြီး၊ ပစ္စည်းတွေ (items) အများကြီးကို variable တစ်ခုတည်းမှာ သိမ်းထားနိုင်တယ်။ ဒါပေမယ့် သူ့မှာ ထူးခြားတဲ့ စည်းမျဉ်း 3 ခု ရှိတယ်။

  1. Unordered — အစဉ်အလိုက် မသိမ်းဘူး။ ထည့်တဲ့အတိုင်း ပြန်ထွက်မလာဘူး။
  2. No duplicates — တူညီတဲ့တန်ဖိုး နှစ်ခါ ထည့်လို့ မရဘူး။ ထည့်မိရင်တောင် တစ်ခါပဲ သိမ်းထားမယ်။
  3. Mutable — ဖန်တီးပြီးတဲ့နောက် ပြောင်းလဲနိုင်တယ် (ထည့်/ဖျက် လုပ်နိုင်တယ်)။

ဥပမာတစ်ခုကြည့်ရအောင်။

hobbies = {"reading", "gaming", "cycling"}
print(hobbies)
print(type(hobbies))
{'reading', 'gaming', 'cycling'}
<class 'set'>

မှတ်ချက်: set ကို print ထုတ်တဲ့အခါ ထည့်ထားတဲ့အစဉ်အတိုင်း မထွက်တတ်ဘူး။ Program ကို တစ်ခါပြန် run ရင် အစဉ်ပြောင်းသွားနိုင်တယ်။ ဒါက error မဟုတ်ဘူး — set ရဲ့ သဘာဝပဲ။

Set ဖန်တီးခြင်း

set ဖန်တီးဖို့ အလွယ်ဆုံးနည်းက curly braces { } သုံးတာပါ။

numbers = {5, 12, 7, 3}
print(numbers)
{3, 5, 7, 12}

အပေါ်က output ကို ကြည့်ပါ — ထည့်တုန်းက 5, 12, 7, 3 ဆိုတဲ့ အစဉ်နဲ့ ထည့်ခဲ့ပေမယ့်၊ print ထုတ်တော့ {3, 5, 7, 12} ဆိုပြီး အစဉ်မတူဘဲ ထွက်လာတယ်။ unordered ဆိုတာ ဒါပါပဲ။

အလွတ် set ဖန်တီးခြင်း

အလွတ် set ဖန်တီးချင်ရင် set() လို့ ရေးရတယ်။

empty_set = set()
print(empty_set)
print(type(empty_set))
set()
<class 'set'>

သတိပြုရန်: အလွတ် set ဖန်တီးဖို့ {} လို့ လုံးဝ မရေးရဘူး။ {} ဆိုတာ dictionary အလွတ်ဖြစ်သွားမယ်၊ set မဟုတ်ဘူး။ ဒါက အစပြုသူတွေ အမှားအများဆုံး အချက်ပဲ။

not_a_set = {}
print(type(not_a_set))
<class 'dict'>

Data type မျိုးစုံ ထည့်နိုင်ခြင်း

set ထဲမှာ data type အမျိုးမျိုး ရောထည့်နိုင်တယ် — int, str, float တွေ ရောလို့ ရတယ်။

mixed = {1, "hello", 3.14, True}
print(mixed)

ဒါပေမယ့် တစ်ခုတော့ သတိထားရမယ် — set ထဲထည့်တဲ့ item တွေက immutable (မပြောင်းလဲနိုင်တဲ့) type တွေ ဖြစ်ရမယ်။ list လို mutable type ကို set ထဲ ထည့်လို့ မရဘူး။

s = {1, 2, 3}
s.add([4, 5])
TypeError: unhashable type: 'list'

Duplicate တွေ အလိုအလျောက် ဖယ်ရှားခြင်း

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

nums = {4, 1, 4, 2, 1, 3}
print(nums)
{1, 2, 3, 4}

4 နဲ့ 1 ကို နှစ်ခါစီ ထည့်ခဲ့ပေမယ့်၊ ရလဒ်မှာ တစ်ခါစီပဲ ကျန်တယ်။

A set keeps only unique values 4 1 4 2 1 3 set() duplicates removed 1 2 3 4 {1, 2, 3, 4}
set က ပွားနေတဲ့ တန်ဖိုးတွေ ဖယ်ရှားပုံ

ဒီအချက်ကို list ထဲက duplicate တွေ ဖယ်ရှားဖို့ အသုံးချနိုင်တယ်။

scores = [85, 92, 85, 70, 92, 100]
unique_scores = set(scores)
print(unique_scores)
print("Unique count:", len(unique_scores))
{70, 100, 85, 92}
Unique count: 4

list ကို set() နဲ့ ပြောင်းလိုက်ရုံနဲ့ duplicate တွေ ပျောက်သွားတယ်။ လက်တွေ့မှာ တော်တော် အသုံးဝင်တဲ့ လှည့်ကွက်တစ်ခုပဲ။

Set method များ

set က mutable ဖြစ်တဲ့အတွက် ဖန်တီးပြီးတဲ့နောက် item တွေ ထည့်/ဖျက် လုပ်နိုင်တယ်။ အသုံးအများဆုံး method တွေကို တစ်ခုချင်း ကြည့်ရအောင်။

add() — item တစ်ခုတည်း ထည့်ခြင်း

colors = {"red", "green"}
colors.add("blue")
print(colors)
{'red', 'green', 'blue'}

duplicate ထည့်မိရင် ဘာမှ မဖြစ်ဘူး — error လည်း မတက်ဘူး၊ ထပ်လည်းမထည့်ဘူး။

colors.add("red")
print(colors)
{'red', 'green', 'blue'}

update() — item အများကြီး တစ်ခါတည်း ထည့်ခြင်း

add() က တစ်ခါတည်း တစ်လုံးပဲ ထည့်နိုင်တယ်။ အများကြီး ထည့်ချင်ရင် update() သုံးတယ်။ list ဖြစ်ဖြစ်၊ နောက် set တစ်ခုဖြစ်ဖြစ် ပေးနိုင်တယ်။

colors = {"red", "green"}
colors.update(["yellow", "purple"])
print(colors)

more_colors = {"orange", "pink"}
colors.update(more_colors)
print(colors)
{'red', 'green', 'yellow', 'purple'}
{'red', 'green', 'yellow', 'purple', 'orange', 'pink'}

remove() နဲ့ discard() — item ဖျက်ခြင်း

item ဖျက်ဖို့ method နှစ်ခု ရှိတယ်။ သူတို့နှစ်ခုရဲ့ ကွာခြားချက်က အရေးကြီးတယ်။

  • remove(x) — x ကို ဖျက်တယ်။ x မရှိရင် error တက်တယ်။
  • discard(x) — x ရှိရင် ဖျက်တယ်။ မရှိရင် ဘာမှ မလုပ်ဘူး (error မတက်ဘူး)။
bag = {"pen", "book", "ruler"}

bag.remove("book")
print(bag)

bag.discard("eraser")
print(bag)

bag.remove("eraser")
{'pen', 'ruler'}
{'pen', 'ruler'}
Traceback (most recent call last):
  ...
KeyError: 'eraser'

discard("eraser") က "eraser" မရှိပေမယ့် တိတ်တိတ်လေး ကျော်သွားတယ်။ remove("eraser") ကြတော့ KeyError နဲ့ program ရပ်သွားတယ်။

သတိပြုရန်: ဖျက်မယ့် item ရှိမရှိ မသေချာရင် discard() သုံးတာ ပိုလုံခြုံတယ်။ remove() ကိုတော့ item ရှိတာ သေချာတဲ့အခါ၊ ဒါမှမဟုတ် error တက်စေချင်တဲ့အခါမှ သုံးပါ။

pop() — item တစ်ခု ထုတ်ယူခြင်း

pop() က set ထဲက item တစ်ခုကို ဖျက်ပြီး ပြန်ပေးတယ်။ ဒါပေမယ့် set က unordered ဖြစ်တဲ့အတွက် ဘယ် item ထွက်လာမလဲ ကြိုမသိနိုင်ဘူး။

bag = {"pen", "book", "ruler"}
taken = bag.pop()
print("Taken:", taken)
print("Remaining:", bag)
Taken: pen
Remaining: {'book', 'ruler'}

မှတ်ချက်: list ရဲ့ pop() က နောက်ဆုံး item ကို ထုတ်ပေးပေမယ့်၊ set ရဲ့ pop() ကတော့ "arbitrary" (မမှန်းဆနိုင်တဲ့) item တစ်ခုကို ထုတ်ပေးတယ်။ "random" လို့ ပြောလေ့ရှိပေမယ့် တကယ်က Python ရဲ့ internal order အတိုင်း ထုတ်တာ ဖြစ်တယ်။

clear() — အကုန်ဖျက်ခြင်း

bag = {"pen", "book", "ruler"}
bag.clear()
print(bag)
set()

clear() ပြီးတဲ့နောက် အလွတ် set set() ပဲ ကျန်တယ်။

အကျဉ်းချုပ် table:

Method အလုပ်လုပ်ပုံ ဥပမာ
add(x) item တစ်ခုတည်း ထည့်တယ် s.add(3)
update(iterable) item အများကြီး ထည့်တယ် s.update([4, 5])
remove(x) ဖျက်တယ်၊ မရှိရင် error တက်တယ် s.remove(3)
discard(x) ဖျက်တယ်၊ မရှိရင် ဘာမှမလုပ်ဘူး s.discard(99)
pop() item တစ်ခု ဖျက် + ပြန်ပေး (မမှန်းဆနိုင်တဲ့ တစ်ခု) item = s.pop()
clear() အကုန်ဖျက်တယ် s.clear()

List, Tuple, Set — နှိုင်းယှဉ်ကြည့်ခြင်း

အခု collection သုံးမျိုး (list, tuple, set) ကို လေ့လာပြီးပြီ။ သူတို့ကို ဘေးချင်းယှဉ်ကြည့်ရင် ဘယ်အချိန်မှာ ဘယ်ဟာသုံးရမလဲ ပိုရှင်းသွားမယ်။

လက္ခဏာ List Tuple Set
အစဉ် (ordered) ဟုတ်တယ် ဟုတ်တယ် မဟုတ်ဘူး
Duplicate ရ/မရ ရတယ် ရတယ် မရဘူး (အလိုအလျောက် ဖယ်ရှားတယ်)
ပြောင်းလဲနိုင်မှု (mutable) ရတယ် မရဘူး ရတယ်
Index နဲ့ ယူလို့ရ/မရ ရတယ် (a[0]) ရတယ် (a[0]) မရဘူး
ဖန်တီးပုံ [1, 2, 3] (1, 2, 3) {1, 2, 3}

ရွေးချယ်ဖို့ လမ်းညွှန်ချက်:

  • အစဉ်လိုက် သိမ်းချင်ပြီး ပြောင်းလဲချင်ရင် → list (ဥပမာ: စျေးဝယ်စာရင်း)
  • မပြောင်းလဲစေချင်တဲ့ အစဉ်လိုက် data ဆို → tuple (ဥပမာ: GPS coordinate)
  • duplicate မလိုချင်ဘူး၊ အမြန်ရှာဖွေချင်တယ်ဆို → set (ဥပမာ: tag တွေ၊ username စာရင်း)

ဒီ table ကို သေချာမှတ်ထားပါ — program ရေးတဲ့အခါမှာ ဘယ် collection သုံးရမလဲ ဆုံးဖြတ်ဖို့ အမြဲ ပြန်ကြည့်ရမယ့် လမ်းညွှန်ပဲ။ ရွေးချယ်မှု မှန်ကန်ရင် code က ပိုတိုမယ်၊ ပိုမြန်မယ်၊ ပိုဖတ်ရလွယ်မယ်။

list နဲ့ set ကြား ကူးပြောင်းတဲ့ ဥပမာတစ်ခု ကြည့်ရအောင်။

# list -> set: remove duplicates
raw_tags = ["python", "coding", "python", "tutorial", "coding"]
clean_tags = set(raw_tags)
print(clean_tags)
print(len(clean_tags))

# set -> list: sort for display
sorted_tags = sorted(clean_tags)
print(sorted_tags[0])
{'python', 'coding', 'tutorial'}
3
coding
# အစဉ်လိုက် + index လိုချင် -> list
shopping = ["rice", "oil", "rice"]
print(shopping[0])

# duplicate မလို -> set
tags = {"python", "coding", "python"}
print(tags)
print(len(tags))
rice
{'python', 'coding'}
2

မှတ်ချက်: set က index မရှိတဲ့အတွက် tags[0] လို့ ရေးလို့ မရဘူး။ အစဉ်လိုက် ယူချင်ရင် sorted(tags) နဲ့ list အဖြစ် ပြောင်းယူပါ။

set ရဲ့ အစွမ်းအထက်ဆုံး အပိုင်းကို ရောက်ပြီ။ သင်္ချာမှာ သင်ဖူးတဲ့ Venn diagram သဘောတရားကို Python မှာ တိုက်ရိုက်သုံးနိုင်တယ်။

နမူနာအဖြစ် ကျောင်းသားကလပ် နှစ်ခုကို ယူကြည့်ရအောင်။

football = {"Aung", "Bo", "Chit", "Nanda"}
basketball = {"Chit", "Nanda", "May", "Zaw"}

Union (| သို့မဟုတ် .union()) — ပေါင်းစည်းခြင်း

set နှစ်ခုလုံးက item တွေ အကုန်လုံး ကို ပေါင်းတယ် (duplicate မပါ)။

all_players = football | basketball
print(all_players)

print(football.union(basketball))
{'Aung', 'Bo', 'Chit', 'Nanda', 'May', 'Zaw'}
{'Aung', 'Bo', 'Chit', 'Nanda', 'May', 'Zaw'}

"ဘောလုံးကလပ် သို့မဟုတ် ဘတ်စကတ်ဘောကလပ် တစ်ခုခုမှာ ပါဝင်သူ အားလုံး" ဆိုတဲ့ မေးခွန်းကို ဖြေနိုင်တယ်။

Intersection (& သို့မဟုတ် .intersection()) — ထပ်နေတဲ့ အပိုင်း

set နှစ်ခုလုံးမှာ တူညီစွာ ပါဝင်တဲ့ item တွေပဲ ယူတယ်။

both_sports = football & basketball
print(both_sports)

print(football.intersection(basketball))
{'Chit', 'Nanda'}

"ကလပ်နှစ်ခုလုံးမှာ ပါဝင်သူ" — Chit နဲ့ Nanda ပဲ။

Difference (- သို့မဟုတ် .difference()) — ကွာခြားချက်

ပထမ set ထဲမှာ ပါပြီး ဒုတိယ set ထဲမှာ မပါတဲ့ item တွေ။

only_football = football - basketball
print(only_football)

print(football.difference(basketball))
{'Aung', 'Bo'}

"ဘောလုံးကလပ်မှာပဲ ပါဝင်ပြီး ဘတ်စကတ်ဘောမှာ မပါဝင်သူ" — သတိထားပါ၊ a - b နဲ့ b - a ရလဒ်မတူဘူး။ အစဉ်အလာက အရေးကြီးတယ်။

print(basketball - football)
{'May', 'Zaw'}

Symmetric Difference (^ သို့မဟုတ် .symmetric_difference())

set နှစ်ခုထဲက တစ်ခုခုမှာပဲ ပါဝင်ပြီး၊ နှစ်ခုလုံးမှာ တစ်ပြိုင်တည်း မပါဝင်တဲ့ item တွေ။

single_sport = football ^ basketball
print(single_sport)

print(football.symmetric_difference(basketball))
{'Aung', 'Bo', 'May', 'Zaw'}

"ကလပ်တစ်ခုတည်းမှာပဲ ပါဝင်သူ" — ကလပ်နှစ်ခုလုံး ဆော့တဲ့ Chit နဲ့ Nanda မပါဘူး။

Union vs intersection A B A | B (union) everything in either circle A B A & B (intersection) only the overlap
union နဲ့ intersection Venn diagram များ
Difference vs symmetric difference A B A - B (difference) in A but not in B A B A ^ B (symmetric diff.) in exactly one circle
difference နဲ့ symmetric difference Venn diagram များ

operation တွေကို အကျဉ်းချုပ်ကြည့်ရအောင် (a = {1, 2, 3}, b = {3, 4, 5} ဆိုပါစို့)။

Operation Operator Method ရလဒ်
Union a | b a.union(b) {1, 2, 3, 4, 5}
Intersection a & b a.intersection(b) {3}
Difference a - b a.difference(b) {1, 2}
Symmetric difference a ^ b a.symmetric_difference(b) {1, 2, 4, 5}

မှတ်ချက်: operator (|, &, -, ^) နဲ့ method (.union()...) နှစ်မျိုးလုံး ရလဒ်အတူတူပဲ။ ကြိုက်တာသုံး၊ ဒါပေမယ့် program တစ်ခုထဲမှာ ပုံစံတစ်မျိုးတည်း စွဲသုံးတာ ဖတ်ရပိုလွယ်တယ်။

Membership testing — ပါဝင်မှု စစ်ဆေးခြင်း

item တစ်ခု set ထဲမှာ ရှိမရှိ စစ်ဖို့ in နဲ့ not in ကို သုံးတယ်။ ဒါက set ရဲ့ အမြန်ဆုံး အလုပ်တွေထဲက တစ်ခုပဲ — list ထဲမှာ လိုက်ရှာတာထက် အများကြီး မြန်တယ်။

fruits = {"apple", "banana", "cherry"}

print("banana" in fruits)
print("grape" in fruits)
print("grape" not in fruits)
True
False
True

လက်တွေ့ ဥပမာတစ်ခု ကြည့်ရအောင် — ပွဲတစ်ခုရဲ့ VIP ဧည့်သည်စာရင်း စစ်ဆေးခြင်း။

vip_guests = {"Aung Aung", "Su Su", "Thiri", "Kaung Htet"}

name = input("Enter your name: ").strip()

if name in vip_guests:
    print(f"Welcome, {name}! Please enjoy the event.")
else:
    print("Sorry, your name is not on the guest list.")

ဒီလို membership check မျိုးက login system တွေ၊ ban list တွေ၊ စာရင်းစစ်ဆေးမှုတွေမှာ အမြဲသုံးတယ်။ set သုံးရင် list သုံးတာထက် ပိုမြန်ပြီး၊ duplicate စိုးရိမ်စရာလည်း မရှိဘူး။

not in နဲ့ ခွင့်မပြုတဲ့ item တွေ စစ်ထုတ်တဲ့ ဥပမာတစ်ခုပါ။

banned_words = {"spam", "scam", "fake"}

comments = ["hello everyone", "this is spam", "nice post", "fake news here"]

for comment in comments:
    words = set(comment.split())
    if words & banned_words:
        print(f"Blocked: '{comment}'")
    else:
        print(f"Allowed: '{comment}'")
Allowed: 'hello everyone'
Blocked: 'this is spam'
Allowed: 'nice post'
Blocked: 'fake news here'

words & banned_words ဆိုတာ မှတ်ချက်ထဲက စကားလုံးတွေနဲ့ တားမြစ်စာရင်းရဲ့ intersection — ထပ်နေတဲ့ စကားလုံး ရှိရင် ရလာတဲ့ set က အလွတ် မဟုတ်တော့ဘူး။ set operation နဲ့ membership testing ကို တွဲသုံးလိုက်တာ သတိထားမိလား။ ဒီလိုမျိုး ပေါင်းစပ်သုံးတတ်လာရင် program ရေးတာ တော်တော် အဆင်ပြေသွားမယ်။

Mini Project: Personal Finance Tracker

အခန်းမှာ သင်ခဲ့တာတွေကို ပေါင်းပြီး လက်တွေ့ program တစ်ခုရေးကြည့်ရအောင်။ Personal Finance Tracker — လစဉ် အသုံးစရိတ်တွေကို category အလိုက် မှတ်တမ်းတင်တဲ့ program ပါ။

ဒီဇိုင်း အကြံဉာဏ်:

  • အသုံးစရိတ် category တွေကို set ထဲမှာ သိမ်းမယ် (duplicate category မရှိစေဖို့)
  • category တစ်ခုချင်းစီရဲ့ စုစုပေါင်း အသုံးစရိတ်ကို dictionary ထဲမှာ သိမ်းမယ် (dictionary ကို နောက်အခန်းမှာ အသေးစိတ် သင်မယ် — အခု category: total ဆိုပြီး တွဲသိမ်းတယ်လို့ပဲ မှတ်ထားပါ)
  • menu-driven program: စရိတ်ထည့်မယ်၊ အစီရင်ခံစာ ကြည့်မယ်၊ ထွက်မယ်

category တွေကို set ထဲမှာ သိမ်းထားတဲ့အတွက် နောက်ပိုင်း category အသစ် ထပ်တိုးချင်ရင် categories.add("Travel") လို့ တစ်ကြောင်းတည်း ရေးရုံပဲ — ကျန်တဲ့ program က အလိုအလျောက် လိုက်လုပ်ပေးမယ်။ expenses dictionary ကိုတော့ category အသစ်အတွက် expenses["Travel"] = 0 လို့ အစပျိုးပေးဖို့ လိုမယ်။ ဒီလို data structure နှစ်ခု တွဲသုံးတာက လက်တွေ့ program တွေမှာ အလွန် အဖြစ်များတဲ့ ပုံစံပဲ။

# Store categories in a set (no duplicates allowed)
categories = {"Food", "Transport", "Shopping", "Health", "Entertainment", "Other"}

# Store expenses in a dictionary, where each category is a key
expenses = {category: 0 for category in categories}

# Main program loop
while True:
    print("\n--- Personal Finance Tracker ---")
    print("1. Add Expense")
    print("2. Show Expenses")
    print("3. Exit")
    choice = input("Choose an option (1/2/3): ").strip()

    if choice == "1":
        # Add Expense
        print("\nAvailable categories:", ", ".join(sorted(categories)))
        category = input("Enter the category: ").strip().capitalize()

        if category not in categories:
            print("Invalid category. Please choose from the available categories.")
            continue

        amount = float(input(f"Enter the amount spent on {category}: "))
        if amount <= 0:
            print("Amount must be a positive number.")
            continue

        expenses[category] += amount
        print(f"{amount} added to {category}.")

    elif choice == "2":
        # Show Expenses
        total = sum(expenses.values())
        print(f"\nTotal Expenses: {total}")
        print("Breakdown by Category:")
        for category, amount in sorted(expenses.items()):
            print(f"- {category}: {amount}")

    elif choice == "3":
        # Exit
        print("Goodbye! Stay on top of your finances!")
        break

    else:
        print("Invalid choice. Please choose 1, 2 or 3.")

ဒီ program မှာ set ရဲ့ အားသာချက် နှစ်ခု ပေါ်လွင်တယ်။

  1. category not in categories — category မှန်/မှား စစ်ဆေးတာ မြန်ဆန်တိကျတယ်။
  2. category စာရင်းမှာ duplicate ပါလာမှာ စိုးရိမ်စရာ မရှိဘူး — set က အလိုအလျောက် ထိန်းပေးတယ်။

မှတ်ချက်: sorted(categories) လို့ ရေးထားတာ print ထုတ်တဲ့အခါ အစဉ်လိုက်လေး ထွက်စေချင်လို့ပါ။ set ကိုယ်တိုင်က unordered ဖြစ်နေဆဲပဲ — sorted() က ယာယီ စာရင်းစဉ်ပေးရုံပါ။

ဒီ project ကို ကိုယ်တိုင် run ကြည့်ပြီး category အသစ်ထည့်တာ၊ မှားတဲ့ category ရိုက်တာ၊ amount အနှုတ် ရိုက်တာတွေကို စမ်းကြည့်ပါ။

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

အောက်ပါ လေ့ကျင့်ခန်းတွေကို ကိုယ်တိုင် စဉ်းစားပြီး ဖြေကြည့်ပါ။ အဖြေကို ချက်ချင်း မကြည့်ဘဲ အရင် ကြိုးစားပါ။ ခက်တဲ့ အပိုင်းတွေမှာ အခန်းထဲက နမူနာ code တွေကို ပြန်ကြည့်နိုင်ပါတယ်။ အားလုံး ပြီးသွားရင် သူငယ်ချင်းနဲ့ အဖြေချင်း ယှဉ်ကြည့်ပါ။ အောင်မြင်ပါစေ။

  1. (လွယ်ကူသော) အောက်ပါ list ထဲက duplicate တွေကို ဖယ်ရှားပြီး set အဖြစ် print ထုတ်ပါ။ ပြီးရင် unique item အရေအတွက်ကိုလည်း ပြပါ။

    numbers = [3, 7, 3, 9, 7, 1, 9, 3]
  2. (အလယ်အလတ်) ဝန်ထမ်း နှစ်သုတ်ရဲ့ set နှစ်ခု ပေးထားတယ်။

    morning_shift = {"Aye", "Bo", "Chit", "Daw"}
    evening_shift = {"Chit", "Daw", "Eain", "Faw"}

    (က) shift နှစ်ခုလုံး ဆင်းရတဲ့ ဝန်ထမ်းတွေ၊ (ခ) မနက် shift ပဲ ဆင်းရတဲ့ ဝန်ထမ်းတွေ၊ (ဂ) ဝန်ထမ်း စုစုပေါင်း အရေအတွက် (duplicate မပါ) ကို set operation တွေနဲ့ ရှာပါ။

  3. (အလယ်အလတ်) အသုံးပြုသူကို အကြိုက်ဆုံး ရုပ်ရှင် 5 ကား ရိုက်ခိုင်းတဲ့ program ရေးပါ။ ရိုက်ထည့်တဲ့ နာမည်တွေကို set ထဲမှာ သိမ်းပါ (တူညီတဲ့နာမည် ထပ်ရိုက်မိရင် အလိုအလျောက် ဖယ်ရှားသွားမယ်)။ အဆုံးမှာ unique ရုပ်ရှင် အရေအတွက်နဲ့ စာရင်းကို print ထုတ်ပါ။

  4. (ခက်သော) remove() နဲ့ discard() ရဲ့ ကွာခြားချက်ကို သက်သေပြတဲ့ program ရေးပါ။ မရှိတဲ့ item တစ်ခုကို method နှစ်ခုလုံးနဲ့ ဖျက်ကြည့်ပါ။ remove() က error တက်တဲ့အခါ try/except နဲ့ ဖမ်းပြီး "Item not found, but the program continues." လို့ print ထုတ်ပါ။

  5. (စိန်ခေါ်မှု) စာကြောင်းတစ်ကြောင်း ရိုက်ထည့်ခိုင်းပြီး၊ အဲဒီထဲက စကားလုံးအမျိုးမျိုး (unique words) ဘယ်နှစ်လုံး ပါသလဲ တွက်တဲ့ program ရေးပါ။ (split() နဲ့ စာကြောင်းကို စကားလုံးတွေ ခွဲပြီး set ထဲ ထည့်ကြည့်ပါ။ စာလုံးအကြီးအသေး ကွာခြားမှုကို lower() နဲ့ ညှိပေးပါ။)