1

Problem : I want to improve my understanding of the python map function. I made a function which can return the lengths of words in a given phrase as a list. However, I would like to simply use the map function with a lambda function and pass in a string. Also, I am using python 3.

Current Function (WORKS) :

phrase = 'How long are the words in this phrase'

def word_lengths(phrase):
    phrase = phrase.split(' ')
    wordLengthList = []
    for i in range(len(phrase)):
        wordLengthList.append(len(phrase[i]))
    return wordLengthList

word_lengths(phrase)

Current implementation of map (DOES NOT WORK):

 list(map(lambda x: len(x.split(' ')), phrase))

If anyone could help me resolve this issue I would really appreciate it.

4
  • 1
    Please update your question to fix the indenting. Python is very sensitive to indenting, as are python programmers. Commented Feb 5, 2018 at 13:30
  • Generally speaking, map is a little outdated and you might be better off using a list comprehension or a generator expression. Plus personally I find them more readable. Commented Feb 5, 2018 at 13:31
  • Are you looking for just map(len, phrase.split(' '))? Hard to tell, because your Current Function (WORKS) - actually doesn't (lst is undefined). Commented Feb 5, 2018 at 13:31
  • Why on earth would you even want to use list(map(lambda... instead of a list comprehension? Commented Feb 5, 2018 at 13:44

4 Answers 4

3

You need to split the input parameter for the phrase variable.

print(list(map(lambda x: len(x), phrase.split(" "))))

Output:

[3, 4, 3, 3, 5, 2, 4, 6]

From the comments: better way of doing it. Thanks Lukas Graf.

print(list(map(len, phrase.split(" ")))
Sign up to request clarification or add additional context in comments.

4 Comments

That lambda x: len(x) is completely redundant. -> map(len, phrase.split(' '))
@LukasGraf; You are right. I was just helping the OP's original problem :).
@LukasGraf and so is the space to split; it's the default and can be omitted.
@Ev.Kounis no, the default is basically "any whitespace", which includes newlines. Slightly different semantics.
2

Here are 4 pieces of code for your logic.

I've added the map without lambda as this is most efficient, and also a list comprehension variety as many consider this most pythonic. Timings are indicative only.

phrase = 'How long are the words in this phrase'

def word_lengths(phrase):
    phrase = phrase.split(' ')
    wordLengthList = []
    for i in range(len(phrase)):
        wordLengthList.append(len(phrase[i]))
    return wordLengthList

def word_lengths_map(phrase):
    return list(map(len, phrase.split(' ')))

def word_lengths_lambda(phrase):
    return list(map(lambda x: len(x), phrase.split(' ')))

def word_lengths_lcomp(phrase):
    return [len(x) for x in phrase.split(' ')]

word_lengths(phrase)         # 4.5 microseconds
word_lengths_map(phrase)     # 2.3 microseconds
word_lengths_lambda(phrase)  # 4.0 microseconds
word_lengths_lcomp(phrase)   # 2.8 microseconds
# [3, 4, 3, 3, 5, 2, 4, 6]

Comments

0

doing this list(map(lambda x: len(x.split(' ')), phrase)) will make map go through each element of phrase (which is a single character), and it will split each of those 1 characters by ' ' (thus creating arrays of length 1)

you should instead split the entire phrase:

list(map(len, phrase.split(' ')))

1 Comment

@StefanPochmann you are right but there is no point in editing the answer
-1

Use map function as follows:

list(map(lambda word: len(word),phrase))

it works perfectly.there is no need to split the word again once we have it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.