Let's assume following list
my_list = ['bottle', 'coffee', 'insufficient', 'differential', 'cat']
I want to write a function that takes two parameters as the input where m are the characters as a string that I want to find (e.g. 'ff') and n is the number of strings as an integer that I want to output.
So def contains_strings('ff', 2):
should output:
['coffee', 'insufficient']
Both words contain 'ff' and the output is 2 in total. The second parameter can be random. So I don't care if the function outputs. ['coffee', 'insufficient'] or ['differential', 'insufficient']
I was only able to write a function that is able to output words that start with certain characters:
import random as _random
my_list = ['bottle', 'coffee', 'insufficient', 'differential', 'cat']
def starting_letters(m: str, n: int):
a = list(filter(lambda x: x.lower().startswith(m), my_list))
print(_random.choices(a, k=n))
starting_letters('c', 2)
Output:
['coffee', 'cat']