Created
May 3, 2021 16:01
-
-
Save IvanFrecia/7ab38759a61c4db8d9eb0d54736e85e8 to your computer and use it in GitHub Desktop.
Data Collection and Processing with Python - Week 2 - 23.2. Map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1) Using map, create a list assigned to the variable greeting_doubled that doubles each element in the | |
# list lst. | |
lst = [["hi", "bye"], "hello", "goodbye", [9, 2], 4] | |
greeting_doubled = map((lambda a_list: a_list * 2), lst) | |
print(greeting_doubled) | |
# Output: | |
# [['hi', 'bye', 'hi', 'bye'], 'hellohello', 'goodbyegoodbye', [9, 2, 9, 2], 8] | |
# 2. Below, we have provided a list of strings called abbrevs. Use map to produce a new list called | |
# abbrevs_upper that contains all the same strings in upper case. | |
abbrevs = ["usa", "esp", "chn", "jpn", "mex", "can", "rus", "rsa", "jam"] | |
abbrevs_upper = map(lambda string: string.upper(), abbrevs) | |
print(abbrevs_upper) | |
# Output: | |
# ['USA', 'ESP', 'CHN', 'JPN', 'MEX', 'CAN', 'RUS', 'RSA', 'JAM'] |
you can use this instead
and for the second one
abbrevs_upper = list(map((lambda str: str.upper()), abbrevs))
print(abbrevs_upper)
Thanks! I forgot in the latest version of python you have to cast to list in these cases...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
lst = [["hi", "bye"], "hello", "goodbye", [9, 2], 4]
greeting_doubled = list(map((lambda value: 2*value), lst))
print(greeting_doubled)