Get reliable answers to your questions at Westonci.ca, where our knowledgeable community is always ready to help. Discover comprehensive answers to your questions from knowledgeable professionals on our user-friendly platform. Explore comprehensive solutions to your questions from a wide range of professionals on our user-friendly platform.

Please do in python.Given an array of integers, create a 2-dimensional array where the first element is a distinct value from the array and the second element is the value’s frequency within the array. Sort the resulting array descending by frequency. If multiple values have the same frequency, they should be sorted ascending.Example: arr = [3,3,1,2,1]There are two values, 3&1 each within frequency of 2, and one value 2 with a frequency of 1: [[3,2], [1,2], [2,1]] Sort the 2-dimensional descending by frequency: [[3,2], [1,2], [2,1]] Sort the 2-dimensional array descending by values with matching frequencies [[1,2], [3,2], [2,1]]

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

View image codiepienagoya