Set ဆိုသည်မှာ ဒေတာထည့်သွင်းသည့် ရှေ့နောက်နေရာ အစီအစဉ်တကျ မထားသည့် ဒေတာအမျိုးအစား(unordered collection data type)ဖြစ်သည်။ ထို့ကြောင့် index နံပါတ်များဖြင့် ဖတ်ယူရန်မဖြစ်(unindexed)နိုင်ပါ။ လိုအပ်လျှင် set တစ်ခု အတွင်း ဒေတာများကို ပြောင်းလဲနိုင်သည်။ (mutable ဖြစ်သည်။)။ set တစ်ခု အတွင်း၌ တူညီသည့် ဒေတာများ ရှိနေခွင့်မပြု။ (no duplicate elements)။ Set အတွင်းမှ ဒေတာများကို လျှင်မြန်စွာ ထုတ်ယူနိုင်သည်။ သင်္ချာမှ union, intersect စသည့် နှင့်သက်ဆိုင်သည့် operation များကို လုပ်နိုင်သည်။
my_set = {"apple", "banana", "cherry"}
နည်းနှစ်မျိုးဖြင့် ပြုလုပ်နိုင်သည်။ နည်း(၂)မျိုးဖြင့် set များကို တည်ဆောက်နိုင်သည်။
curly braces {} ထဲတွင် element များကို ကော်မာခံ၍ ရေးခြင်းနှင့် built-in set function ကို သုံးခြင်းတို့ဖြစ်သည်။ Set နှင့် dictionary နှစ်မျိုးလုံးကို ဖြင့်ရေးသည်။ dictionary အတွင်းရှိ item များသည် key-value pair များဖြစ်သောကြောင့် “:” ပါသည်။
သတိပြုရန်အချက်မှာ curly braces {} ကြောင့် empty set နှင့် empty dictionary ရောမှားနိုင်သည်။ Empty set တစ်ခုကို "a = {}" ဖြင့် ပြုလုပ်၍ ရနိုင်ပါ။ "a = {}" သည် dictionary သာဖြစ်သည်။ Empty set တစ်ခု ပြုလုပ်လိုပါက set() ကို သုံးရသည်။
my_set = {"apple", "banana", "cherry"} # curly braces {} ထဲတွင် element များကို ကော်မာခံ၍ ရေးသည်။
print(my_set)
# or use the set function and create from an iterable, e.g. list, tuple, string
my_set_2 = set(["one", "two", "three"])
print(my_set_2)
# careful: an empty set cannot be created with {}, as this is interpreted as dict
# use set() instead
a = {}
print(type(a))
a = set() # set() ကို သုံး၍ empty set ပြုလုပ်သည်။
print(type(a)) # set() ဖြစ်ကြောင်း စစ်သည်။
list, string, tuple¸ dictionary စသည့် data structure များကို set အဖြစ်သို့ပေါင်းနိုင်သည်။ တန်ဖိုးတူ ဒေတာများထဲမှ တစ်ခုကိုသာ သိမ်းဆည်းသည်။ (discarding any duplicate)
my_set4 = set( 'letters' ) #'e' နှင့် 't' တို့သည် နှစ်လုံးစီပါနေသောကြောင့် တစ်ခုကိုသာ သိမ်းဆည်းသည်။
my_set4
my_set_3 = set("aaabbbcccdddeeeeeffff")
print(my_set_3)
Set ထဲတွင် မိမိသိလိုသည့် item တစ်ခု ရှိ မရှိ (exist or not)ကို သိလိုလျှင် in key wordကို သုံး၍ စစ်နိုင်သည်။ in keyword သည် အသုံးအများဆုံး ဖြစ်သည်။
a = {1,2,3,4}
for num in a:
print(num)
အနည်းငယ်ခက်သည့် ဥပမာတစ်ခုဖြင့် ရှင်းပြမည်။ Drinks ဟုနာမည်ပေးထားသည့် dictionary တစ်ခုကို အသုံးပြု၍ ရှင်းပြမည်။ အရက်အမည်နှင့် ထိုထဲမှာ ပါဝင်သည့် အရာများကို dictionary တစ်ခုအဖြစ် တည်ဆောက်ထားသည်။ If statement နှင့် in key wordကို သုံး၍ set အတွင်း၌ ရှိနေသည့် အရက်အမည်များကို ဖော်ပြသည်။
drinks = {
'martini': {'vodka', 'vermouth'},
'black russian': {'vodka', 'kahlua'},
'white russian': {'cream', 'kahlua', 'vodka'},
'manhattan': {'rye', 'vermouth', 'bitters'},
'screwdriver': {'orange juice', 'vodka'}
}
drinks
for name, contents in drinks.items(): # name နှင့် contents တို့အစား မိမိကြိုက်သည့် အမည်ပေးနိုင်သည်။
if 'vodka' in contents:
print(name)
set.add() နှင့် update() method ကို အသုံးပြုနိုင်သည်။ ရှိပြီးသား set တစ်ခုအတွင်းသို့ ဒေတာများထည့်လိုသည့်အခါ set.add() method ကို အသုံးပြုနိုင်သည်။
my_set = set()
# use the add() method to add elements
my_set.add(42)
my_set.add(True)
my_set.add("Hello")
# note: the order does not matter, and might differ when printed
print(my_set)
# nothing happens when the element is already present:
my_set.add(42)
print(my_set)
တစ်ခုထက်ပိုများသည့် iterable item များကို set တစ်ခုအတွင်းသို့ ထည့်လိုသည့်အခါ update() method ကို အသုံးပြုသည်။
a = {1,2,3}
print(a)
a.update([3,4,5,6])
print(a)
pop() method set တစ်ခု အတွင်းမှ တစ်ခုတည်းကိုသာ ဖယ်ရှားလိုလျှင် pop() method ကို သုံးသည်။ Use the set's pop() method to remove and return an item from the set. Items are removed from the beginning of the set, as follows:
a = {1, 2, 3, 4}
print(a.pop())
print(a)
remove() ကို သုံး၍ ထဲမှ item ကို ဖယ်ထုတ်နိုင်သည်။ ဖယ်ထုတ်မည့် item သည် set ထဲတွင် မရှိလျှင် KeyError ပေလိမ့်မည်။
a = {1,2,3}
a.remove(3)
a
a.remove(3) #ဖယ်ထုတ်မည့် item သည် set ထဲတွင် မရှိလျှင် KeyError ပေလိမ့်မည်။
ထိုသို့ KeyError မဖြစ်ပေါ်စေရန်အတွက် discard()ကို အသုံးပြုသည်။ discard()သည် မရှိပါက KeyError မဖြစ်ပေါ်ပေ။ KeyError ဖြစ်ပေါ်ခြင်းကို ကျော်လွှားနိုင်သည်။
a = {1,2,3}
a.discard(2)
print(a)
a.discard("nonexistent item") # Set ထဲတွင် မရှိသည့် item ကို discard လုပ်ခိုင်းသည်။
print(a)
remove() နှင့် discard() method သည် item တစ်ခုချင်းစီကိုသာ ဖယ်ရှားနိုင်သည်။ item များ အားလုံးကို တစ်ပြိုင်နက် ဖယ်ရှားလိုပါက clear() method ကို အသုံးပြုရသည်။
a = {1,2,3,4,5,6}
print(a)
a.clear()
print(a) # set() သည် empty set ဖြစ်သည်။
# remove(x): removes x, raises a KeyError if element is not present
my_set = {"apple", "banana", "cherry"}
my_set.remove("apple")
print(my_set)
# KeyError:
# my_set.remove("orange")
# discard(x): removes x, does nothing if element is not present
my_set.discard("cherry")
my_set.discard("blueberry")
print(my_set)
# clear() : remove all elements
my_set.clear()
print(my_set)
# pop() : return and remove a random element
a = {True, 2, False, "hi", "hello"}
print(a.pop())
print(a)
my_set = {"apple", "banana", "cherry"}
if "apple" in my_set:
print("yes")
Set object များအတွင်းရှိ item များကို ဖတ်လိုသည့်အခါ index နံပါတ်ကို အသုံးပြုရန်မဖြစ်နိုင်ပါ။ (Set objects do not support indexes)။ list နှင့် tuple တို့တွင် index နံပါတ်ကိုအသုံးပြု၍(indexing နည်းဖြင့်) item များကို access လုပ်နိုင်သည်။
# Iterating over a set by using a for in loop
# Note: order is not important
my_set = {"apple", "banana", "cherry"}
for i in my_set:
print(i)
Union လုပ်သည့် set နှစ်ခုစလုံးတွင် ပါဝင်နေသည့် item များ သို့မဟုတ် element များ အားလုံးကို ယူရသည်။ Union လုပ်ပုံကို အောက်တွင် Venn diagram ဖြင့် သရုပ်ဖော်ထားသည်။ Set A နှင့် set B ကို Union လုပ်လျှင် set A နှင့် set B နှစ်ခုစလုံးမှ item များအားလုံး ပါဝင်သည့် set တစ်ခုကို ပြန်ပေးသည်။
odds = {1, 3, 5, 7, 9}
evens = {0, 2, 4, 6, 8}
primes = {2, 3, 5, 7,11}
# union() : combine elements from both sets, no duplication
# note that this does not change the two sets
u = odds.union(evens)
print(u)
u = odds.union(evens, primes) # Set (၃) ခု စလုံးဖြင့် union() လုပ်သည်။
print(u)
Set A နှင့် set B ကို intersection လုပ်လျှင် set A နှင့် set B နှစ်ခုစလုံးထဲတွင် ပါဝင်နေသည့် item များ ပါဝင်သည့် set တစ်ခုကို ပြန်ပေးသည်။
# intersection(): take elements that are in both sets
i = odds.intersection(evens)
print(i)
i = odds.intersection(primes)
print(i)
i = evens.intersection(primes)
print(i)
set_A = {1, 2, 3, 4, 5, 6, 7, 8, 9}
set_B = {1, 2, 3, 10, 11, 12}
# difference() : returns a set with all the elements from the setA that are not in setB.
diff_set = set_A.difference(set_B)
print(diff_set)
# A.difference(B) is not the same as B.difference(A)
diff_set = set_B.difference(set_A)
print(diff_set)
Python Set ၏ in-built function ဖြစ်သည်။ အဓိပ္ပာယ်မှာ set A နှင့် set B အတွင်းရှိ element များ အားလုံးထဲမှ set A နှင့် set B ထဲတွင် နှစ်ခုစလုံးထဲတွင်ပါဝင်နေသည့် element များကို နုတ်ထားသည့် set ဖြစ်သည်။
အစိမ်ရောင် ချယ်ထားသည့် set သည်symmetric difference set ဖြစ်သည်။ (Symmetric Difference is marked in Green)
# symmetric_difference() : returns a set with all the elements that are in setA and setB but not in both
diff_set = set_A.symmetric_difference(set_B)
print(diff_set)
# A.symmetric_difference(B) = B.symmetric_difference(A)
diff_set = set_B.symmetric_difference(set_A)
print(diff_set)
update() - SetB ထဲမှ element များကို setA ထဲသို့ပေါင်းထည့်သည်။
setA = {1, 2, 3, 4, 5, 6, 7, 8, 9}
setB = {1, 2, 3, 10, 11, 12}
# update() : Update the set by adding elements from another set.
#setB ထဲမှ element များကို setA ထဲသို့ပေါင်းထည့်သည်။
setA.update(setB)
print(setA)
intersection_update()
setA နှင့် setB တို့မှ နှစ်ခုစလုံးတွင် ပါသည့် element များကိုသာ ယူထားသည့် set ဖြစ်သည်။ (Update the set by keeping only the elements found in both)။ တစ်နည်းအားဖြင့်setA နှင့် setB တို့မှ နှစ်ခုစလုံးတွင် မပါသည့် element များကို နှုတ်ပြီးကျန်သည့် set ဖြစ်သည်။ (removes the items that is not present in both sets)
# intersection_update() : Update the set by keeping only the elements found in both
# The intersection_update() method removes the items that is not present in both sets
setA = {1, 2, 3, 4, 5, 6, 7, 8, 9}
setB = {1, 2, 3, 10, 11, 12}
setA.intersection_update(setB)
print(setA)
difference_update()
တခြား set တွင် ရှိသည့် element များကို မိမိ set ထဲမှ ဖယ်ထုတ်ခြင်းဖြင့် update လုပ်သည်။
# difference_update() : Update the set by removing elements found in another set.
setA = {1, 2, 3, 4, 5, 6, 7, 8, 9}
setA.difference_update(setB)
print(setA)
symmetric_difference_update()
Set နှစ်ခုစလုံးတွင် ပါဝင်နေသည့် element များဖယ်ထုတ်ပြီး set နှစ်ခုစလုံးမှ ကျန်သည့် element များကို ယူသည်။ update လုပ်သည်။
# symmetric_difference_update() : Update the set by only keeping the elements found in either set, but not in both
setA = {1, 2, 3, 4, 5, 6, 7, 8, 9}
setA.symmetric_difference_update(setB)
print(setA)
# Note: all update methods also work with other iterables as argument, e.g lists, tuples
# setA.update([1, 2, 3, 4, 5, 6])
ကော်ပီကူးနိုင်သည့် နည်းများမှာ .update(), .copy() တို့ဖြစ်သည်။
set_org = {1, 2, 3, 4, 5}
# this just copies the reference to the set, so be careful
set_copy = set_org
set_copy
# now modifying the copy also affects the original
set_copy.update([3, 4, 5, 6, 7])
print("Copy Set :", set_copy)
print("Original Set :", set_org)
# use copy() to actually copy the set
set_org = {1, 2, 3, 4, 5}
set_copy = set_org.copy()
set_copy
# now modifying the copy does not affect the original
set_copy.update([3, 4, 5, 6, 7])
print("Copy Set :", set_copy)
print("Original Set :", set_org)
setA = {1, 2, 3, 4, 5, 6}
setB = {1, 2, 3}
# Return True if all items setB are present in setA
print(setA.issubset(setB))
print(setB.issubset(setA)) # True - setB သည် setA ၏ အစိတ်အပိုင်းတစ်ခု ဖြစ်သည်။
set x မှ item များ အားလုံးသည် set y ထဲတွင် ရှိနေလျှင် ပြန်ပေးသည်။ (return True if all items set x are present in set y:)
x = {"a", "b", "c"}
y = {"f", "e", "d", "c", "b", "a"}
z = x.issubset(y)
print(z)
# issuperset(setX): Returns True if the set contains setX
print(setA.issuperset(setB)) # True
print(setB.issuperset(setA))
set x ထဲမှ item တစ်ခုမျှ set y ထဲတွင် မရှိလျှင် True ပြန်ပေးသည်။ (Return True if no items in set x is present in set y)
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "facebook"}
z = x.isdisjoint(y)
# isdisjoint(setX) : Return True if both sets have a null intersection, i.e. no same elements
setC = {7, 8, 9}
print(setA.isdisjoint(setB))
print(setA.isdisjoint(setC))
Frozen set ဆိုသည်မှာ ပြင်၍ ပြောင်း၍(immutable) set ဖြစ်သည်။ Frozen set ကို တည်ဆောက်ပြီးသည့်နောက် ပြင်၍ ပြောင်း၍ မရတော့ပေ။
Frozen set is just an immutable version of normal set. While elements of a set can be modified at any time, elements of frozen set remains the same after creation.
Creation with: my_frozenset = frozenset(iterable)
a = frozenset([0, 1, 2, 3, 4])
odds = frozenset({1, 3, 5, 7, 9})
evens = frozenset({0, 2, 4, 6, 8})
print(odds.union(evens))
print(odds.intersection(evens))
print(odds.difference(evens))
b = frozenset('asdfagsa')
print(b)
frozenset({'f', 'g', 'd', 'a', 's'})
cities = frozenset(["Frankfurt", "Basel","Freiburg"])
print(cities)
Passing a dictionary to the set() method will create a set of its key
print(dir(set))
Activity 26: Building a Set
Set များကို for loop သုံး၍ တည်ဆောက်နိုင်သည်။
squares = set() # အဖြေများကို ထည့်ရန် empty set တစ်ခု တည်ဆောက်သည်။
for i in range(5): # for loop
squares.add(i * i) # squares set ထဲသို့ ကိန်းကို နှစ်ထပ်တင်၍ တစ်ခုချင်းစီထည့်သည်။
print(squares) # ရလဒ်များပါသည့် squares list ကို ဖော်ပြသည်။
အထက်တွင် ဖော်ပြခဲ့သည့် ပို၍ ကျစ်လစ်သည့်နည်းဖြင့် ပြန်ရေးနိုင်သည်။ ist comprehension နည်းဖြစ်သည်။
# better: use list comprehension
# new_list = [expression for member in iterable]
squares = {i * i for i in range(5)}
print(squares)
# set comprehension
quote = "hello everybody"
unique_vowels = {i for i in quote if i in 'aeiou'}
print(unique_vowels)
squares = {i: i * i for i in range(5)}
print(squares)
#Set များကို for loop သုံး၍ တည်ဆောက်နိုင်သည်။
#{ expression for expression in iterable }
a_set = {number for number in range(1,6) if number % 3 == 1}
a_set
-End of Set-