Knowee
Questions
Features
Study Tools

Write a class MyList that inherits from list:Public instance method: def print_sorted(self): that prints the list, but sorted (ascending sort)You can assume that all the elements of the list will be of type intYou are not allowed to import any moduleguillaume@ubuntu:~/0x0A$ cat 1-main.py#!/usr/bin/python3MyList = __import__('1-my_list').MyListmy_list = MyList()my_list.append(1)my_list.append(4)my_list.append(2)my_list.append(3)my_list.append(5)print(my_list)my_list.print_sorted()print(my_list)guillaume@ubuntu:~/0x0A$ ./1-main.py[1, 4, 2, 3, 5][1, 2, 3, 4, 5][1, 4, 2, 3, 5]guillaume@ubuntu:~/0x0A$

Question

Write a class MyList that inherits from list:Public instance method: def print_sorted(self): that prints the list, but sorted (ascending sort)You can assume that all the elements of the list will be of type intYou are not allowed to import any moduleguillaume@ubuntu:~/0x0A cat 1-main.py#!/usr/bin/python3MyList = __import__('1-my_list').MyListmy_list = MyList()my_list.append(1)my_list.append(4)my_list.append(2)my_list.append(3)my_list.append(5)print(my_list)my_list.print_sorted()print(my_list)guillaume@ubuntu:~/0x0A ./1-main.py[1, 4, 2, 3, 5][1, 2, 3, 4, 5][1, 4, 2, 3, 5]guillaume@ubuntu:~/0x0A$

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is the Python code for the class MyList that inherits from list and includes a method to print the list in ascending order:

class MyList(list):
    def print_sorted(self):
        print(sorted(self))

This class inherits from the built-in list class in Python, which means it has all the methods and properties of a list. The print_sorted method uses the built-in sorted function to return a new list that contains all elements from the original list in ascending order, which is then printed.

You can use this class like this:

my_list = MyList()
my_list.append(1)
my_list.append(4)
my_list.append(2)
my_list.append(3)
my_list.append(5)
print(my_list)  # prints: [1, 4, 2, 3, 5]
my_list.print_sorted()  # prints: [1, 2, 3, 4, 5]
print(my_list)  # prints: [1, 4, 2, 3, 5]

As you can see, the original list is not modified by the print_sorted method.

This problem has been solved

Similar Questions

SortingEvery Python list has a sort method that sorts it in place. If you don’t want to messup your list, you can use the sorted function, which returns a new list:x = [4, 1, 2, 3]y = sorted(x) # y is [1, 2, 3, 4], x is unchangedx.sort() # now x is [1, 2, 3, 4]By default, sort (and sorted) sort a list from smallest to largest based on naivelycomparing the elements to one another.If you want elements sorted from largest to smallest, you can specify a reverse=Trueparameter. And instead of comparing the elements themselves, you can compare theresults of a function that you specify with key:# sort the list by absolute value from largest to smallestx = sorted([-4, 1, -2, 3], key=abs, reverse=True) # is [-4, 3, -2, 1]# sort the words and counts from highest count to lowestwc = sorted(word_counts.items(),key=lambda word_and_count: word_and_count[1],reverse=True)

Define a function sort_list(list1). The function returns three lists: the original list, the list in ascending order, and the list in descending order. Remember: your function should return three lists.Examplelist1 = [10, 1, 9, 2, 8, 3, 7, 4, 6, 5]Expected output:[10, 1, 9, 2, 8, 3, 7, 4, 6, 5][1, 2, 3, 4, 5, 6, 7, 8, 9, 10][10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

my_first_list = ['a', 'b', 'c']my_second_list = ['d', 'e', 'f', 'g']my_third_list = my_second_list + my_first_listmy_third_list.pop()my_third_list.sort()print (my_third_list)

Consider a list (list = []). You can perform the following commands:insert i e: Insert integer at position .print: Print the list.remove e: Delete the first occurrence of integer .append e: Insert integer at the end of the list.sort: Sort the list.pop: Pop the last element from the list.reverse: Reverse the list.Initialize your list and read in the value of followed by lines of commands where each command will be of the types listed above. Iterate through each command in order and perform the corresponding operation on your list.

list_sort function Sorts the argument input list (nums) in descending orderuser_code.py123def list_sort(nums): # write some code here passRestore original fileSave onlySave & Grade Unlimited attempts

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.