-1

I have a list generated from a 2-d array which looks like below

a = [[4,3],[4,5]]

Each item in the list a is the row,column collected from the coordinates of interest of the 2-d array

I would like to sort this list a, so that I can get the lexicographically smallest row, column which in this case is [4,3]

I have tried

np.lexsort([[4,3],[4,5]])

But it cannot comprehend the output.

Ahamed Moosa
  • 1,395
  • 7
  • 16
  • 30

2 Answers2

1

np.lexsort uses the last element as a primary key, so it first sorts on 3 and 5 in your example. If you want to use the first element as a key you can do the following:

import numpy as np
a = np.array([[4,3],[4,5],[5,2],[5,6]])
print(np.lexsort(np.fliplr(a).T))

output:

[0 1 2 3]

The output states that the first element, [4,3], is the smallest, and therefore this element gets the value 0.

joostblack
  • 2,465
  • 5
  • 14
1

You say "I would like to sort this list a, so that I can get the lexicographically smallest row, column which in this case is [4,3]". But if all you're interested in is getting the smallest element of a list, then you don't need to sort it. You can just use builtin function min.

Min row

Both sorting and taking the min use lexicographical order by default in python, so you can just call min directly:

a = [[4,3],[4,5]]
min_row = min(a)
print(min_row)
# [4, 3]

Min column

If you want to get the min column in lexicographical order, then you need to transpose your list of lists so that the inner lists correspond to the columns. See this relevant question: Transpose list of lists and the documentation on zip

a = [[4,3],[4,5]]
min_col = min(zip(*a))
print(min_col)
# (3, 5)

Sorting

If you really want to sort rather than taking the minimum element, then you can use .sort() or sorted( ) rather than min( ).

a = [[4,3],[4,5]]

# sorting a copy
sorted_rows = sorted(a)
print(sorted_rows)

# sorting in-place
a.sort()
print(a)
Stef
  • 13,242
  • 2
  • 17
  • 28
  • Thanks for your answer. The example I gave was a simple one. If I have to test with many use cases then , I thought sorting is the best option. – Ahamed Moosa Sep 02 '21 at 12:01
  • @MosesSoleman That entirely depends on what "many use cases" means. – Stef Sep 02 '21 at 12:03
  • In case if I have to decide which is smaller among [[4,5], [6,3], [7,2]] ? – Ahamed Moosa Sep 02 '21 at 12:05
  • 1
    @MosesSoleman If you are looking for the `k` smallest rows in a table of `n` rows, with k << n, then rather than sorting you can use [`numpy.argpartition`](https://stackoverflow.com/questions/34226400/find-the-index-of-the-k-smallest-values-of-a-numpy-array) or [`heapq.nlargest`](https://docs.python.org/3/library/heapq.html#heapq.nlargest) – Stef Sep 02 '21 at 12:05
  • @MosesSoleman If you only want the smallest row, then use `min`. For instance `min([[4,5], [6,3], [7,2]])` – Stef Sep 02 '21 at 12:06
  • Thanks for the suggestions @Stef. Actually I want to decide which co-ordinate is lexicographical smaller by factoring in both row and column. Not just row or column. – Ahamed Moosa Sep 02 '21 at 12:09
  • 1
    @MosesSoleman Then I suggest you ask a new question with a more complete explanation of what you want. It's hard/impossible to understand/guess what you want from the question you actually asked and from these comments. Or rather to ask a new question, you could take some time and try to find an algorithm to solve your problem yourself. – Stef Sep 02 '21 at 12:12
  • I appreciate your effort to answer my question. I will try to rephrase the question and try to write an algorithm to solve it – Ahamed Moosa Sep 02 '21 at 12:23