Question

In: Computer Science

Write a python programA3) Lambda Expressions. Use lambda expression to sort the list ["democratic", "republican", "equal",...

Write a python programA3) Lambda Expressions. Use lambda expression to sort the list ["democratic", "republican", "equal", "python", "break", "two", "ruby"] by: a) the last 2 characters of each list item in reverse order b) create new list from list above where each item has only 3 characters and first one is capitalized. For example, the list item = "democratic" will transform into "Dem". Apply alphabetically sorting to the new list.  

Solutions

Expert Solution

Program Code Screenshot

Sample Output

Program Code to Copy

l = ["democratic", "republican", "equal", "python", "break", "two", "ruby"]
#Sort the list by last 2 chars
l.sort(key=lambda x : x[-2:],reverse=True)
print('After sorting by last 2 chars ')
print(l)
#Create a new list by taking first 3 chars
nl = [x[0].upper()+x[1:3] for x in l]
print('New list by taking first 3 chars ',nl)

nl.sort()
print('After sorting alphabetically : ')
print(nl)


Related Solutions

write and essay on how Montesquieu makes a distinction between two types of republican governments: democratic...
write and essay on how Montesquieu makes a distinction between two types of republican governments: democratic and aristocratic. What distinguishes these two types of government (please discuss with respect to both their institutional form (nature) and their animating spirit (principle))? What are each republican form of government’s strengths and weaknesses?
Write a program in Python to practice evaluating some logical expressions and determine whether the expressions...
Write a program in Python to practice evaluating some logical expressions and determine whether the expressions are equivalent. Read in a 0 or 1 integer value for each of the following variables: A, B, C, and D. Use an if…else statement to change the 0 and 1 integer values into True and False Boolean values. Booleans in Python have to have this capitalization (True/False, not true/false or TRUE/FALSE) and shouldn’t contain double quotes (which makes them a String instead of...
Regular expressions are used in Python for describing and identifying specific patterns. In Python, we use...
Regular expressions are used in Python for describing and identifying specific patterns. In Python, we use “re” or “Regex” to denote regular expressions. Write the Python Code to return matching analytics word in the following given text. Write the Python Code to return how many times analytics word is provided in this text. Definitions are useful to the extent they serve a purpose. So, is defining analytics important? Yes and no. It’s not likely that we’ll ever arrive at a...
Question4: Create and use a lambda expression which calculates the sum of three numbers. Ask the...
Question4: Create and use a lambda expression which calculates the sum of three numbers. Ask the user for three numbers and use the lambda function to print the sum [6 marks] Question5: Write a generator function which generates the Fibonacci series. [0,1,2,4,8,16,…]. The Fibonacci series is a list of numbers where each number is the sum of the previous two. [3 marks] Then write the code to iterate through the first six numbers of sequence. [3 marks] Question6: Write a...
#Python 5. Write function called evaluate() that evaluates the following Python expressions or assignments as specified:...
#Python 5. Write function called evaluate() that evaluates the following Python expressions or assignments as specified: Request input from the user for three variables (floating-point or integer) x, y, z, and myAverage. If the average of the first three numbers equals the fourth number, print 'Your average is correct.'. If not print 'Your average is not correct'. and print the correct average. Print the largest value among x, y, and z. Print the minimum value of x, y, y. >>>...
use python 1. Write a program that a. defines a list of countries that are members...
use python 1. Write a program that a. defines a list of countries that are members of BRICS (Brazil, Russia, India, China, Sri Lanka) b. Check whether a country is a member of BRICS or not Program run Enter the name of country: Pakistan Pakistan is not a member of BRICS Enter the name of country : India India is a member of BRICS 2. Write a program to create a list of numbers in the range of 1 to...
Create a Python program that will take an unsorted list of 1000 integers and sort them...
Create a Python program that will take an unsorted list of 1000 integers and sort them using a bubble sort and an insertion sort. Your output should include displaying both sorted lists (from each algorithm) identifying each sorted list by the algorithm used.
I need to write an interpreter for arithmetic expressions in Python. This program receives an input...
I need to write an interpreter for arithmetic expressions in Python. This program receives an input string with an arithmetic expression and does: 1. prints a ">>>" as if it was a terminal 2. lets us allocate a variable to a a numeric value (as "a = 3") then it stores {'a':3} to a dictionary memory 3. whenever it receives an expression or input, transform it to postfix notation and print it (even when allocating variables) 4. it is able...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the functions defined below. You are expected to re-use these functions in implementing other functions in the file. Include a triple-quoted string at the bottom displaying your output. Here is the starter outline for the homework: a. def count_character(text, char): """ Count the number of times a character occurs in some text. Do not use the count() method. """ return 0 b. def count_sentences(text): """...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the functions defined below. You are expected to re-use these functions in implementing other functions in the file. Include a triple-quoted string at the bottom displaying your output. Here is the starter outline for the homework: g. def big_words(text, min_length=10): """ Return a list of big words whose length is at least min_length """ return [] h. def common_words(text, min_frequency=10): """ Return words occurring at...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT