Use python
x is a list of strings. For example,
x = [’python is cool’, ’pythom is a large heavy-bodied snake’, ’The
python course is worse taking’]
In the example, there is totally 1 occurrence of the word
’python’ (case sensitive) in the strings whose length are more than
20 in the list x.
In order to count the number of occurrences of a certain word str
in the strings which are long enough, use filter and
reduce and write a function word_count(x, str,
n) in
the functional programming paradigm.
This function takes a list of strings x , the string we want to
find str and a number n as the inputs. The output or the returned
value is the total number of occurrences of the string
str in the strings whose length is more than n(>n) in the list
x. The function should be in the following format:
def word_count(x, str, n):
# your code here
Hint:
• You could use filter to filter out the string whose length are
not long enough and usereduce to do the summation.
• str.count(sub) return the number of occurrences of substring sub
in string str.
Note that you are required to use filter, reduce to finish your code.
Python code:
from functools import reduce
def word_count(x,str1,n):
li = list(filter(lambda x:len(x)>n,x))
cou = reduce(lambda x,y:x+y.count(str1),li,0)
print("Number of occurences of string "+str1+" in given list
elements whose length is >"+str(n)+" is : ",end="")
print(cou)
x = ['python is cool','pythom is a large heavy-bodied snake','The
python course is worse taking']
word_count(x,'python',20)
Execution screenshots:
Note:please like the answer.Thank you.Have a nice day.
Get Answers For Free
Most questions answered within 1 hours.