Python Dictionary setdefault() နည်းလမ်း
ဥပမာ
"မော်ဒယ်" ပစ္စည်း၏တန်ဖိုးကို ရယူပါ-
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.setdefault("model", "Bronco")
print(x)
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
နည်းလမ်း သည် setdefault()
သတ်မှတ်ထားသောသော့ဖြင့် ပစ္စည်းတန်ဖိုးကို ပြန်ပေးသည်။
သော့မရှိပါက သတ်မှတ်ထားသောတန်ဖိုးဖြင့် သော့ကိုထည့်ပါ၊ အောက်ပါဥပမာကိုကြည့်ပါ။
အထားအသို
dictionary.setdefault(keyname, value)
ကန့်သတ်တန်ဖိုးများ
Parameter | Description |
---|---|
keyname | Required. The keyname of the item you want to return the value from |
value | Optional. If the key exist, this parameter has no effect. If the key does not exist, this value becomes the key's value Default value None |
နောက်ထပ် ဥပမာများ
ဥပမာ
"အရောင်" ပစ္စည်း၏တန်ဖိုးကိုရယူပါ၊ "အရောင်" ပစ္စည်းများမရှိပါက၊ တန်ဖိုး "အဖြူ" ဖြင့် "အရောင်" ထည့်ပါ။
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.setdefault("color",
"white")
print(x)