forked from arsho/46-Simple-Python-Exercises-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_27.py
More file actions
35 lines (26 loc) · 1.11 KB
/
problem_27.py
File metadata and controls
35 lines (26 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
'''
Write a program that maps a list of words into a list of
integers representing the lengths of the correponding words.
Write it in three different ways:
1) using a for-loop,
2) using the higher order function map()
3) using list comprehensions
'''
def map_word_with_length_using_for(word_list):
map_word_length = dict()
for item in word_list:
map_word_length[item] = len(item)
return map_word_length
#####################################################
def map_word_with_length_using_map(word_list):
word_len_list = list(map(lambda x:len(x),word_list))
return dict(zip(word_list,word_len_list))
#####################################################
def map_word_with_length_using_list_comprehensions(word_list):
word_len_list = [len(item) for item in word_list]
return dict(zip(word_list, word_len_list))
# list of Shanto's imaginary girlfriends :D
word_list = ['mithila','prokriti','amrita']
print(map_word_with_length_using_for(word_list))
print(map_word_with_length_using_map(word_list))
print(map_word_with_length_using_list_comprehensions(word_list))