แผนที่ หลาม()ฟังก์ชั่น
ตัวอย่าง
คำนวณความยาวของแต่ละคำในทูเพิล:
def myfunc(n):
return len(n)
x = map(myfunc, ('apple',
'banana', 'cherry'))
ความหมายและการใช้งาน
ฟังก์ชันmap()
เรียกใช้ฟังก์ชันที่ระบุสำหรับแต่ละรายการใน iterable รายการถูกส่งไปยังฟังก์ชันเป็นพารามิเตอร์
ไวยากรณ์
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'))