Python Dictionaries

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 ဆိုတာဘာလဲ?

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",
}

Constructing a Dictionary

Dictionary တည်ဆောက်ခြင်း နည်းနှစ်မျိုးဖြင့် dictionary များကို တည်ဆောက်နိုင်သည်။ (၁) တွန့်ကွင်း (curly brackets { }) ထဲတွင် key-value pair များကို ကော်မာခံ(comma)ရေး၍ Python dictionary တည်ဆောက်နိုင်သည်။ key-value pair ကို ရေးသည့်အခါ key : value ပုံစံမျိုးဖြင့် ရေးသားသည်။ (၂) dict() function ကို သုံး၍ တည်ဆောက်နိုင်သည်။

In [1]:
 #empty dictionary 
empty_dict = {}
empty_dict
Out[1]:
{}
In [2]:
# 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
Out[2]:
{'state': 'NY', 'city': 'New York'}

dict() function ဖြင့် dictionary တည်ဆောက်သည့်အခါ = operator ကိုသုံး၍ key နှင့် value များကို တွဲပေးရသည်။ state နှင့် city သည် key များ ဖြစ်ကြသည် ။ NY နှင့် New York တို့သည့် key နှင့်သက်ဆိုင်သည့် value များ ဖြစ်ကြသည်။

In [3]:
# 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))
{'key1': 'value1', 'key2': 'value2'}
<class 'dict'>
In [4]:
thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
print(thisdict)
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
In [5]:
#Get the value of the "model" key:
x = thisdict["model"]
x 
Out[5]:
'Mustang'
In [6]:
# get() method ကို အသုံးပြုနိုင်သည်။ အဖြေတူသည်။ 
# Get the value of the "model" key:
x = thisdict.get("model")
x
Out[6]:
'Mustang'

Dictionary အတွင်းရှိ value များကို ပြောင်းနိုင်သည်။ (Change Values)

In [7]:
# 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
Out[7]:
{'brand': 'Ford', 'model': 'Mustang', 'year': 2018}

Convert by Using dict()

dict() function ကို အသုံးပြု၍ value နှစ်ခု ပါသည့် ကိန်းအစဉ်အတန်း(two-value sequences)ကို dictionary တစ်ခု အဖြစ် ပြောင်းနိုင်သည်။

In [8]:
# example using lol (a list of two-item lists):
lol = [ ['a', 'b'], ['c', 'd'], ['e', 'f'] ]
dict(lol)
Out[8]:
{'a': 'b', 'c': 'd', 'e': 'f'}
In [9]:
#  sequence containing two-item sequences ကို dictionary တစ်ခု အဖြစ် ပြောင်းနိုင်သည်။ 
#  A list of two-item tuples ကို dictionary တစ်ခု အဖြစ် ပြောင်းပုံကို ဥပမာ အဖြစ် ဖော်ပြထားသည်။ 
lot = [ ('a', 'b'), ('c', 'd'), ('e', 'f') ]
dict(lot)
Out[9]:
{'a': 'b', 'c': 'd', 'e': 'f'}
In [10]:
#  A list of two-character strings ကို dictionary တစ်ခု အဖြစ် ပြောင်းပုံကို ဥပမာ အဖြစ် ဖော်ပြထားသည်။ 
los = [ 'ab', 'cd', 'ef' ]
dict(los)
Out[10]:
{'a': 'b', 'c': 'd', 'e': 'f'}
In [11]:
#  A tuple of two-character strings ကို  dictionary တစ်ခု အဖြစ် ပြောင်းပုံကို ဖော်ပြထားသည်။ 
tos = ( 'ab', 'cd', 'ef' )
dict(tos)
Out[11]:
{'a': 'b', 'c': 'd', 'e': 'f'}

dictionary ထဲတွင် key များ၏ အစဥသည် (order of keys) ကြိုက်သလို(arbitrary) ဖြစ်နိုင်သည်ကို သတိပြုပါ။

