خريطة بايثون () وظيفة
مثال
احسب طول كل كلمة في المجموعة:
def myfunc(n):
return len(n)
x = map(myfunc, ('apple',
'banana', 'cherry'))
التعريف والاستخدام
تنفذ map()
الوظيفة وظيفة محددة لكل عنصر في قابل للتكرار. يتم إرسال العنصر إلى الوظيفة كمعامل.
بناء الجملة
map(function, iterables)
قيمه المعامل
Parameter | Description |
---|---|
function | Required. The function to execute for each item |
iterable | Required. A sequence, collection or an iterator object. You can send as many iterables as you like, just make sure the function has one parameter for each iterable. |
مزيد من الأمثلة
مثال
اصنع ثمارًا جديدة عن طريق إرسال كائنين متكررين في الوظيفة:
def myfunc(a, b):
return a + b
x = map(myfunc, ('apple', 'banana',
'cherry'), ('orange', 'lemon', 'pineapple'))