• Register
0 votes
182 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.

6 5 3
7,540 points

Please log in or register to answer this question.

2 Answers

0 votes

Solution:

I have faced the same Problem in the recent past. I have the  following solution on your problem.

Depending on the contents of list, there different approaches and they can be used according to need. In the easiest possible case, when the play_list is considered the list of integers or strings, the sorted() function must be used as follows:

 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 the contents of play_list are complex objects e.g. lists or class object  the key parameter must  be specified that will be used for comparisons as follows:

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']]

So I will suggest the following approaches:

1) If the elements are atomic, then you can use sorted(play_list);

2) If the elements are complex, then you can use sorted(play_list, key=key)

9 7 4
38,600 points
0 votes

Solution:

The sort() method sorts the elements of a given list.

The sort() method sorts the elements of a given list in a specific ascending or descending order.

The syntax of the sort() method is:

list.sort(key=..., reverse=...)

Alternatively, you can also use Python's built-in sorted() function for the same purpose.

sorted(list, key=..., reverse=...)

Example: sort a given list

# vowels list
vowels = ['e', 'a', 'u', 'o', 'i']

# sort the vowels
vowels.sort()

# print vowels
print('Sorted list:', vowels)

Sorting user using user defined order.

list_name.sort(key=…, reverse=…) – it sorts according to user’s choice
# Python program to demonstrate sorting by user's 
# choice 

# function to return the second element of the 
# two elements passed as the parameter 
def sortSecond(val): 
	return val[1] 

# list1 to demonstrate the use of sorting 
# using using second key 
list1 = [(1, 2), (3, 3), (1, 1)] 

# sorts the array in ascending according to 
# second element 
list1.sort(key = sortSecond) 
print(list1) 

# sorts the array in descending according to 
# second element 
list1.sort(key = sortSecond, reverse = True) 
print(list1) 

 

10 6 4
31,120 points

Related questions

0 votes
1 answer 25 views
25 views
Problem: Given that play_list has been defined to be a list, write a statement that sorts the list?
asked Mar 9, 2020 ArifulIslam 7.5k points
0 votes
1 answer 150 views
150 views
Problem : Facing following issue related to list in Python given that a refers to a list, write the necessary code to reverse the elements of the list.
asked Nov 16, 2019 peterlaw 6.9k points
1 vote
1 answer 238 views
238 views
Problem: I am new in python programming language and I am very confused about programming logics. Supose, someone has given two already defined variables, i and j, write a statement that swaps their associated values. Can anyone help me with this problem?
asked Apr 9, 2020 LizzyM 6.1k points
0 votes
2 answers 190 views
190 views
Problem: I am enable to write statement which can define plist to be the empty list. Can anyone help? Write a statement that defines plist to be the empty list?
asked Feb 17, 2020 maddi86 5.4k points
0 votes
2 answers 772 views
772 views
Problem : Python Program to Calculate the Average of Numbers in a Given List
asked Oct 21, 2019 peterlaw 6.9k points
0 votes
1 answer 27 views
27 views
Problem: How can I write a program that reads an unspecified number of integers and then prints how many numbers the user has entered and their sum without using a list or array?
asked Feb 27 Code Learner 9.2k points
0 votes
1 answer 10 views
10 views
Problem: For a list ["foo","bar","baz"]and a list item "bar", what is the cleanest way to get its index (1) in python?
asked Feb 27 sasha 16.2k points
0 votes
1 answer 10 views
10 views
Problem: Is this the cleanest way to write a list to a file, since writelines() doesn't insert newline characters? file.writelines(["%s\n" % item for item in list]) It seems like there would be a standard way...
asked Feb 17 Mashhoodch 13k points
0 votes
2 answers 724 views
724 views
Problem : I am new to Unity5. I am facing following error while trying to do setDestination. "SetDestination" can only be called on an active agent that has been placed on a NavMesh. UnityEngine.NavMeshAgent:SetDestination(Vector3) CompleteProject.EnemyMovement:Update() (at Assets/ ... ; } } } } May I know what is wrong with the above code? Does anyone have any solution on my issue?
asked Nov 27, 2019 alecxe 7.5k points