• Register
0 votes
44 views

Problem:

Given that play_list has been defined to be a list, write a statement that sorts the list?

9.9k points

Please log in or register to answer this question.

1 Answer

0 votes

Solution:

Depending on the contents of list, different approaches should be used. In easiest possible case, when play_list is list of integers or strings, sorted() function should be used:
play_list = [3, 1, 2, 5, 4]

sorted_play_list = sorted(play_list)

print(sorted_play_list) # [1, 2, 3, 4, 5]

#----------------------------------------

play_list = ['cdgf', 'fs', 'aa', 'Zsdf', 'qvtr', 'fa']

sorted_play_list = sorted(play_list)

print(sorted_play_list) # ['Zsdf', 'aa', 'cdgf', 'fa', 'fs', 'qvtr']

If contents of play_list are complex objects (like lists or class object), the key parameter should specified, which will be used for comparisons:

play_list = [[2, 'b'],[1, 'c'],[3, 'a']]

# sort by second element of nettled list

sorted_play_list = sorted(play_list, key=lambda a: a[1])

print(sorted_play_list) # [[3, 'a'], [2, 'b'], [1, 'c']]

 

Explanation:

Sorting is ordering a list of objects. We can distinguish two types of sorting. If the number of objects is small enough to fits into the main memory, sorting is called internal sorting. To sort the whole table, select the table. Move the pointer over the letter above the column by which you want to sort. Click the arrow that appears, then choose a sorting option: Sort Ascending: Sort the data in alphabetical order (A to Z) or by increasing numerical values.

14.7k points

Related questions

0 votes
2 answers 260 views
260 views
Problem : I need solution for below problem using Python Given that play_list has been defined to be a list, write a statement that sorts the list.
asked Nov 24, 2019 alecxe 7.5k points
0 votes
1 answer 13 views
13 views
Problem: I am asked to reverse a which takes head as a parameter whereas head is a linked list The function I have written for reversing the list is giving [3,2,1,2,1,2,1,2,1] and works only for a list of length 3. How can I generalize it for a list of length n?
asked Apr 25 ummeshani 10.8k points
0 votes
1 answer 19 views
19 views
Problem: I have no idea to solve this, somebody help : Given a list named play_list, write an expression whose value is the length of play_list.
asked Jun 12 asha 121k points
0 votes
2 answers 94 views
94 views
Problem: Given the array a, write an expression that refers to the first element of the array?
asked Apr 12, 2020 ArifulIslam 9.9k points
0 votes
2 answers 192 views
192 views
Problem: In C++, you can create aliases to a previously defined data type by using the ____ statement?
asked Apr 12, 2020 ArifulIslam 9.9k points
0 votes
1 answer 12 views
12 views
Problem: Can someone please help fix this > Write a program that reads in three strings and sorts them lexicographically
asked May 1 ummeshani 10.8k points
0 votes
1 answer 80 views
80 views
Problem: Hi there! I am confused in while loops and I need to write a program in which a value is taken as input from the user. If the value is less than 0, the program should increment it by 5 as long as the value remains less than 0. As the value becomes greater than or equal to 0, it should print the number of times the value has been incremented and the final value. Any help??
asked Jan 21 Code Learner 10.5k points
0 votes
1 answer 106 views
106 views
Problem: Write a conditional that assigns True to fever if temperature is greater than 98.6.?
asked Apr 18, 2020 ArifulIslam 9.9k points
0 votes
2 answers 1.1k views
1.1k views
Problem: which statement can be used to handle some of the runtime errors in a program?
asked Mar 3, 2020 JHill 200 points
0 votes
1 answer 33 views
33 views
Problem: I was trying to solve a problem in Codility provided below, Write a function: class Solution { public int solution(int[] A); } that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A. For example, given A = [1, 3, 6, 4, 1, 2], the function ... for (int i = 0; i < N; i++) { if (C[i] != 0) { return (i + 1); } } return (N + 1); }
asked Apr 25 sumaiya simi 43.9k points