Lambda : Data Processing and Visulisation with Python (Python Exercise 19)

Data Processing and Visulisation with Python

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.

  1. 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 to shout_spells.
  2. 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:

  1. List heros contains all the super heros. List names contains their correspongding names.
  2. 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'])

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值