1

I am trying to sort a numpy array of tuples. somehow, I came accross a solution like the following, which would give me flexibilities to control the indexes and order of each.

m = mm[mm[:,0].argsort()] # First sort doesn't need to be stable.
m = mm[mm[:,1].argsort(kind='mergesort')[::-1][:100000]]
m = mm[mm[:,3].argsort(kind='mergesort')[::-1][:100000]]
m = mm[mm[:,2].argsort(kind='mergesort')[::-1][:100000]]

but I have received an error as like

IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed

does it means this kind of solution does not apply to tuple but list?

would someone suggest a better way that I could speed up the sorting procedure rather than np.argsort(mm, order=('a','b','c','d'))?

how would order be able to cope like np.argsort(mm, order=('a','b',-'c','d')) where I can have the index 'c' in reverse order?

Thanks you

user2625363
  • 845
  • 2
  • 10
  • 15
  • The code you copied looks like it's for a two-dimensional array, not for an array of tuples. These are different things, and hence the error message. What are you trying to do? What does your numpy array look like? Why are you using numpy for an array of tuples? – Frank Yellin Sep 21 '21 at 17:06
  • The numpy array is like [(1,2,3,4,'A'),(4,5,6,7,'B')], I have tried to use list instead, but dtype = [('e',int), ('c',int), ('b',int),('f',int),('k','S40')] is still getting error that the 'K' string is "ValueError: invalid literal for int()" – user2625363 Sep 21 '21 at 17:11
  • I'm still really confused. Why are you using Numpy? You've just got a list of tuples. I assume every element is (int, int, int, int, string). What happens when you just call `mylist.sort()`? Are you just trying to sort an array of tuples, or is there something more subtle going on here. You really haven't explained this well. – Frank Yellin Sep 21 '21 at 17:17
  • I have to sort the data with some indexes would be in decending order while others would be in asending – user2625363 Sep 21 '21 at 17:36
  • https://stackoverflow.com/questions/9376384/sort-a-list-of-tuples-depending-on-two-elements – Frank Yellin Sep 21 '21 at 19:46

1 Answers1

0

You talk about an "array of tuples", which is without context vague.

Your use of:

np.argsort(mm, order=('a','b','c','d'))

suggests that mm is a structured array, with fields named, 'a','b', etc.

That would also account for the dimension error when doing mm[:,0]. You can't treat a 1d structured array as though it were 2d with columns!

You should have told us mm.shape and mm.dtype.

As for reversing the sort on the 'c' field, I'd make a copy of the array, and assign negative values to the 'c' field.

I don't think there's an alternative to doing this sort of 'ordered' sort. There is a np.lexsort which you might apply to an "unstructured" copy of your "structured" array (assuming the fields have uniform dtypes). But I don't know if the speed's any different.

hpaulj
  • 221,503
  • 14
  • 230
  • 353