Python တွင် dictionary အတွင်း element ရှိမရှိ စစ်ဆေးခြင်း
Dictionary အတွင်း key တစ်ခုရှိမရှိကို
in operator ဖြင့်စစ်ဆေးနိုင်ပြီး
မရှိကြောင်းကိုမူ not in ဖြင့်
စစ်ဆေးနိုင်ပါသည်။
လက်တွေ့စမ်းကြည့်ပါမည်။ ကျွန်ုပ်တို့တွင် အောက်ပါ dictionary ရှိသည်ဆိုပါစို့။
dct = {
'a': 1,
'b': 2,
'c': 3
}
Dictionary အတွင်း key 'a' ပါသော
element ရှိမရှိ စစ်ဆေးကြည့်ပါမည်။
print('a' in dct) # True ကိုထုတ်ပြမည်
Dictionary အတွင်း key 'x' ပါသော
element မရှိကြောင်း စစ်ဆေးကြည့်ပါမည်။
print('x' not in dct) # True ကိုထုတ်ပြမည်
Dictionary ပေးထားသည်။
dct = {
'x': 1,
'y': 2,
'z': 3
}
၄င်းအတွင်း key 'w' ပါသော element
ရှိမရှိ စစ်ဆေးပါ။
အောက်ပါ code ပေးထားသည်။
dct = {
1: 'x',
2: 'y',
3: 'z',
4: 'w'
}
print('x' in dct)
Console တွင် မည်သည်ကို ထုတ်ပြမည်နည်း။
အောက်ပါ code ပေးထားသည်။
dct = {
1: 'x',
2: 'y',
3: 'z',
4: 'w'
}
print('x' not in dct)
Console တွင် မည်သည်ကို ထုတ်ပြမည်နည်း။
အောက်ပါ code ပေးထားသည်။
dct = {
1: 'x',
2: 'y',
3: 'z',
4: 'w'
}
print(3 in dct)
Console တွင် မည်သည်ကို ထုတ်ပြမည်နည်း။