top of page

Python3 - Basic Knowledge of using Dictionary

<Unicode>

Hello, all again ပါ😃

စာမရေးတာကြာပီဆိုတော့ အရင်ဆုံး Draftထဲက အကျန်လေးရှင်းပေါ့

ဒီတခေါက် ပြောမှာကတော့ အခုချိနိမှာ dataတွေများလာပီ အဲ့လိုပဲ တဖက်မှာ ကိုယ့်ရဲ့ systemမှာ appတခု script ဖြစ်ဖြစ်မှာ Data တွေကို ဘယ်လို structureကျကျ သိမ်းမလဲဆိုတာ စဉ်းစားစရာဖြစ်လာပီ

ကိုယ်က လက်ရှိ Python နဲ့ကျင်လည်နေတာ့ ဒီမှာ dataတွေကို ဘယ်လို structureကျကျသိမ်းနိုင်လဲ ပြောပြ သွားပါမယ်.....

Python မှာ mainly အနေနဲ. data structure 4မျိူး ရှိပီး - list, dictionary, tuples and sets!

ဒီtopic မှာတော့ dictionary ကိုဘယ်လို သုံးပီး Dictionary, List ဘယ်လို ကွာခြားလဲရယ် ရှင်းပြသွားပါ့မယ်


How to use and declare and access Dictionary

          ဒီ topic မှာ dictionary ကြီးကို စာနဲ့ ရှင်းပြတာထက် ပိုမြင်အောင် exampleလေးနဲ့ပေ့ါ

ကိုယ်တွေ dictionary ကြည့်ရင် alphabetနဲ့ရှာကြတယ်လေ. တနည်းအားဖြင့် ဒါက keyပေါ့ keyဆိုတဲ့ alphabet နဲ့ရှာမှ သူ့ရဲ့ value- wordရဲ့ meaning ကိုသိတာလေ

အဲ့လိုပါပဲ python မှာလည်း data တွေသိမ်းတဲ့ အခါမှာ သူ့အတွဲနဲ့သူ ပြန်ပီး Analysis လုပ်တဲ့အခါ တူရာ keywordနဲ့ ပြန်ဆွဲထုတ်နိုင်တော့ ပိုပီး efficient ဖြစ်တာပေါ့

ဒီ key-value နဲ့ တွဲပီး သိမ်းတာက python Dictionary ကို သုံးရတဲ့ အ ကြောင်းပြချက်ပေါ့.

ဒါဆို Dictionary ရဲ့ google definitionကို နားလည်မယ်ထင်ပါတယ်

A dictionary is similar to a list, but you access values by looking up a key instead of a numeric index. A key can be any string or number.


How to Declare Dictionary

   အိုခေ အရင်ဆုံး Empty dictionaryတခု (_var) ကြေညာကြည့်ကြမယ် ...

ဒီမှာ Dictionary တခုကို ၂နည်းကြေညာပေးနိုင်တယ်

  • {} curly brace literals

  • dict() function သုံးပီးရယ်ပေါ့

📷

the

Emptyကို createကြည့်ပီးပီဆိုတော့ key:value ဘယ်လို ထည့်ရလဲ ပြောသွားပါ့မယ်

📷

📷


How to access key, value in dictionary

       Dictionary တွေ createကြည့်ပီးပီဆိုတော့ methodတွေ ကို ဘာတွေ သုံးနိုင်လဲ ပြောသွားပေးပါမယ်

ကိုယ် createလုပ်ထားတဲ့ _var ဆိုတဲ့ dict က values တွေ keysတွေ သက်သက်ချင်းပြန်သုံးမယ်ဆို values () and keys()

📷

ဒီ keyမှာ ဘာvalueတွေ သိမ်းထားလဲ ပြန်ဆွဲထုတ်ချင်ရင်

  • .get() or

  • dictname[]

📷

dict.items() ကျတော့ loop ပတ်ပီး dictionaryထဲက key, value တွေ ထုတ်ချင်ရင်သုံးလိူ့ရပါတယ်

📷

Pop() ကျတော့ remove itemsလုပ်ချင်ရင် argumentအနေနဲ့ ကိုယ်remove လုပ်ချင်တဲ့ keyကိ ု passလုပ်ပီး သုံးပေးရပါတယ်

📷

နောက် method တခု အနေနဲ့ len()

📷

ကျန်တဲ့ method ေ တွကို ဆက်ပီး ဒီlink မှာကြည့်လို့ရပါတယ် -


How to add new key: value in the existed dictionary

      ဒါဆို နဲနဲလေး ဆက်သွားမယ်နော်....... စောစောက pop()နဲ့ removeလိုက်သလို...

ကိုယ်က ရှိပီးသား dictionary ကို ထပ်ပြီး ထည့်ချင်တယ်ဆိုရင်ကော လ ွယ်ပါတယ်

ရှိပီးသား dict_name.[key] = value ဆို ရပါပီ

📷

အိုခေ! ဒီkeyတခုမှာ data ေ တွ အများကြီး သိမ်းချင်တယ်...

for example : nameတွေကို name ဆိုတဲ့ keyအောက်မှာ သိမ်းမယ် ဒီမှာ ကိုယ်အားသန်ရာ data structureနဲ့သိမ်းနိုင်တယ်

Dictionary ထဲမှာ dictionary နဲ့ ပြန်သိမ်းနိုင်သလိုDictionary ထဲမှာ list နဲ့ ပြန်သိမ်းလို့လည်း ရပါတယ်

📷

ဒီမှာတော့ no 2 လေးနဲ့ သိမ်းပြပါမယ်......List နဲ့ Dict ကွာခြားချက်လေးကို မြင်စေချင်လို့ပါ

📷

ဒီoutputကို ကြည့်ချင်အားဖြင့် Dictionary က key:value အတွဲလိုက်သွားပီး! List ကျ index နဲ့ ပြန်ခေါ်ပီး သုံးရပါတယ် (JAVA မှာဆို ARRAYလိုပေါ့)

နောက်ဆူံးအ နေနဲ့....

we can modify python List through list.append() list.extend()!

So, List and Dictionary are mutable objects (a mutable object can be changed after it is created)

Hope this article helps! See you again in the next post!

Feel Free for any feedback!

 
 
 

Recent Posts

See All
cisco csr1000v ios upgrade

Today Let me share about upgrading or downgrading ios using ansible. Original idea credit to eanylin (https://github.com/eanylin). I...

 
 
 

Comments


Subscribe Form

  • facebook
  • twitter
  • linkedin

©2019 by proximus. Proudly created with Wix.com

bottom of page