ပေါ်ထားပြီးပြီးဆီးဆီးရဲ့ပေါ်

字典(Dictionary)

字典是一个无序、可变和有索引的集合。在 Python 中,字典用花括号编写,拥有键和值。

အကျိုးဆက်

创建并打印字典:

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
print(thisdict)

运行实例

访问项目

您可以通过在方括号内引用其键名来访问字典的项目:

အကျိုးဆက်

获取 "model" 键的值:

x = thisdict["model"]

运行实例

还有一个名为 get() 的方法会给你相同的结果:

အကျိုးဆက်

获取 "model" 键的值:

x = thisdict.get("model")

运行实例

更改值

您可以通过引用其键名来更改特定项的值:

အကျိုးဆက်

把 "year" 改为 2019:

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
thisdict["year"] = 2019

运行实例

遍历字典

您可以使用 for 循环遍历字典。

循环遍历字典时,返回值是字典的键,但也有返回值的方法。

အကျိုးဆက်

逐个打印字典中的所有键名:

for x in thisdict:
  print(x)

运行实例

အကျိုးဆက်

逐个打印字典中的所有值:

for x in thisdict:
  print(thisdict[x])

运行实例

အကျိုးဆက်

您还可以使用 values() 函数返回字典的值:

for x in thisdict.values():
  print(x)

运行实例

အကျိုးဆက်

通过使用 items() 函数遍历键和值:

for x, y in thisdict.items():
  print(x, y)

运行实例

检查键是否存在

要确定字典中是否存在指定的键,请使用 in 关键字:

အကျိုးဆက်

检查字典中是否存在 "model":

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
if "model" in thisdict:
  print("Yes, 'model' is one of the keys in the thisdict dictionary")

运行实例

字典长度

要确定字典有多少项目(键值对),请使用 len() 方法。

အကျိုးဆက်

打印字典中的项目数:

print(len(thisdict))

运行实例

添加项目

通过使用新的索引键并为其赋值,可以将项目添加到字典中:

အကျိုးဆက်

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
thisdict["color"] = "red"
print(thisdict)

运行实例

删除项目

有几种方法可以从字典中删除项目:

အကျိုးဆက်

pop() 方法删除具有指定键名的项:

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
thisdict.pop("model")
print(thisdict)

运行实例

အကျိုးဆက်

popitem() 方法删除最后插入的项目(在 3.7 之前的版本中,删除随机项目):

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
thisdict.popitem()
print(thisdict)

运行实例

အကျိုးဆက်

del thisdict["model"] thisdict.popitem()

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
ကြောင်းချက်အရာ ကို ပြုပြင်ပါ
print(thisdict)

运行实例

အကျိုးဆက်

del thisdict["model"] del

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
ကြောင်းချက်အရာ ကို ပြုပြင်ပါ
del thisdict

运行实例

အကျိုးဆက်

clear() print(thisdict) #this က အမှား ဖြစ်ပါ သတ္တိကြီးသည် "thisdict" မပါဘဲ

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
ကြောင်းချက်အရာ ကို ပြုပြင်ပါ
print(thisdict)

运行实例

thisdict.clear()

ကုဒ္ဒရာ ကို ပြုပြင်ပါ ကုဒ္ဒရာ ကို ပြုပြင်ပါ dict2 = dict1dict2 သာ dict1 အခြား စကားရပ် ကုဒ္ဒရာ အား ပြုပြင်ပါ dict1 အခြား စကားရပ် ကုဒ္ဒရာ အား ပြုပြင်ပါ dict2 အခြား စကားရပ် ကုဒ္ဒရာ အား ပြုပြင်ပါ

အခြား စကားရပ် ကုဒ္ဒရာ ကို ပြုပြင်ပါ copy()

အကျိုးဆက်

သုံးစွဲပါ copy() စကားရပ် ကုဒ္ဒရာ ကို ပြုပြင်ပါ

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
mydict = thisdict.copy()
print(mydict)

运行实例

