Question

I have a line of code which is:

D = {'h' : 'hh' , 'e' : 'ee'}
str = 'hello'
data = ''.join(map(lambda x:D.get(x,x),str))
print data

this gives an output -> hheello

I am trying to understand how does map function work here. Does map take each character of the string and compare it with dictionary key, and give back the corresponding key value?

How does it do for each character here? There is no iteration. Is there any good example to understand this better?

Was it helpful?

Solution 3

It takes individual elements of str. Following is the readable code for the same implementation:

D = { 'h' : 'hh' , 'e' : 'ee'}
str = 'hello'
returns = []  # create list for storing return value from function
def myLambda(x): # function does lambda 
    return D.get(x,x)

for x in str: #map==> pass iterable 
    returns.append(myLambda(x)) #for each element get equivalent string from dictionary and append to list

print ''.join(returns) #join for showing result

OTHER TIPS

There is no loop because map requires an "iterable" (i.e. an object on which you can do an iteration) and does the loop itself.

map, if not present natively, could be implemented as:

def map(f, it):
    return [f(x) for x in it]

or, even more explicitly, as:

def map(f, it):
    result = []
    for x in it:
        result.append(f(x))
    return result

In Python a string is an iterable, and on iteration loops over the characters in the string. For example

map(ord, "hello")

returns

[104, 101, 108, 108, 111]

because those are the character codes for the chars in the string.

Map just applies the function to each item of a list. E.g.,

map(lambda x: 10*x, [1,2,3,4])

gives

[10, 20, 30, 40]

Generally speaking, a map operation works like this:

MAP (f,L) returns L'

Input:

  • L is a list of n elements [ e1 , e2 , ... , en ]
  • f is a function

Output

  • L' is the list L after the application of f to each element individually: [ f(e1) , f(e2) , ... , f(en) ]

So, in your case, the join operation, which operates on lists, starts with the empty string and repeatedly concatenates each element e obtained in the following way:

Take a character x from str; return D.get(x,x)

Note that the above (which is the explaining of the map operation) will give you 'hh' and 'ee' with input 'h' and input 'e' respectively, while it will leave the other characters as they are.

Since str is a string, map() will apply the function (lambda in this case) to every item of the string. [map()][2] works on iterables and sequences, so it can work in a string because a string is a sequence.

Try this so you get the idea:

str2 = "123"
print map(int, str2)
>>> [1, 2, 3]

In this case you are casting each letter in str2 to int:

int("1") -> 1
int("2") -> 2
int("3") -> 3

and return them in a list:

[1, 2, 3]

Note: Don't use Python built-in names as names of variables. Don't use str as the name of a variable because you are hiding its built-in implementation. Use str1, my_str o, s ... instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top