Dictionary အတွင်းရှိ key များကို တစ်ခုချင်းစီ ဖော်ပြပါ။

In [88]:
car_dict = { "brand": "Ford", "model": "Mustang", "year": 1964 }
for x in car_dict:
  print(x)
brand
model
year

Dictionary အတွင်းရှိ key နှင့် value များကို တွဲ၍ တစ်ခုချင်းစီ ဖော်ပြပါ။

In [13]:
car_dict = { "brand": "Ford", "model": "Mustang", "year": 1964 }
for x in car_dict:
  print(x,":" ,thisdict[x], )
brand : Ford
model : Mustang
year : 1964

[key] ဖြင့် item များ အသစ်ထည့်ခြင်း၊ ရှိပြီးသားများကိုပြောင်းခြင်း

key ကို Dictionary ထဲရှိ item များ အသစ်ထည့်ခြင်း၊ ရှိပြီးသားများကိုပြောင်းခြင်

In [14]:
dictionary_name = { "key_1": "value_1", "key_2": "value_2", "key_3": "value_3"}
dictionary_name
Out[14]:
{'key_1': 'value_1', 'key_2': 'value_2', 'key_3': 'value_3'}
In [15]:
# [key] ကို သုံး၍  "key_4" နှင့် "value_4" ကို key-value pair အသစ် အဖြစ် ထည့်သည်။ 
dictionary_name["key_4" ] ="value_4"
print(dictionary_name)
{'key_1': 'value_1', 'key_2': 'value_2', 'key_3': 'value_3', 'key_4': 'value_4'}

Key-value pair တစ်ခုကို dictionary သို့ထည့်သည့်အခါ dictionary ထဲတွင် key သည် ရှိပြီးသားဖြစ်လျှင် ရှိပြီးသာ(existing) value ကို value အသစ်ဖြင့် ပြောင်းလဲ အစားထိုးသည်။ Dictionary ထဲတွင် key မရှိလျှင် ထည့်ပေးသည်။ List နှင့် မတူသည့်အချက်တစ်ခု ဖြစ်သည်။
Dictionary တစ်ခုအတွင်း၌ တစ်ခုနှင့် တစ်ခု မတူညီသည့် key များသာ ရှိရမည်။ (Dictionary keys must be unique.)

In [16]:
# [key] ကို သုံး၍ "value_2" ကို "value_TWO" အဖြစ်သို့ ပြောင်းသည်။ 
dictionary_name["key_2" ] ="value_TWO"
print(dictionary_name)
{'key_1': 'value_1', 'key_2': 'value_TWO', 'key_3': 'value_3', 'key_4': 'value_4'}

update() ဖြင့် dictionary နှစ်ခု ပေါင်းခြင်း

update() function ကို သုံး၍ dictionary တစ်ခုမှ key-value pair များကို တခြား dictionary တစ်ခုထဲသို့ ကော်ပီကူးထည့်နိုင်သည်။ အောက်တွင် dict_2 မှ key-value pair များကို dict_1 သို့ ကော်ပီကူးထည့်သည်။

In [17]:
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)
{'key_1': 'value_1', 'key_2': 'value_2', 'key_3': 'value_3', 'key_4': 'value_4', 'key_5': 'value_5', 'key_6': 'value_6'}

ဒီလိုမျိုး update() နဲ့ dictionary နှစ်ခုကို ပေါင်း(merge)တဲ့အခါ dictionary နှစ်ခု စလုံးမှာ တူညီတဲ့ key တွေ ပါရှိနေရင်ဘယ်လို လုပ်မလဲ။ ဒုတိယ(နောက်က) dictionary ကို အနိုင်ပေးပါတယ်။ ဥပမာ ဒုတိယ(နောက်က) dictionary က key 'b' ရဲ့ value 'platypus' ကိုပဲ update လုပ်ပါတယ်။

