Skip to main content
added 250 characters in body
Source Link
Martijn Pieters
  • 1.1m
  • 325
  • 4.2k
  • 3.4k

This is going to be painful, but using default python you have 2 options:

  • decorate the 3rd list1st and 2nd lists with enumerate(), then sort, then sort the other lists based on these using the extra index. to refer to values from the 3rd list:

      cat_sorted = [
          [e for i, e in sorted(enumerate(cat[0]), key=lambda p: cat[2][p[0]])],
          [e for i, e in sorted(enumerate(cat[1]), key=lambda p: cat[2][p[0]])],
          sorted(cat[2])
      ]
    

although it may help to sort cat[2] in-place instead of using sorted(); you cannot get around using sorted() for the other two.

  • zip() the three lists together, then sort on the third element of this new list of lists, then zip() again to get back to the original structure:

      from operator import itemgetter
      cat_sorted = zip(*sorted(zip(*cat), key=itemgetter(2)))
    

Neither will be a performance buster, not with plain python lists of millions of numbers.

This is going to be painful, but using default python you have 2 options:

  • decorate the 3rd list with enumerate(), then sort, then sort the other lists based on the extra index.

  • zip() the three lists together, then sort on the third element this new list of lists, then zip() again to get back to the original structure:

      from operator import itemgetter
      cat_sorted = zip(*sorted(zip(*cat), key=itemgetter(2)))
    

This is going to be painful, but using default python you have 2 options:

  • decorate the 1st and 2nd lists with enumerate(), then sort these using the index to refer to values from the 3rd list:

      cat_sorted = [
          [e for i, e in sorted(enumerate(cat[0]), key=lambda p: cat[2][p[0]])],
          [e for i, e in sorted(enumerate(cat[1]), key=lambda p: cat[2][p[0]])],
          sorted(cat[2])
      ]
    

although it may help to sort cat[2] in-place instead of using sorted(); you cannot get around using sorted() for the other two.

  • zip() the three lists together, then sort on the third element of this new list of lists, then zip() again to get back to the original structure:

      from operator import itemgetter
      cat_sorted = zip(*sorted(zip(*cat), key=itemgetter(2)))
    

Neither will be a performance buster, not with plain python lists of millions of numbers.

added 100 characters in body
Source Link
Martijn Pieters
  • 1.1m
  • 325
  • 4.2k
  • 3.4k

This is going to be painful, but using default python you have 2 options:

  • decorate the 3rd list with enumerate(), then sort, then sort the other lists based on the extra index.

  • zip() the three lists together, then sort on the third element this new list of lists, then zip() again to get back to the original structure.:

      from operator import itemgetter
      cat_sorted = zip(*sorted(zip(*cat), key=itemgetter(2)))
    

This is going to be painful, but you have 2 options:

  • decorate the 3rd list with enumerate(), then sort, then sort the other lists based on the extra index.

  • zip() the three lists together, then sort on the third element this new list of lists, then zip() again to get back to the original structure.

This is going to be painful, but using default python you have 2 options:

  • decorate the 3rd list with enumerate(), then sort, then sort the other lists based on the extra index.

  • zip() the three lists together, then sort on the third element this new list of lists, then zip() again to get back to the original structure:

      from operator import itemgetter
      cat_sorted = zip(*sorted(zip(*cat), key=itemgetter(2)))
    
Source Link
Martijn Pieters
  • 1.1m
  • 325
  • 4.2k
  • 3.4k

This is going to be painful, but you have 2 options:

  • decorate the 3rd list with enumerate(), then sort, then sort the other lists based on the extra index.

  • zip() the three lists together, then sort on the third element this new list of lists, then zip() again to get back to the original structure.