Movatterモバイル変換


[0]ホーム

URL:


Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only.Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

How to utilize the map() Function

The built-in functionmap() takes a function as a first argument and applies it to each of the elements of its second argument, aniterable. Examples of iterables are strings, lists, and tuples.

For more information on iterables and iterators, check outIterables and Iterators.map() returns an iterator corresponding to the transformed collection.

00:00Next up,map().Just likefilter(),map() takes two arguments: afunction and aniterable.

00:08But it works differently.It returns a new iterator by applying the function to every item of theiterableit’s been passed, yielding those results.

00:19It allows quick application of a function to every element of an iterable.Here’s an example where we’re passing it the numbers1,2,3, and4and the lambda expression squares the input number.

00:34This gives us a new list with the squares of the original numbers—[1, 4, 9, 16].Now let’s seemap() in action in a little more detail.

00:49As you’ve just heard,map() applies a function to an iterable to create a new iterable. In this case,we’ll be supplying it with a list of numbers and using a lambda function tosquare those numbers to create the new list. Firstly,we’ll need a list of numbers,and we’ll print that list out.

01:13Next, create the new list of numberswith a lambda expression which will square any value that’s passed to it.And finally, print out the new list.Let’s see that running. And here,the first list is the original list of numbers from1 to10,and the second list is the square of those values.

01:47The lambda expression being used in amap() function doesn’t have to generate thesame kind of data as has been passed into it. So herewe’re going to see one where we get either aTrue orFalse result.

02:00Again, we’ll start with a list of 10 numbers.

02:08Next, we’ll usemap() to create a new list and have a lambda expression whichgeneratesTrue orFalse depending on the condition.

02:22Here you can see this expression will generate eitherTrue orFalse dependingon whetherx % 2 is equal to0 or not.

02:32Now we’ll print the new list out.Let’s see that running.And as expected, we have our list of numbers from1 to10,but we also have our output from ourmap(),which has generated eitherFalse orTruedepending on whether the numbers are odd or even.

03:03Now you’re going to see an example where the lambda expression returns a tuple,modifying the contents of the tuple it’s been passed.

03:29Here’s a list of tuples containing team names,and the number of times they’ve taken part in the World Cup finals.

03:41We’re going to print that out,and now we’re going to usemap() to apply a lambda expression across the entirelist of tuples, incrementing the number of times each team has taken part.

03:52This is of course assuming that all these teams have made it into the next finals.Next, we create a new list usingmap()on a lambda expression which takes ateam tuple,and outputs a new tuple wherethe first element is the unchanged team name,and the second element is the incremented number of times the team has taken part.

04:26Finally, we pass in thewc_teams list of tuples to map.

04:37The last step is to print out thisnew list of tuples.Let’s see that code running.

04:54Here you can see the original list of tuples with the team name and the number oftimes each team’s taken part. And secondly,you can see thenew list of tuples with the unchanged team names and theincremented number of times the team has taken part.

Become a Member to join the conversation.

Course Contents

Overview
63%

[8]ページ先頭

©2009-2026 Movatter.jp