In [18]:
first = {'a': 1, 'b': 2}
second = {'b': 'platypus'}
first.update(second)
first  
Out[18]:
{'a': 1, 'b': 'platypus'}

del ဖြင့် key-value pair တစ်ခုကို ဖျက်ခြင်း

del key word ကိုသုံး၍ key နာမည်ဖြင့် dictionary ထဲမှ item ကို ဖျက်(Delete)ခြင်း

In [19]:
dict_1 = {'key_1': 'value_1', 'key_2': 'value_2', 'key_3': 'value_3'}
del dict_1['key_1']
dict_1
Out[19]:
{'key_2': 'value_2', 'key_3': 'value_3'}
  • dictionary ထဲမှ value များ ရယူရန် (access လုပ်ရန်) values() method ကို အသုံးပြုနိုင်သည်။

clear() ဖြင့် Dictionary ထဲမှ Key များ အားလုံးကို ဖျက်ခြင်း

Dictionary ထဲမှ key များ အားလုံးနှင့် value များ အားလုံးကို ဖျက်လိုလျှင် clear() ကို သုံးနိုင်သည် သို့မဟုတ် empty dictionary ({}) ဖြင့် dictionary name ကို reassign အလုပ်နိုင်သည်။

In [20]:
dict_1.clear()
dict_1
Out[20]:
{}
In [21]:
dict_1  = {} #empty dictionary ({}) ဖြင့် dictionary name ကို reassign အလုပ်နိုင်သည်။ 

Test for a Key by Using in

Dictionary ထဲတွင် မိမိသိလိုသည့် key တစ်ခု ရှိ မရှိ (exist or not) သိလိုလျှင် in key wordကို သုံး၍ စစ်နိုင်သည်။

In [22]:
dict_1 = {'key_1': 'value_1', 'key_2': 'value_2', 'key_3': 'value_3'}
'key_1' in dict_1 # True
Out[22]:
True
In [23]:
'key_5' in dict_1 #False
Out[23]:
False
In [92]:
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")
Yes, 'model' is one of the keys in the thisdict dictionary

[ key ] ဖြင့် dictionary ထဲမှ Item တစ်ခု ကို ထုတ်ယူခြင်း

ဤနည်းသည် dictionary တွင် အသုံးအများဆုံး ဖြစ်သည်။ key ထည့်ပေး၍ ၎င်းနှင့် သက်ဆိုင်သည့် (corresponding) value ကို ရယူနိုင်သည်။

In [24]:
dict_1 = {'key_1': 'value_1', 'key_2': 'value_2', 'key_3': 'value_3'}
dict_1['key_1']
Out[24]:
'value_1'

Dictionary ထဲတွင် မိမိ ထည့်ပေးသည့် key မရှိပါက exception (KeyError) ပြန်ပေးလိမ့်မည်။

In [25]:
dict_1['key_5'] # KeyError  ပြန်ပေးလိမ့်မည်။     
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-25-80b742ec5040> in <module>
----> 1 dict_1['key_5'] # KeyError  ပြန်ပေးလိမ့်မည်။

KeyError: 'key_5'

Dictionary ထဲတွင် မိမိ ထည့်ပေးသည့် key မရှိပါက exception ပြန်ပေးလိမ့်မည်။ ထိုသို့ exception ပြန်ပေးခြင်း မဖြစ်ပေါ်စေရန် မိမိ ထည့်ပေးမည့် key ရှိ မရှိ စစ်နိုင်သည်။ သို့မဟုတ် get() function ကို အသုံးပြုနိုင်သည်။

In [26]:
dict_1.get('key_1')
Out[26]:
'value_1'

key ကို အသုံးပြု၍ value များကို access လုပ်နိုင်သည်။ Dictionary က သိမ်းဆည်းနိုင်သည့် ဒေတာအမျိုးအစားများသည် ပြောင်းလွယ်ပြင်လွယ်(flexible)ဖြစ်သည်။ ဥပမာ:

