Data Processing and Visulisation with Python
Python Exercise 19
Echo word
Rewrite the following Python function with lambda expression.
def echoWord(word, echo):
"""Concatenate echo copies of word."""
words = word * echo
return words
sample
#---------Finish the following statement in one line---------------
# Define echoWord as a lambda function
echoWord = lambda word,echo :word * echo
#--------------------End of your code------------------------------
#---------Finish the following statement in one line---------------
# Call echoWord with 'great!' and 5, assign the call to result
result = echoWord("great!",5)
#--------------------End of your code------------------------------
# Print result
print(result)
echoWord = lambda word,echo :word * echo
echoWord("great!",5)
print(echoWord("great!",5))
Shout out spells
Finish the following script.
- In the map() call, pass a lambda function that concatenates the string ‘!!!’ to a string item; also pass the list of strings
spells
. Assign the resulting map object toshout_spells
. - Convert
shout_spells
to a list and print out the list.
# Create a list of strings: spells
spells = ["protego", "accio", "expecto patronum", "legilimens"]
#---------Finish the following statement in one line---------------
# Use map() to apply a lambda function over spells
list(map(lambda x : x + '!!!',spells))
#--------------------End of your code------------------------------
#---------Finish the following statement in one line---------------
# Convert shout_spells to a list: shout_spells_list
shout_spells_list = list(map(lambda x : x +'!!!',spells))
#--------------------End of your code------------------------------
# Print the result
print(shout_spells_list)
Filter out list
Write one Python statement to get a new list which contains only odd elements from a given list.
Note : You can use list comprehension.
lst = [32,33,567,768,34,5,7,67,45,3,245,8,245,6,967,4]
#---------Finish the following statement in one line---------------
newList = [i for i in lst if i%2 != 0 ]
print(newList)
#--------------------End of your code------------------------------
newList
Word length
Write one Python statement to create a list containing the length for each word except ‘the’ in a given sentence.
sentence = 'the quick brown fox jumps over the lazy sleepy dog'
words = sentence.split()
#---------Finish the following statement in one line---------------
wordLen = [len(i) for i in words if i != 'the' ]
#--------------------End of your code------------------------------
print(wordLen)
Color with size
Write a Python statement to create a list of color size tuple to present all the color size assortment with given color list and size list.
colors = ['White', 'Black', 'Blue']
sizes = ['XS', 'S', 'M', 'L', 'XL']
#---------Finish the following statement in one line---------------
assortment = [color+" "+size for color in colors for size in sizes]
#--------------------End of your code------------------------------
print(assortment)
Heros names
Write a Python statement to create a dictionary of super heros with their names.
Note:
- List
heros
contains all the super heros. Listnames
contains their correspongding names. - You can use zip function
method 1
names = ['Bruce', 'Clark', 'Peter', 'Logan', 'Wade']
heros = ['Batman', 'Superman', 'Spiderman', 'Wolverine', 'Deadpool']
#---------Finish the following statement in one line---------------
heroNames = {heros[i]:names[i] for i in range(len(names))}
#--------------------End of your code------------------------------
print(heroNames)
method 2
names = ['Bruce', 'Clark', 'Peter', 'Logan', 'Wade']
heros = ['Batman', 'Superman', 'Spiderman', 'Wolverine', 'Deadpool']
#---------Finish the following statement in one line---------------
heroNames = dict(zip(names,heros))
#--------------------End of your code------------------------------
print(heroNames)
List difference
Write a Python statement (with two lists lst1
and lst2
) to create and return a new list containing all the elements in lst1
without all those in lst2
.
Note : Use both lambda and list comprehension.
method 1
#---------Finish the following statement in one line---------------
newList = lambda x,y : [i for i in x if i not in y]
#--------------------End of your code------------------------------
lst1 = [2,43,54,86,34,5,7,7,435,2,71,84,53]
lst2 = [34,65,4,72,5,54,6,32,5,34,67,28]
newList(lst1, lst2)
method 2
#---------Finish the following statement in one line---------------
newList = lambda l1,l2:[i for i in l1 if i not in l2]
#--------------------End of your code------------------------------
lst1 = [2,43,54,86,34,5,7,7,435,2,71,84,53]
lst2 = [34,65,4,72,5,54,6,32,5,34,67,28]
newList(lst1, lst2)
Same beginning and end
Write a Python statement to get a list of strings lst
and return a new list containing all the elements in lst
, which starts and ends with the same character.
#---------Finish the following statement in one line---------------
newList=lambda l:[x for x in l if x[0]==x[-1]]
#--------------------End of your code------------------------------
lst = ['sdfs','sgfgh','jhdfg','tyusdht']
newList(lst)
newList(['iugiu','iglhjk','weyuw','liu','llhjkl'])