zip function

zip function သည် အစဉ်လိုက်အရာများ (စာရင်းများ၊ tuples များ၊ sets များ) မှ element များကို ပေါင်းစည်းပေးသည့် iterator တစ်ခုကို ဖန်တီးပေးပါသည်။ ဤ function သည် ပထမ နှင့် ဒုတိယ အစဉ်လိုက်အရာများမှ element အားလုံးကို အစဉ်လိုက် အတူတကွပေါင်းစပ်၍ ပြန်ပေးသော iterator တစ်ခုဖြစ်သည်။ function ၏ parameter တွင် ကျွန်ုပ်တို့ စိတ်ဝင်စားသော tuples များ၊ စာရင်းများ၊ sets များ စသည်တို့ကို ဖော်ပြရပါမည်။

Syntax

pythonzip(ပထမအစဉ်လိုက်အရာ၊ ဒုတိယအစဉ်လိုက်အရာ စသည်)

နမူနာ

zip function ကို အသုံးပြု၍ မူလ tuple နှစ်ခုကို အခြေခံပြီး tuple အသစ်တစ်ခု ဖန်တီးကြည့်ပါစို့။

pythontlp1 = ('a', 'b', 'c')
tlp2 = (1, 2, 3)

res = zip(tlp1, tlp2)
print(tuple(res))

ကုဒ်ကို run လိုက်သော အဖြေ။

python(('a', 1), ('b', 2), ('c', 3))

နမူနာ

ယခု zip function ကို အသုံးပြု၍ မူလ စာရင်းနှစ်ခုကို အခြေခံပြီး tuple အသစ်တစ်ခု ဖန်တီးကြည့်ပါစို့။

pythonlst1 = ['d', 'e', 'f']
lst2 = [4, 5, 6]

res = zip(lst1, lst2)
print(tuple(res))

ကုဒ်ကို run လိုက်သော အဖြေ။

python(('d', 4), ('e', 5), ('f', 6))

နမူနာ

set နှစ်ခုမှ tuple တစ်ခုကို ဖန်တီးကြည့်ပါစို့။

pythonst1 = {'a', 'b', 'c'}
st2 = {'d', 'e', 'f'}

res = zip(st1, st2)
print(tuple(res))

ကုဒ်ကို run လိုက်သော အဖြေ။

python(('a', 'd'), ('c', 'e'), ('b', 'f'))

နမူနာ

ယခု set သုံးခုမှ tuple တစ်ခုကို ဖန်တီးကြည့်ပါစို့။

pythonst1 = {'a', 'b', 'c'}
st2 = {'d', 'e', 'f'}
st3 = {1, 2, 3}

res = zip(st1, st2, st3)
print(tuple(res))

ကုဒ်ကို run လိုက်သော အဖြေ။

python(('c', 'f', 1), ('b', 'd', 2), ('a', 'e', 3))

နမူနာ

zip function ကို အသုံးပြု၍ object အများအပြားကို တစ်ချိန်တည်းတွင် ဖြတ်သန်း လုပ်ဆောင်နိုင်ပါသည်။ နမူနာအနေဖြင့် စာရင်းသုံးခုကို ဖြတ်သန်းကြည့်ပါစို့။

pythonlst1 = [1, 2, 3]
lst2 = [4, 5, 6]
lst3 = [7, 8, 9]

for el1, el2, el3 in zip(lst1, lst2, lst3):
  print(el1, el2, el3)

ကုဒ်ကို run လိုက်သော အဖြေ။

python1 4 7
2 5 8
3 6 9

ဆက်လက်ဖတ်ရှုရန်

  • filter function,
    သည် iterable object များကို စစ်ထုတ်ပေးပါသည်။
  • map function,
    သည် iterable object များကို ဖြတ်သန်းပေးပါသည်။