Python3 - Basic Knowledge of using Dictionary

<Zawgyi>
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() fuction သံုးပီးရယ္ေပါ့


  • 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 မွာၾကည့္လို႔ရပါတယ္ https://www.programiz.com/python-programming/methods/dictionary

How to add new key:value in 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 objecta (mutable object can be changed after it is created)


Hope this article helps! See you again in next post! 
Feel Free for any feedback! 





Comments

Popular posts from this blog

Using Zabbix Inventory Script for AWX Ansible Tower Inventory

cisco csr1000v ios upgrade or downgrade

Aker SSH Gateway