Get comprehensive solutions to your questions with the help of IDNLearn.com's experts. Get accurate and timely answers to your queries from our extensive network of experienced professionals.
Sagot :
Following are the Python program to sort the 2-dimensional array in descending order.
Program Explanation:
- Defining a list "arr" that holds an integer value.
- Defining multiple empty lists "m,n, and o".
- In the next step, a for loop is declared that defines an if block that uses an append method to add value into the list.
- Inside this, "m" variable that sort by frequency of array, and declared two for loop with the if conditional block.
- In this, it sorts array and store its value with the "m" and print its value.
Program:
arr=[3,3,1,2,1]#defining a list
m=[] #defining an empty list m
n=[] #defining an empty list n
o=[]#defining an empty list o
for i in arr: #defining a for loop that iterate arr
if i not in o: # defining if block that add value in empty list
n.append(i)#using append method to add value
n.append(arr.count(i))#using append method add its index value
m.append(n) #element and its frequency storeds in 2d matrix
n=[]#defining an empty list n
o.append(i)#calling append method to add value
m=sorted(m,key=lambda l:l[1], reverse=True) # defining m variable that sort by frequency of array
# sort by element
for i in range(len(m)):#using loop variable i that takes m matrix length
for j in range(len(m)):#using loop variable j that takes m matrix length
if m[i][0]<m[j][0] and m[i][1]==m[j][1]:#using if block that sort array element
m[i],m[j]=m[j],m[i]#defining matrix that hold matrix value
print(m) #print matrix value
Output:
Please find the attached file.
Learn more:
brainly.com/question/12286809
Thank you for being part of this discussion. Keep exploring, asking questions, and sharing your insights with the community. Together, we can find the best solutions. Find reliable answers at IDNLearn.com. Thanks for stopping by, and come back for more trustworthy solutions.