Skip to main content

Mapping list

You're already familiar with using list comprehensions to map one list into another. There is another way to
accomplish the same thing, using the built−in map function. It works much the same way as the filter function.
Example 16.10. Introducing map
>>>
...
...
>>>
>>>
[2,
>>>
[2,
>>>
>>>
...
...
>>>
[2,
def double(n):
return n*2
li = [1, 2, 3, 5, 9, 10, 256, −3]
map(double, li)
4, 6, 10, 18, 20, 512, −6]
[double(n) for n in li]
4, 6, 10, 18, 20, 512, −6]
newlist = []
for n in li:
newlist.append(double(n))
newlist
4, 6, 10, 18, 20, 512, −6]
[8]
map takes a function and a list and returns a new list by calling the function with each element of the
list in order. In this case, the function simply multiplies each element by 2.
You could accomplish the same thing with a list comprehension. List comprehensions were first
introduced in Python 2.0; map has been around forever.
You could, if you insist on thinking like a Visual Basic programmer, use a for loop to accomplish the
same thing.

map with lists of mixed datatypes
>>> li = [5, 'a', (2, 'b')]
>>> map(double, li)
[10, 'aa', (2, 'b', 2, 'b')]
As a side note, I'd like to point out that map works just as well with lists of mixed datatypes, as long as the
function you're using correctly handles each type. In this case, the double function simply multiplies the
given argument by 2, and Python Does The Right Thing depending on the datatype of the argument. For
integers, this means actually multiplying it by 2; for strings, it means concatenating the string with itself; for
tuples, it means making a new tuple that has all of the elements of the original, then all of the elements of the
original again.

filenameToModuleName = lambda f: os.path.splitext(f)[0]
moduleNames = map(filenameToModuleName, files)

As you saw in Section 4.7, Using lambda Functions, lambda defines an inline function. And as you saw in
Example 6.17, Splitting Pathnames, os.path.splitext takes a filename and returns a tuple (name,
extension). So filenameToModuleName is a function which will take a filename and strip off the file
extension, and return just the name.
Calling map takes each filename listed in files, passes it to the function filenameToModuleName, and
returns a list of the return values of each of those function calls. In other words, you strip the file extension off
of each filename, and store the list of all those stripped filenames in moduleNames.

Comments

Popular posts from this blog

CKA Simulator Kubernetes 1.22

  https://killer.sh Pre Setup Once you've gained access to your terminal it might be wise to spend ~1 minute to setup your environment. You could set these: alias k = kubectl                         # will already be pre-configured export do = "--dry-run=client -o yaml"     # k get pod x $do export now = "--force --grace-period 0"   # k delete pod x $now Vim To make vim use 2 spaces for a tab edit ~/.vimrc to contain: set tabstop=2 set expandtab set shiftwidth=2 More setup suggestions are in the tips section .     Question 1 | Contexts Task weight: 1%   You have access to multiple clusters from your main terminal through kubectl contexts. Write all those context names into /opt/course/1/contexts . Next write a command to display the current context into /opt/course/1/context_default_kubectl.sh , the command should use kubectl . Finally write a second command doing the same thing into ...

OWASP Top 10 Threats and Mitigations Exam - Single Select

Last updated 4 Aug 11 Course Title: OWASP Top 10 Threats and Mitigation Exam Questions - Single Select 1) Which of the following consequences is most likely to occur due to an injection attack? Spoofing Cross-site request forgery Denial of service   Correct Insecure direct object references 2) Your application is created using a language that does not support a clear distinction between code and data. Which vulnerability is most likely to occur in your application? Injection   Correct Insecure direct object references Failure to restrict URL access Insufficient transport layer protection 3) Which of the following scenarios is most likely to cause an injection attack? Unvalidated input is embedded in an instruction stream.   Correct Unvalidated input can be distinguished from valid instructions. A Web application does not validate a client’s access to a resource. A Web action performs an operation on behalf of the user without checkin...