In [96]:
my_dict =  {'key1':123,'key2':[12,23,33],'key3':['item0','item1','item2']}
# Call values by their key
my_dict['key2']
Out[96]:
[12, 23, 33]
In [36]:
my_dict = {'key1':123,'key2':[12,23,33],'key3':['item0','item1','item2']}
In [37]:
# Let's call items from the dictionary
my_dict['key3']
Out[37]:
['item0', 'item1', 'item2']
In [38]:
# Can call an index on that value
my_dict['key3'][0]
Out[38]:
'item0'
In [39]:
# Can then even call methods on that value
my_dict['key3'][0].upper()
Out[39]:
'ITEM0'
In [97]:
my_dict['key1']
Out[97]:
123

အောက်ပါကဲ့သို့ ပေါင်းခြင်း အနုတ်ခြင်း မြှောက်ခြင်း စသည်တို့ပြုလုပ်ရန် built-in method ပါရှိသည်။ ဥပမာ:

In [41]:
# Subtract 123 from the value
my_dict['key1'] = my_dict['key1'] - 123
In [42]:
#Check
my_dict['key1']
Out[42]:
0
In [43]:
# Set the object equal to itself minus 123 
my_dict['key1'] -= 123
my_dict['key1']
Out[43]:
-123

keys() ဖြင့် dictionary ထဲမှ Item အားလုံးကို ထုတ်ယူခြင်း

Dictionary ထဲမှ key အားလုံးကို တပြိုင်နက် access လုပ်ရန် key() ကိုသုံးနိုင်သည်။ ဥပမာ

In [27]:
dict_1.keys()
Out[27]:
dict_keys(['key_1', 'key_2', 'key_3'])

list() function ကို သုံး၍ dictionary ထဲမှ key များ ၊ value များနှင့် item များကို list အဖြစ်သို့ ပြောင်းနိုင်သည်။ (the list() function to turn the results of values() and items() into normal Python lists.)

In [28]:
list(dict_1.keys()) #  dict_keys object အားလုံးကို list တစ်ခု  အဖြစ်ပြောင်းသည်။ 
Out[28]:
['key_1', 'key_2', 'key_3']

values()ဖြင့် dictionary ထဲမှ value အားလုံးကို ထုတ်ယူခြင်း

Dictionary ထဲမှ value အားလုံးကို တပြိုင်နက် ရယူရန် values()function ကိုသုံးသည်။

In [29]:
list(dict_1.values()) # Dictionary ထဲမှ value များအားလုံးကို list တစ်ခု အဖြစ်ထုတ်ယူသည်။ 
Out[29]:
['value_1', 'value_2', 'value_3']
In [93]:
car_dict = { "brand": "Ford", "model": "Mustang", "year": 1964 }
for x in car_dict.values():
  print(x)
Ford
Mustang
1964

items() ဖြင့် dictionary ထဲမှ key-value pairs အားလုံးကို ထုတ်ယူခြင်း

Dictionary ထဲမှ key-value pair အားလုံးကို အတွဲလိုက် ရယူရန် items() function ကိုသုံးသည်။

In [30]:
list(dict_1.items()) # Dictionary ထဲမှ key-value pair  များအားလုံးကို list တစ်ခု အဖြစ်ထုတ်ယူသည်။ 
Out[30]:
[('key_1', 'value_1'), ('key_2', 'value_2'), ('key_3', 'value_3')]
In [90]:
car_dict = { "brand": "Ford", "model": "Mustang", "year": 1964 }
for x, y in car_dict.items():
  print(x, ":" ,y)
brand : Ford
model : Mustang
year : 1964

Assign with =, Copy with copy()

= operator ဖြင့် assign လုပ်သည်။ copy()ဖြင့် ကော်ပီကူးသည်။

