Change language

heapq in Python to print all elements in sorted order from a row and column sorted matrix

| | |

Examples:

 Input: mat = [[10, 20, 30, 40], [15, 25, 35, 45], [27, 29, 37, 48], [32, 33, 39, 50]] Output: Elements of matrix in sorted order [10, 15, 20, 25, 27, 29, 30, 32, 33, 35, 37, 39, 40, 45, 48, 50] 

This problem has an existing solution, please refer to combining two sorted arrays using heapq module .

# A function to print all elements in sorted order
# from a matrix sorted by row and column

from heapq import merge

 

def sortedMatrix (mat ):

 

# initialize the result variable with the first line matrices

result = mat [ 0 ]

 

# now go through the full matrix

# after the first row and concatenate each row with

# result one by one

# after the last operation, the result will contain

# list of sorted matrix elements

for row in mat [ 1 :]:

  result = list (merge (result, row))

  

  return result

  

if __ name__ = = "__ main__" :

  mat = [[ 10 , 20 , 30 , 40 ], [ 15 , 25 , 35 , 45 ],

[ 27 , 29 , 37 , 48 ], [ 32 , 33 , 39 , 50 ]]

  print ’Elements of matrix in sorted order’

  print sortedMatrix (mat)

Output:

 Elements of matrix in sorted order [10, 15, 20, 25, 27, 29, 30, 32, 33, 35, 37, 39, 40, 45, 48, 50] 

This article courtesy of Shashankom Mishra (Gullu) . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you’d like to share more information on the topic discussed above.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically