Question: Create a list of 100 random strings of random length between 5 and 8 characters and consisting of upper and lower case characters. Use the
Create a list of 100 random strings of random length between 5 and 8 characters and consisting of upper and lower case characters. Use the random functions that we discussed in class to create the strings. You can use the following to create a list of all upper and lower case letters. 2. Letters [ sorted(chr(cx) for c in range(97,123) for x in (0,32)1 As you can see above, a lambda is a simple function that has no name and can be used anywhere that a function can be used. The syntax of the Python lambda is: lambda variable_list: definition ex. lambda x x**5 or another using two variables lambda (x,y):x-5 +3 when used in the sorted function the variable will apply to each item in the list to be sorted. You don't have to manually do anything else. For example: ?[ Zebra',' Mallet','Ant','Angel','Demon'] M [4, 5, 2, 8] L- sorted(L, key lambda x: len(x)) sorts the list based on the length of each item L sorted(L, key lambda x: x[2]) sorts the list based on the third character in each item. It will give an error if one of the items is too short. We could change it to use a slice and it will work fine. L- sorted(L, key lambda x: x[3:4]) Lambdas can be assigned to a variable and then used list a function call. For example, H - lambda xy:x3+y+8 assigns the lambda to H, now H(2,5) will invoke the lambda and return the calculated value of 2**3+5+8 or 21. sorted() also has an option to reverse the sort. So L- sorted(L, key- len, reverse- True) sorts the list from longest to shortest. Notice we didn't use a lambda in this sort but just the function name that we applied
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