In [31]:
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)
{'green': 'go', 'yellow': 'go faster', 'red': 'smile for the camera'}
{'green': 'go', 'yellow': 'go faster', 'red': 'smile for the camera', 'blue': 'confuse everyone'}

dictionary တစ်ခုအတွင်းမှ key-value pair များကို တခြား dictionary တစ်ခုအဖြစ် ကော်ပီကူးလိုလျှင် copy()ကို သုံးသည်။

In [32]:
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_1': 'value_1', 'key_2': 'value_2', 'key_3': 'value_3'}
{'key_1': 'value_1', 'key_2': 'value_2', 'key_3': 'value_3'}

assign လုပ်၍ key အသစ်များ ထပ်ထည့်ခြင်း

Key နှင့် ကို တစ်ခုချင်းစီ သီးခြားထည့်သည့်နည်းဖြစ်သည်။

In [44]:
# Create a new dictionary
d = {}
In [45]:
#  assign လုပ်၍ key အသစ်များ ထပ်ထည့်ခြင်း (Create a new key through assignment)
d['animal'] = 'Dog'
In [46]:
# Can do this with any object
d['answer'] = 42
In [47]:
# 
d
Out[47]:
{'animal': 'Dog', 'answer': 42}

Nesting with Dictionaries

Dictionary တစ်ခုထဲတွင် တခြားသော dictionary များ အလွှာလိုက် အထပ်ထပ် ရှိနေနိုင်သည်။ ထိုသို့ dictionary တစ်ခုထဲတွင် တခြားသော dictionary များ ရှိနေခြင်းကြောင့် Nested dictionary ဟု ခေါ်ဆိုခြင်းဖြစ်သည်။

In [48]:
# Dictionary nested inside a dictionary nested inside a dictionary
d = {'key1':{'nestkey':{'subnestkey':'value'}}}
d 
Out[48]:
{'key1': {'nestkey': {'subnestkey': 'value'}}}

Nested dictionary ထဲမှ value များ မည်ကဲ့သို့ ထုတ်ယူနိုင်သည်ကို အောက်တွင် ဖော်ပြထားသည်။ Main dictionary မှ ['key1'] ၊ ဒုတိယ dictionary မှ ['nestkey'] ၊ တတိယအဆင့် dictionary မှ ['subnestkey'] စသည့်ဖြင့် key များကို အဆင့်ဆင့် ထည့်ပေးရသည်။

In [49]:
# Keep calling the keys
d['key1']['nestkey']['subnestkey']
Out[49]:
'value'

Nested Dictionaries အောက်တွင် dictionary တစ်ခုတွင် sub dictionary (၃)ခု ပါဝင်သည့် Nested dictionary တစ်ခုကို ဥပမာအဖြစ် ဖော်ပြထားသည်။

In [50]:
myfamily = {
  "child1" : {
    "name" : "Emil",
    "year" : 2004
  },
  "child2" : {
    "name" : "Tobias",
    "year" : 2007
  },
  "child3" : {
    "name" : "Linus",
    "year" : 2011
  }
}
myfamily
Out[50]:
{'child1': {'name': 'Emil', 'year': 2004},
 'child2': {'name': 'Tobias', 'year': 2007},
 'child3': {'name': 'Linus', 'year': 2011}}

Dictionary Methods

In [51]:
# dictionary တစ်ခု တည်ဆောက်သည်။ 
d = {'key1':1,'key2':2,'key3':3}
In [52]:
# .keys() ဖြင့် dictionary အတွင်းရှိ key များ အားလုံးကို ကြည့်နိုင်သည်။ ဖော်ပြနိုင်သည်။ 
d.keys()
Out[52]:
dict_keys(['key1', 'key2', 'key3'])
In [53]:
#  .values .() ဖြင့် dictionary အတွင်းရှိ value များ အားလုံးကို ကြည့်နိုင်သည်။ ဖော်ပြနိုင်သည်။ 
d.values()
Out[53]:
dict_values([1, 2, 3])

.items() ဖြင့် အတွင်းရှိ item များ အာလုံး ဖော်ပြနိုင်သည်။ ကြည့်နိုင်သည်။ Dictionary တွင် ပါရှိသည့် item များသည် key-value pair များ ဖြစ်ကြသည်။

In [54]:
# Method to return tuples of all items  (we'll learn about tuples soon)
d.items()
Out[54]:
dict_items([('key1', 1), ('key2', 2), ('key3', 3)])

Dictionary တစ်ခုပြုလုပ်ပြီးသည့်နှင့် တပြိုက်နက် dictionary object တစ်ခု ပြုလုပ်ပြီးဖြစ်သည်။ dictionary object တွင် ရှိသည့် attributes နှင့် properties တို့ကို built-in dir() function သုံး၍ ကြည့်နိုင်သည်။

In [55]:
dictionary = {}
print(dir(dictionary))
['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']

built-in isinstance() function ကိုသုံး၍ dictionary သည် dict class ၏ instance တစ်ခု ဖြစ်ကြောင်း စစ်ဆေးနိုင်သည်။

In [56]:
isinstance(dictionary, dict) 
Out[56]:
True

Dictionary တစ်ခုအတွင်းသို့ ဒေတာများထည့်ခြင်း

dict function ကိုသုံး၍ Dictionary တစ်ခုအတွင်းသို့ ဒေတာများ ထည့်ခြင်း dict() function ကိုသုံးသည့်အခါ = operator ဖြင့် keys များ value များကို တွဲ၍ assign လုပ်သည်။

In [57]:
dictionary1 = dict( state="NY",
city="New York")
print(dictionary1)
{'state': 'NY', 'city': 'New York'}

{ } ကို သုံးလျှင် key နှင့် value အကြားတွင် : ခံ၍ ရေးသည်။

In [58]:
dictionary2 = {
"state": "Maryland", "city": "Baltimore"
} 
print(dictionary2)
{'state': 'Maryland', 'city': 'Baltimore'}

တည်ဆောက်ပြီးသား တစ်ခုထဲသို့ key ကိုသုံး၍ value ကို ထည့်ပေး(Assign လုပ်) နိုင်သည်။ (2. Assign values to existing dictionaries using keys)။ ဥပမာ အထက်က ထဲသို့ 'bird': 'Baltimore oriole'ကို ထည့်သည်။ key သည်'bird' ဖြစ်သည်။ value သည် 'Baltimore oriole' ဖြစ်သည်။

In [59]:
dictionary2['bird'] = 'Baltimore oriole'
dictionary2
Out[59]:
{'state': 'Maryland', 'city': 'Baltimore', 'bird': 'Baltimore oriole'}

Dictionaryတစ်ခုမှ data များကို ဖတ်ယူခြင်း (Reading Data from a Dictionary)

Dictionary မှ value များကို key ဖြင့် ဖတ်ယူနိုင်သည်။

In [60]:
print(dictionary1['state']) 
NY

ထိုသို့ ဖတ်ယူသည့်အခါ ထည့်ပေးသည့် key သည် dictionary ထဲတွင် မရှိလျှင် KeyError ပေလိမ့်မည်။

In [61]:
print(dictionary1['age'])
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-61-a7b4904266cb> in <module>
----> 1 print(dictionary1['age'])

KeyError: 'age'

KeyError မဖြစ်ပေါ်စေရန်အတွက် get() function ကို အသုံးပြုသည်။ get() function ဖြင့် ဖတ်သည့်အခါ ထည့်ပေးသည့် key မရှိပါက None ပြန်ပေးသည်။ KeyError ဖြစ်ပေါ်ခြင်းကို ကျော်လွှားနိုင်သည်။

In [62]:
print(dictionary1.get('age'))
None

Iterating Through Dictionaries

