List နှင့် tuple တို့တွင် sequence များ အကြောင်းကို လေ့လာခဲ့ပြီးနောက် mapping လုပ်ခြင်းအကြောင်း ဆက်လက်လေ့လာကြအောင်။ Sequence ဆိုသည်မှာ object များကို ၎င်းတို့၏ နေရာအစဉ်(by their relative position)နှင့် တွဲ၍ သိမ်းဆည်းထားခြင်း(store)ဖြစ်သည်။ (sequence stored objects by their relative position)။ Mapping ဆိုသည်မှာ object များကို ၎င်းတို့၏ key နှင့်တွဲ၍ သိမ်းဆည်းထားသည်။ Mapping လုပ်သည့် dictionary တွင် နေရာသည် အရေးကြီးသည့် အရာမဟုတ်ပေ။ Python dictionary တစ်ခုတွင် key နှင့် သက်ဆိုင်ရာ(associated) value တွဲထားသည့်အတွဲများ ပါဝင်သည်။ တခြားသော programming language များလေ့လာခဲ့ဘူးပါက Dictionary ကို hash table, associative array, hashe, or hashmap များ အဖြစ် မှတ်ယူနိုင်သည်။
Python dictionary သည် list နှင့် ခပ်ဆင်ဆင်တူညီသည်။ List တွင် နေရာအစဉ်(position order)သည် အဓိကကြသည်။ Dictionary တွင် key သည် အဓိကကြသည်။ Key သည် များသောအာဖြင့် string အမျိုးအစားဖြစ်သည်။ သို့သော် boolean, integer, float, tuple, string စသည့်အမျိုးအစား immutable item များ ဖြစ်နိုင်သည်။
Dictionary သည် ဒေတာများ ကိုထားနိုင်သည့် သိမ်းဆည်း data structure အမျိုးအစား တစ်ခု ဖြစ်သည်။ ဥပမာ - လူတစ်ဦး၏ အမည်နှင့် အသက်အရွယ်ကို structure အဖြစ် ဖွဲ့စည်းတည်ဆောက်၍ dictionary ပုံစံဖြင့် သိမ်းဆည်းထားနိုင်သည်။
Dictionary သည် ဒေတာများကို သိမ်းဆည်းထား( storing information)နိုင်သည့် data structure အမျိုးအစားတစ်ခု ဖြစ်သည်။ ဥပမာ - လူတစ်ဦး၏ အမည်နှင့် အသက်အရွယ်ကို structure ဖွဲ့စည်းတည်ဆောက်၍ dictionary ပုံစံဖြင့် သိမ်းဆည်းထားနိုင်သည်။
Dictionary ထဲတွင် ဒေတာများကို key-value pair ပုံစံမျိုးဖြင့် သိမ်းဆည်းသည်။ Dictionary ထဲတွင် သိမ်းဆည်းသည့် ဒေတာများသည် အစီအစဉ်တကျ သိုလှောင်ထားခြင်းမရှိပါ။ (Dictionaries do not store information in any particular order) ။ ထို့ကြောင့် သင်ထည့်သွင်းသည့် အစဉ်အတိုင်း ဒေတာများကို ပြန်လည်ရရှိလိမ့်မည်မဟုတ်ပါ။ Dictionary တွင် ပါရှိသည့် item များသည် key-value pair များ ဖြစ်ကြသည်။
Dictionary ထဲတွင် သိမ်းဆည်းသည့် ဒေတာများသည် အစီအစဉ်တကျ သိုလှောင်ထားခြင်းမရှိပါ။ (Dictionaries do not store information in any particular order) ။ ထို့ကြောင့် သင်ထည့်သွင်းသည့် အစဉ်အတိုင်း ဒေတာများကို ပြန်လည်ရရှိလိမ့်မည် မဟုတ်ပါ။ List တွင် ဒေတာများကို index နံပါတ် ဖြင့် access လုပ်သည်။ Dictionary တွင် ဒေတာများကို key ဖြင့် access လုပ်ခြင်းကြောင့် key သည် မှတ်မိရန် ပိုလွယ်ကူသည်။
Dictionary ထဲတွင် ဒေတာများကို order သို့မဟုတ် position နှင့် မသိမ်းဆည်းထားသောကြောင့် indexing လုပ်သည့်အခါ index နံပါတ်များ ထည်ပေး၍ မရပါ။ Dictionary တွင် index လုပ်သည့်အခါ key ထည်ပေးရပါသည်။ အဘယ်ကြောင့်ဆိုသော် ဒေတာများကို key-value pair ပုံစံမျိုးဖြင့် သိမ်းဆည်းသောကြောင့် ဖြစ်သည်။
Python dictionary ကို အတိုခေါက်အားဖြင့် dict ဟူ၍လည်းခေါ်လေ့ရှိသည်။ Unordered dictionary(dict) နှင့် ordered dictionary ဟူ၍ dictionary(OrderedDict) နှစ်မျိုးရှိသည်။ Unordered dictionary(dict) သည် default ဖြစ်သည်။ OrderedDict တွင် key-value pair ၏ ထည့်ပေးသည့် အစဉ်(order of insertion)ကို မှတ်ထားသည်။
ဒေတာများကို key-value pair ပုံစံမျိုးဖြင့် အောက်ပါအတိုင်း သိမ်းဆည်းသည်။
dictionary_name = {key_1: value_1, key_2: value_2, key_3: value_3}
dictionary_name = {key_1: value_1, key_2: value_2, key_3: value_3}
တွန့်ကွင်း curly brackets ({}) အတွင်း၌ key : value pairs များကို ကော်မာဖြင့် ခွဲရေးပြီး(comma-separated) ထည့်သွင်းထားခြင်းဖြင့် dictionary တည်ဆောက်နိုင်သည်။ အောက်ပါအတိုင်း ရေးလျှင်ပို၍ ဖတ်ရလွယ်ပြီး နားလည် သဘောပေါက်နိုင်သည်။ Python ၏ list, tuple,or dictionary စသည်တို့ အတွင်းရှိ item များကို ရေးသည့်အခါ နောက်ဆုံးအလုံး၏ နောက်တွင် comma ချန်ထားခဲ့ပြီး ရေးနိုင်သည်။
dictionary_name = {
"key_1": "value_1",
"key_2": "value_2",
"key_3": "value_3",
}
Dictionary တည်ဆောက်ခြင်း နည်းနှစ်မျိုးဖြင့် dictionary များကို တည်ဆောက်နိုင်သည်။ (၁) တွန့်ကွင်း (curly brackets { }) ထဲတွင် key-value pair များကို ကော်မာခံ(comma)ရေး၍ Python dictionary တည်ဆောက်နိုင်သည်။ key-value pair ကို ရေးသည့်အခါ key : value ပုံစံမျိုးဖြင့် ရေးသားသည်။ (၂) dict() function ကို သုံး၍ တည်ဆောက်နိုင်သည်။
#empty dictionary
empty_dict = {}
empty_dict
# Use the dict() or curly bracket notation to create a dictionary and assign it to a variable, d.
d = dict(
state="NY",
city="New York"
)
d
dict() function ဖြင့် dictionary တည်ဆောက်သည့်အခါ = operator ကိုသုံး၍ key နှင့် value များကို တွဲပေးရသည်။ state နှင့် city သည် key များ ဖြစ်ကြသည် ။ NY နှင့် New York တို့သည့် key နှင့်သက်ဆိုင်သည့် value များ ဖြစ်ကြသည်။
# Make a dictionary with {} and : to signify a key and a value
my_dict = {'key1':'value1','key2':'value2' }
print(my_dict)
# တည်ဆောက်လိုက်သည့် dictionary ၏ အမျိုးအစားကို built-in type() function စစ်နိုင်သည်။
print(type(my_dict))
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)
#Get the value of the "model" key:
x = thisdict["model"]
x
# get() method ကို အသုံးပြုနိုင်သည်။ အဖြေတူသည်။
# Get the value of the "model" key:
x = thisdict.get("model")
x
Dictionary အတွင်းရှိ value များကို ပြောင်းနိုင်သည်။ (Change Values)
# You can change the value of a specific item by referring to its key name:
# Change the "year" to 2018:
car_dict = { "brand": "Ford", "model": "Mustang", "year": 1964 }
car_dict["year"] = 2018
car_dict
dict() function ကို အသုံးပြု၍ value နှစ်ခု ပါသည့် ကိန်းအစဉ်အတန်း(two-value sequences)ကို dictionary တစ်ခု အဖြစ် ပြောင်းနိုင်သည်။
# example using lol (a list of two-item lists):
lol = [ ['a', 'b'], ['c', 'd'], ['e', 'f'] ]
dict(lol)
# sequence containing two-item sequences ကို dictionary တစ်ခု အဖြစ် ပြောင်းနိုင်သည်။
# A list of two-item tuples ကို dictionary တစ်ခု အဖြစ် ပြောင်းပုံကို ဥပမာ အဖြစ် ဖော်ပြထားသည်။
lot = [ ('a', 'b'), ('c', 'd'), ('e', 'f') ]
dict(lot)
# A list of two-character strings ကို dictionary တစ်ခု အဖြစ် ပြောင်းပုံကို ဥပမာ အဖြစ် ဖော်ပြထားသည်။
los = [ 'ab', 'cd', 'ef' ]
dict(los)
# A tuple of two-character strings ကို dictionary တစ်ခု အဖြစ် ပြောင်းပုံကို ဖော်ပြထားသည်။
tos = ( 'ab', 'cd', 'ef' )
dict(tos)
dictionary ထဲတွင် key များ၏ အစဥသည် (order of keys) ကြိုက်သလို(arbitrary) ဖြစ်နိုင်သည်ကို သတိပြုပါ။
Dictionary အတွင်းရှိ key များကို တစ်ခုချင်းစီ ဖော်ပြပါ။
car_dict = { "brand": "Ford", "model": "Mustang", "year": 1964 }
for x in car_dict:
print(x)
Dictionary အတွင်းရှိ key နှင့် value များကို တွဲ၍ တစ်ခုချင်းစီ ဖော်ပြပါ။
car_dict = { "brand": "Ford", "model": "Mustang", "year": 1964 }
for x in car_dict:
print(x,":" ,thisdict[x], )
key ကို Dictionary ထဲရှိ item များ အသစ်ထည့်ခြင်း၊ ရှိပြီးသားများကိုပြောင်းခြင်
dictionary_name = { "key_1": "value_1", "key_2": "value_2", "key_3": "value_3"}
dictionary_name
# [key] ကို သုံး၍ "key_4" နှင့် "value_4" ကို key-value pair အသစ် အဖြစ် ထည့်သည်။
dictionary_name["key_4" ] ="value_4"
print(dictionary_name)
Key-value pair တစ်ခုကို dictionary သို့ထည့်သည့်အခါ dictionary ထဲတွင် key သည် ရှိပြီးသားဖြစ်လျှင် ရှိပြီးသာ(existing) value ကို value အသစ်ဖြင့် ပြောင်းလဲ အစားထိုးသည်။ Dictionary ထဲတွင် key မရှိလျှင် ထည့်ပေးသည်။ List နှင့် မတူသည့်အချက်တစ်ခု ဖြစ်သည်။
Dictionary တစ်ခုအတွင်း၌ တစ်ခုနှင့် တစ်ခု မတူညီသည့် key များသာ ရှိရမည်။ (Dictionary keys must be unique.)
# [key] ကို သုံး၍ "value_2" ကို "value_TWO" အဖြစ်သို့ ပြောင်းသည်။
dictionary_name["key_2" ] ="value_TWO"
print(dictionary_name)
update() function ကို သုံး၍ dictionary တစ်ခုမှ key-value pair များကို တခြား dictionary တစ်ခုထဲသို့ ကော်ပီကူးထည့်နိုင်သည်။ အောက်တွင် dict_2 မှ key-value pair များကို dict_1 သို့ ကော်ပီကူးထည့်သည်။
dict_1 = {'key_1': 'value_1', 'key_2': 'value_2', 'key_3': 'value_3'}
dict_2 = {'key_4': 'value_4', 'key_5': 'value_5', 'key_6': 'value_6'}
dict_1.update(dict_2)
print(dict_1)
ဒီလိုမျိုး update() နဲ့ dictionary နှစ်ခုကို ပေါင်း(merge)တဲ့အခါ dictionary နှစ်ခု စလုံးမှာ တူညီတဲ့ key တွေ ပါရှိနေရင်ဘယ်လို လုပ်မလဲ။ ဒုတိယ(နောက်က) dictionary ကို အနိုင်ပေးပါတယ်။ ဥပမာ ဒုတိယ(နောက်က) dictionary က key 'b' ရဲ့ value 'platypus' ကိုပဲ update လုပ်ပါတယ်။
first = {'a': 1, 'b': 2}
second = {'b': 'platypus'}
first.update(second)
first
del key word ကိုသုံး၍ key နာမည်ဖြင့် dictionary ထဲမှ item ကို ဖျက်(Delete)ခြင်း
dict_1 = {'key_1': 'value_1', 'key_2': 'value_2', 'key_3': 'value_3'}
del dict_1['key_1']
dict_1
Dictionary ထဲမှ key များ အားလုံးနှင့် value များ အားလုံးကို ဖျက်လိုလျှင် clear() ကို သုံးနိုင်သည် သို့မဟုတ် empty dictionary ({}) ဖြင့် dictionary name ကို reassign အလုပ်နိုင်သည်။
dict_1.clear()
dict_1
dict_1 = {} #empty dictionary ({}) ဖြင့် dictionary name ကို reassign အလုပ်နိုင်သည်။
Dictionary ထဲတွင် မိမိသိလိုသည့် key တစ်ခု ရှိ မရှိ (exist or not) သိလိုလျှင် in key wordကို သုံး၍ စစ်နိုင်သည်။
dict_1 = {'key_1': 'value_1', 'key_2': 'value_2', 'key_3': 'value_3'}
'key_1' in dict_1 # True
'key_5' in dict_1 #False
car_dict = { "brand": "Ford", "model": "Mustang", "year": 1964 }
if "model" in car_dict:
print("Yes, 'model' is one of the keys in the thisdict dictionary")
ဤနည်းသည် dictionary တွင် အသုံးအများဆုံး ဖြစ်သည်။ key ထည့်ပေး၍ ၎င်းနှင့် သက်ဆိုင်သည့် (corresponding) value ကို ရယူနိုင်သည်။
dict_1 = {'key_1': 'value_1', 'key_2': 'value_2', 'key_3': 'value_3'}
dict_1['key_1']
Dictionary ထဲတွင် မိမိ ထည့်ပေးသည့် key မရှိပါက exception (KeyError) ပြန်ပေးလိမ့်မည်။
dict_1['key_5'] # KeyError ပြန်ပေးလိမ့်မည်။
Dictionary ထဲတွင် မိမိ ထည့်ပေးသည့် key မရှိပါက exception ပြန်ပေးလိမ့်မည်။ ထိုသို့ exception ပြန်ပေးခြင်း မဖြစ်ပေါ်စေရန် မိမိ ထည့်ပေးမည့် key ရှိ မရှိ စစ်နိုင်သည်။ သို့မဟုတ် get() function ကို အသုံးပြုနိုင်သည်။
dict_1.get('key_1')
key ကို အသုံးပြု၍ value များကို access လုပ်နိုင်သည်။ Dictionary က သိမ်းဆည်းနိုင်သည့် ဒေတာအမျိုးအစားများသည် ပြောင်းလွယ်ပြင်လွယ်(flexible)ဖြစ်သည်။ ဥပမာ:
my_dict = {'key1':123,'key2':[12,23,33],'key3':['item0','item1','item2']}
# Call values by their key
my_dict['key2']
my_dict = {'key1':123,'key2':[12,23,33],'key3':['item0','item1','item2']}
# Let's call items from the dictionary
my_dict['key3']
# Can call an index on that value
my_dict['key3'][0]
# Can then even call methods on that value
my_dict['key3'][0].upper()
my_dict['key1']
အောက်ပါကဲ့သို့ ပေါင်းခြင်း အနုတ်ခြင်း မြှောက်ခြင်း စသည်တို့ပြုလုပ်ရန် built-in method ပါရှိသည်။ ဥပမာ:
# Subtract 123 from the value
my_dict['key1'] = my_dict['key1'] - 123
#Check
my_dict['key1']
# Set the object equal to itself minus 123
my_dict['key1'] -= 123
my_dict['key1']
Dictionary ထဲမှ key အားလုံးကို တပြိုင်နက် access လုပ်ရန် key() ကိုသုံးနိုင်သည်။ ဥပမာ
dict_1.keys()
list() function ကို သုံး၍ dictionary ထဲမှ key များ ၊ value များနှင့် item များကို list အဖြစ်သို့ ပြောင်းနိုင်သည်။ (the list() function to turn the results of values() and items() into normal Python lists.)
list(dict_1.keys()) # dict_keys object အားလုံးကို list တစ်ခု အဖြစ်ပြောင်းသည်။
Dictionary ထဲမှ value အားလုံးကို တပြိုင်နက် ရယူရန် values()function ကိုသုံးသည်။
list(dict_1.values()) # Dictionary ထဲမှ value များအားလုံးကို list တစ်ခု အဖြစ်ထုတ်ယူသည်။
car_dict = { "brand": "Ford", "model": "Mustang", "year": 1964 }
for x in car_dict.values():
print(x)
Dictionary ထဲမှ key-value pair အားလုံးကို အတွဲလိုက် ရယူရန် items() function ကိုသုံးသည်။
list(dict_1.items()) # Dictionary ထဲမှ key-value pair များအားလုံးကို list တစ်ခု အဖြစ်ထုတ်ယူသည်။
car_dict = { "brand": "Ford", "model": "Mustang", "year": 1964 }
for x, y in car_dict.items():
print(x, ":" ,y)
= operator ဖြင့် assign လုပ်သည်။ copy()ဖြင့် ကော်ပီကူးသည်။
signals = {'green': 'go', 'yellow': 'go faster', 'red': 'smile for the camera'}
save_signals = signals # = operator ဖြင့် assign လုပ်သည်။
print(save_signals)
signals['blue'] = 'confuse everyone'
print(save_signals)
dictionary တစ်ခုအတွင်းမှ key-value pair များကို တခြား dictionary တစ်ခုအဖြစ် ကော်ပီကူးလိုလျှင် copy()ကို သုံးသည်။
dict_1 = {'key_1': 'value_1', 'key_2': 'value_2', 'key_3': 'value_3'}
dict_2 = {'key_4': 'value_4', 'key_5': 'value_5', 'key_6': 'value_6'}
dict_2 = dict_1.copy()
print(dict_1)
print(dict_2)
Key နှင့် ကို တစ်ခုချင်းစီ သီးခြားထည့်သည့်နည်းဖြစ်သည်။
# Create a new dictionary
d = {}
# assign လုပ်၍ key အသစ်များ ထပ်ထည့်ခြင်း (Create a new key through assignment)
d['animal'] = 'Dog'
# Can do this with any object
d['answer'] = 42
#
d
Dictionary တစ်ခုထဲတွင် တခြားသော dictionary များ အလွှာလိုက် အထပ်ထပ် ရှိနေနိုင်သည်။ ထိုသို့ dictionary တစ်ခုထဲတွင် တခြားသော dictionary များ ရှိနေခြင်းကြောင့် Nested dictionary ဟု ခေါ်ဆိုခြင်းဖြစ်သည်။
# Dictionary nested inside a dictionary nested inside a dictionary
d = {'key1':{'nestkey':{'subnestkey':'value'}}}
d
Nested dictionary ထဲမှ value များ မည်ကဲ့သို့ ထုတ်ယူနိုင်သည်ကို အောက်တွင် ဖော်ပြထားသည်။ Main dictionary မှ ['key1'] ၊ ဒုတိယ dictionary မှ ['nestkey'] ၊ တတိယအဆင့် dictionary မှ ['subnestkey'] စသည့်ဖြင့် key များကို အဆင့်ဆင့် ထည့်ပေးရသည်။
# Keep calling the keys
d['key1']['nestkey']['subnestkey']
Nested Dictionaries အောက်တွင် dictionary တစ်ခုတွင် sub dictionary (၃)ခု ပါဝင်သည့် Nested dictionary တစ်ခုကို ဥပမာအဖြစ် ဖော်ပြထားသည်။
myfamily = {
"child1" : {
"name" : "Emil",
"year" : 2004
},
"child2" : {
"name" : "Tobias",
"year" : 2007
},
"child3" : {
"name" : "Linus",
"year" : 2011
}
}
myfamily
# dictionary တစ်ခု တည်ဆောက်သည်။
d = {'key1':1,'key2':2,'key3':3}
# .keys() ဖြင့် dictionary အတွင်းရှိ key များ အားလုံးကို ကြည့်နိုင်သည်။ ဖော်ပြနိုင်သည်။
d.keys()
# .values .() ဖြင့် dictionary အတွင်းရှိ value များ အားလုံးကို ကြည့်နိုင်သည်။ ဖော်ပြနိုင်သည်။
d.values()
.items() ဖြင့် အတွင်းရှိ item များ အာလုံး ဖော်ပြနိုင်သည်။ ကြည့်နိုင်သည်။ Dictionary တွင် ပါရှိသည့် item များသည် key-value pair များ ဖြစ်ကြသည်။
# Method to return tuples of all items (we'll learn about tuples soon)
d.items()
Dictionary တစ်ခုပြုလုပ်ပြီးသည့်နှင့် တပြိုက်နက် dictionary object တစ်ခု ပြုလုပ်ပြီးဖြစ်သည်။ dictionary object တွင် ရှိသည့် attributes နှင့် properties တို့ကို built-in dir() function သုံး၍ ကြည့်နိုင်သည်။
dictionary = {}
print(dir(dictionary))
built-in isinstance() function ကိုသုံး၍ dictionary သည် dict class ၏ instance တစ်ခု ဖြစ်ကြောင်း စစ်ဆေးနိုင်သည်။
isinstance(dictionary, dict)
dict function ကိုသုံး၍ Dictionary တစ်ခုအတွင်းသို့ ဒေတာများ ထည့်ခြင်း dict() function ကိုသုံးသည့်အခါ = operator ဖြင့် keys များ value များကို တွဲ၍ assign လုပ်သည်။
dictionary1 = dict( state="NY",
city="New York")
print(dictionary1)
{ } ကို သုံးလျှင် key နှင့် value အကြားတွင် : ခံ၍ ရေးသည်။
dictionary2 = {
"state": "Maryland", "city": "Baltimore"
}
print(dictionary2)
တည်ဆောက်ပြီးသား တစ်ခုထဲသို့ key ကိုသုံး၍ value ကို ထည့်ပေး(Assign လုပ်) နိုင်သည်။ (2. Assign values to existing dictionaries using keys)။ ဥပမာ အထက်က ထဲသို့ 'bird': 'Baltimore oriole'ကို ထည့်သည်။ key သည်'bird' ဖြစ်သည်။ value သည် 'Baltimore oriole' ဖြစ်သည်။
dictionary2['bird'] = 'Baltimore oriole'
dictionary2
Dictionary မှ value များကို key ဖြင့် ဖတ်ယူနိုင်သည်။
print(dictionary1['state'])
ထိုသို့ ဖတ်ယူသည့်အခါ ထည့်ပေးသည့် key သည် dictionary ထဲတွင် မရှိလျှင် KeyError ပေလိမ့်မည်။
print(dictionary1['age'])
KeyError မဖြစ်ပေါ်စေရန်အတွက် get() function ကို အသုံးပြုသည်။ get() function ဖြင့် ဖတ်သည့်အခါ ထည့်ပေးသည့် key မရှိပါက None ပြန်ပေးသည်။ KeyError ဖြစ်ပေါ်ခြင်းကို ကျော်လွှားနိုင်သည်။
print(dictionary1.get('age'))
Dictionary အတွင်းရှိ key နှင့် value များ တစ်တွဲချင်းစီကို iterate လုပ်ခြင်း for loop ကို သုံး၍ dictionary အတွင်းရှိ key နှင့် value များ တစ်တွဲချင်းစီကို iterate လုပ်ခြင်းသည်် အရိုးရှင်း ဆုံးနည်းဖြစ်သည်။
dictionary1 = dict( state="NY", city="New York")
for item in dictionary1: print(item)# for loop ဖြင့် get dictionary's keys ကို iterate လုပ်ခြင်း
Dictionary ထဲရှိ key သို့မဟုတ် value သို့မဟုတ် နှစ်မျိုးစလုံးကို iterate လုပ်နိုင်သည်။ keys() method ကို သုံး၍ key များကို ထုတ်ယူနိုင်သည်။ values() method ကို သုံး၍ value များကို ထုတ်ယူနိုင်သည်။
dictionary1 = dict( state="NY",
city="New York"
)
for item in dictionary1.keys(): print(item)
for item in dictionary1.values(): print(item) # dictionary1 = {'state': 'NY', 'city': 'New York'}
key နှင့် value နှစ်မျိုးစလုံးကို တစ်ပြိုင်နက် iterate လုပ်နိုင်သည်။ ထုတ်ယူနိုင်သည်။
for key, value in dictionary1.items(): print(key, ':', value) # dictionary1 = {'state': 'NY', 'city': 'New York'}
for loop ကို သုံး၍ dictionary အတွင်း၌ iterating လုပ်စရာမလိုဘဲ in keyword ကို သုံး၍ မိမိသိလိုသည့် key တစ်ခုသည် dictionary ထဲတွင် ရှိမရှိကို စစ်နိုင်သည်။ ရှိလျှင် True(Boolean value) သို့မဟုတ် မရှိလျှင် False (Boolean value) ကို ပြန်ပေးလိမ့်မည်။
a = {
"size": "10 feet", "weight": "16 pounds"
}
print("size" in a)
print("length" in a)
dictionary အတွက် dir() function ကို သုံးလျှင် attribute တချို့ကို တွေ့နိုင်သည်။
print(dir(a))
အောက်တွင် method တချို့ကို ဥပမာဖြင့် ရှင်းပြထားသည်။
dictionary ထဲသို့ key-value pair အသစ်များထည့်လိုသည့်အခါ .update() method ကိုသုံးသည်။ empty dictionary တစ်ခုထဲသို့ .update() method ဖြင့် key-value pair အသစ် တစ်ခု ထည့်ပုံကို ဥပမာအဖြစ် ပြထားသည်။
a = {}
a.update({"name": "Dan Brown"})
a
ရှိပြီးသား key ဖြစ်လျှင် ထပ်ခါ ထပ်ခါ key-value pair များကို ထပ်မထည့်ပါ။ ထို့ကြောင့် .update() function သည် dictionary နှစ်ခုကို ပေါင်း(merge)ပြီး မတူညီသည့်(different) key များကို ရလိုသည့်အခါ အသုံးဝင်သည်။ ရှိပြီးသား key-value pair ကို ထပ် မထည့်ပေးသောကြောင့် ဖြစ်သည်။
clear()
dict.clear() and dict.pop() dictionary အတွင်းမှ key များ အားလုံးကို ဖယ်ရှား ရှင်းထုတ်လိုသည့်အခါ clear() method ကို သုံးသည်။
a = {'name': 'Dan Brown Xavier'}
a.clear()
a
del keyword
key-value pair တစ်ခုတည်းကို ဖယ်ထုတ်လိုလျှင် del keyword ကို သုံးသည်။
a = {"name": "Skandar Keynes", "age": "24"}
del a["name"]
a
pop()
Dictionary ထဲမှ key-value pair ကို ဖယ်ထုတ်ပြီး ထိုဖယ်ထုတ်လိုက်သည့် key-value pair ကို ကိစ္စ တစ်ခုခု အတွက် အသုံးလိုလျှင် pop() ကိုသုံးသည်။
a = {"name": "Skandar Keynes", "age": "24"}
b = a.pop("name")
print(a)
print(b)
dict.copy()
shallow copy အဖြစ် ကော်ပီ ကူးလိုသည့်အခါ copy method ကို သုံးသည်။ ကော်ပီကူးပြီးသည့်အခါ
မူလ ဒေတာနှင့် ဘာမှ မသက်ဆိုင်တော့သည့် object အသစ်များ ဖြစ်သွားသည်။
a = {"name": "Skandar Keynes", "age": "24"}
b = a.copy()
print(a)
print(b)
a["name"] = "Janet Jackson"
print(a)
print(b) # b သည် a ၏ shallow copy ဖြစ်သည်။
အထက်က ဥပမာတွင် b သည် a ၏ shallow copy ဖြစ်သည်။ dictionary a မှ key-value pair အားလုံးကို dictionary b အဖြစ် ကော်ပီကူးပေးပြီး အသစ်တစ်ခု ပြလုပ်ပေးသည်။ a ကို update လုပ်သည့်အခါ b သည် ဘာမှ မပြောင်းလဲပါ။ သီးခြားလွတ်လပ်သည့် dictionary နှစ်ခုအဖြစ် ရှိနေလိမ့်မည်။
= operator ကို အသုံးပြုပါက deep copy အဖြစ် ကော်ပီကူးပေးသည်။ a နှင့် b တို့သည် တူညီသည့် ဒေတာများကို ရည်ညွန်းသည်။ ထို့ကြောင့် တစ်ခုကို update လုပ်လျှင် တခြားတစ်ခုသည် လိုက်၍ ပြောင်းလဲလိမ့်မည်။
a = {"name": "Skandar Keynes", "age": "24"}
b = a
a["name"] = "Janet Jackson"
b["age"] = 16
print(a)
print(b)
popitem() method သည် dictionary ထဲမှ random item ထုတ်ယူပေးသည်။ ထိုသို့ထုတ်ယူပေးပြီးနောက် dictionary ထဲမှ ဖျက်လိုက်သည်။
a = {"name": "Skandar Keynes", "age": "24", "sex": "male"}
a.popitem()
a
a = {"name": "Skandar Keynes", "age": "24", "sex": "male"}
b = a.setdefault("name")
a
b
သမာန် dictionary သည် ထည့်ပေးသည့် ဒေတာများ၏ အစဥ◌်ကို သိမ်းဆည်းထားလေ့ မရှိပေ။ (do not maintain the insertion order of the key-value pairs)။
ဒေတာများ၏ ထည့်ပေးသည့် အစဥ◌်(insertion order of keys)ကို ထိန်းသိမ်းထားလိုပါက သို့မဟုတ် ထည့်ထားပေးသည့် အစဥ◌အတိုင်း dictionary ထဲတွင် ရှိနေစေလိုပါက Ordered dictionary ကို အသုံးပြုရသည်။ (Ordered dictionaries are dictionaries that maintain the insertion order of keys.)
Ordered dictionary ကို iterate လုပ်သည့်အခါ ထည့်ပေးသည့် အစဥ အတိုင်း key များကို access လုပ်နိုင်သည်။
End