ပုံစံ ကုဒ္ဒရာ ပုံစံ ကို ပြုပြင်ပါ dict()

အကျိုးဆက်

သုံးစွဲပါ dict() စကားရပ် ကုဒ္ဒရာ ကို ကုဒ္ဒရာ ပုံစံ ကို ပြုပြင်ပါ

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
mydict = dict(thisdict)
print(mydict)

运行实例

ပါဝင်သည် ကုဒ္ဒရာ

ကုဒ္ဒရာ အခြား ကုဒ္ဒရာ အပါအဝင် ကုဒ္ဒရာ ကို ပါဝင်သည် ကုဒ္ဒရာ ဖြစ်၍ အမည်များစွာ ကုဒ္ဒရာ ဖြစ်သည်

အကျိုးဆက်

သုံးဆယ် ကုဒ္ဒရာ အပါအဝင် ကုဒ္ဒရာ ကို ဖန်တီးပါ

myfamily = {
  "child1" : {
    "name" : "Phoebe Adele",
    "year" : 2002
  },
  "child2" : {
    "name" : "Jennifer Katharine",
    "year" : 1996
  },
  "child3" : {
    "name" : "Rory John",
    "year" : 1999
  }
}

运行实例

သို့မဟုတ် သုံးဆယ် ကုဒ္ဒရာ အခြား ကုဒ္ဒရာ အား ကုဒ္ဒရာ အခြား ကုဒ္ဒရာ အား ဖန်တီးပြီး သုံးဆယ် ကုဒ္ဒရာ အပါအဝင် ကုဒ္ဒရာ ကို ဖန်တီးပါ

အကျိုးဆက်

သုံးဆယ် ကုဒ္ဒရာ ကို ဖန်တီးပြီး အခြား သုံးဆယ် ကုဒ္ဒရာ အပါအဝင် ကုဒ္ဒရာ ကို ဖန်တီးပါ

child1 = {
  "name" : "Phoebe Adele",
  "year" : 2002
}
child2 = {
  "name" : "Jennifer Katharine",
  "year" : 1996
}
child3 = {
  "name" : "Rory John",
  "year" : 1999
}
myfamily = {
  "child1" : child1,
  "child2" : child2,
  "child3" : child3
}

运行实例

dict() ကုဒ္ဒရာပညာ

သို့မဟုတ် dict() ကုဒ္ဒရာပညာအသုံးပြုပါ

အကျိုးဆက်

thisdict = dict(brand="Porsche", model="911", year=1963)
# ကျွန်ုပ်သည် ကြောင်းချက်အရာ အား စကားရပ်အား မမှုပ်နှံကြောင်း သုံးစွဲပါတယ်
# ကျွန်ုပ်သည် ချက်အရာကို ကြောင်းချက်အရာ အား မမှုပ်နှံကြောင်း သုံးစွဲပါတယ်
print(thisdict)

运行实例

字典方法

Python 提供一组可以在字典上使用的内建方法。

方法 描述
clear() 删除字典中的所有元素
copy() 返回字典的副本
fromkeys() 返回拥有指定键和值的字典
get() 返回指定键的值
items() 返回包含每个键值对的元组的列表
keys() စက်တင် ကို ပြောင်းလဲသော စာကြောင်း စာအုပ်
pop() သတ်မှတ်ထားသော ကိုယ်ဝင်စက်တင် ကို ဖျက်သိမ်း
popitem() နောက်ဆုံး စက်တင်ကို ပြန်လည်ပေး
setdefault() သတ်မှတ်ထားသော ကိုယ်ဝင်စက်တင် ကို နှင့် သတ်မှတ်ထားသော အကျဉ်းချဲ ကို ပြန်လည်ပေး
update() သတ်မှတ်ထားသော စက်တင်အချက်အလက် ကို အသုံးပြု၍ စက်တင် ချုပ်ကို ထပ်ထည့်
values() အခြေခံချက် မှ အကျဉ်းချဲ စာကြောင်း စာအုပ်