Dictionary အတွင်းရှိ key နှင့် value များ တစ်တွဲချင်းစီကို iterate လုပ်ခြင်း for loop ကို သုံး၍ dictionary အတွင်းရှိ key နှင့် value များ တစ်တွဲချင်းစီကို iterate လုပ်ခြင်းသည်် အရိုးရှင်း ဆုံးနည်းဖြစ်သည်။

In [63]:
dictionary1 = dict( state="NY", city="New York")
for item in dictionary1: print(item)# for loop ဖြင့် get dictionary's keys ကို iterate  လုပ်ခြင်း
state
city

Dictionary ထဲရှိ key သို့မဟုတ် value သို့မဟုတ် နှစ်မျိုးစလုံးကို iterate လုပ်နိုင်သည်။ keys() method ကို သုံး၍ key များကို ထုတ်ယူနိုင်သည်။ values() method ကို သုံး၍ value များကို ထုတ်ယူနိုင်သည်။

In [64]:
dictionary1 = dict( state="NY",
city="New York"
)
for item in dictionary1.keys(): print(item)
state
city
In [65]:
for item in dictionary1.values(): print(item) # dictionary1 =  {'state': 'NY', 'city': 'New York'}
NY
New York

key နှင့် value နှစ်မျိုးစလုံးကို တစ်ပြိုင်နက် iterate လုပ်နိုင်သည်။ ထုတ်ယူနိုင်သည်။

In [67]:
for key, value in dictionary1.items(): print(key, ':', value) # dictionary1 =  {'state': 'NY', 'city': 'New York'}
state : NY
city : New York

Checking for the Existence of Particular Keys

for loop ကို သုံး၍ dictionary အတွင်း၌ iterating လုပ်စရာမလိုဘဲ in keyword ကို သုံး၍ မိမိသိလိုသည့် key တစ်ခုသည် dictionary ထဲတွင် ရှိမရှိကို စစ်နိုင်သည်။ ရှိလျှင် True(Boolean value) သို့မဟုတ် မရှိလျှင် False (Boolean value) ကို ပြန်ပေးလိမ့်မည်။

In [68]:
a = {
"size": "10 feet", "weight": "16 pounds"
}
print("size" in a)
print("length" in a)
True
False

Additional Dictionary Attributes

dictionary အတွက် dir() function ကို သုံးလျှင် attribute တချို့ကို တွေ့နိုင်သည်။

In [69]:
print(dir(a))
['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']

အောက်တွင် method တချို့ကို ဥပမာဖြင့် ရှင်းပြထားသည်။

dict.update()

dictionary ထဲသို့ key-value pair အသစ်များထည့်လိုသည့်အခါ .update() method ကိုသုံးသည်။ empty dictionary တစ်ခုထဲသို့ .update() method ဖြင့် key-value pair အသစ် တစ်ခု ထည့်ပုံကို ဥပမာအဖြစ် ပြထားသည်။

In [70]:
a = {}
a.update({"name": "Dan Brown"})
a
Out[70]:
{'name': 'Dan Brown'}

ရှိပြီးသား key ဖြစ်လျှင် ထပ်ခါ ထပ်ခါ key-value pair များကို ထပ်မထည့်ပါ။ ထို့ကြောင့် .update() function သည် dictionary နှစ်ခုကို ပေါင်း(merge)ပြီး မတူညီသည့်(different) key များကို ရလိုသည့်အခါ အသုံးဝင်သည်။ ရှိပြီးသား key-value pair ကို ထပ် မထည့်ပေးသောကြောင့် ဖြစ်သည်။
clear()

dict.clear() and dict.pop() dictionary အတွင်းမှ key များ အားလုံးကို ဖယ်ရှား ရှင်းထုတ်လိုသည့်အခါ clear() method ကို သုံးသည်။

In [71]:
a = {'name': 'Dan Brown Xavier'}
a.clear()
a
Out[71]:
{}

del keyword
key-value pair တစ်ခုတည်းကို ဖယ်ထုတ်လိုလျှင် del keyword ကို သုံးသည်။

In [72]:
a = {"name": "Skandar Keynes", "age": "24"}
del a["name"]
a
Out[72]:
{'age': '24'}

pop()
Dictionary ထဲမှ key-value pair ကို ဖယ်ထုတ်ပြီး ထိုဖယ်ထုတ်လိုက်သည့် key-value pair ကို ကိစ္စ တစ်ခုခု အတွက် အသုံးလိုလျှင် pop() ကိုသုံးသည်။

In [73]:
a = {"name": "Skandar Keynes", "age": "24"}
b = a.pop("name")
print(a)
print(b)
{'age': '24'}
Skandar Keynes
In [74]:
dict.copy()
shallow copy အဖြစ် ကော်ပီ ကူးလိုသည့်အခါ copy method ကို သုံးသည် ကော်ပီကူးပြီးသည့်အခါ
မူလ ဒေတာနှင့် ဘာမှ မသက်ဆိုင်တော့သည့် object အသစ်များ ဖြစ်သွားသည် 
  File "<ipython-input-74-441e9bbcaecb>", line 2
    shallow copy အဖြစ် ကော်ပီ ကူးလိုသည့်အခါ copy method ကို သုံးသည်။ ကော်ပီကူးပြီးသည့်အခါ
               ^
SyntaxError: invalid syntax
In [75]:
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  ဖြစ်သည်။
{'name': 'Skandar Keynes', 'age': '24'}
{'name': 'Skandar Keynes', 'age': '24'}
{'name': 'Janet Jackson', 'age': '24'}
{'name': 'Skandar Keynes', 'age': '24'}

အထက်က ဥပမာတွင် b သည် a ၏ shallow copy ဖြစ်သည်။ dictionary a မှ key-value pair အားလုံးကို dictionary b အဖြစ် ကော်ပီကူးပေးပြီး အသစ်တစ်ခု ပြလုပ်ပေးသည်။ a ကို update လုပ်သည့်အခါ b သည် ဘာမှ မပြောင်းလဲပါ။ သီးခြားလွတ်လပ်သည့် dictionary နှစ်ခုအဖြစ် ရှိနေလိမ့်မည်။

= operator ကို အသုံးပြုပါက deep copy အဖြစ် ကော်ပီကူးပေးသည်။ a နှင့် b တို့သည် တူညီသည့် ဒေတာများကို ရည်ညွန်းသည်။ ထို့ကြောင့် တစ်ခုကို update လုပ်လျှင် တခြားတစ်ခုသည် လိုက်၍ ပြောင်းလဲလိမ့်မည်။

In [76]:
a = {"name": "Skandar Keynes", "age": "24"}
b = a
a["name"] = "Janet Jackson"
b["age"] = 16
print(a)
print(b)
{'name': 'Janet Jackson', 'age': 16}
{'name': 'Janet Jackson', 'age': 16}

dict.popitem()

popitem() method သည် dictionary ထဲမှ random item ထုတ်ယူပေးသည်။ ထိုသို့ထုတ်ယူပေးပြီးနောက် dictionary ထဲမှ ဖျက်လိုက်သည်။

In [78]:
a = {"name": "Skandar Keynes", "age": "24", "sex": "male"}
a.popitem()
Out[78]:
('sex', 'male')
In [79]:
 a
Out[79]:
{'name': 'Skandar Keynes', 'age': '24'}
In [80]:
a = {"name": "Skandar Keynes", "age": "24", "sex": "male"}
b = a.setdefault("name")
a
Out[80]:
{'name': 'Skandar Keynes', 'age': '24', 'sex': 'male'}
In [81]:
b
Out[81]:
'Skandar Keynes'

Ordered Dictionaries

သမာန် 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

In [ ]: