0

I have two lists: a list of 2D-arrays and the time it was generated, which is an integer. They are of length N and both "equally disordered". So by using the indices to order the list 'time' I can order the list of 2D-arrays.

I would like to do something like:

ordered_list_of_arrays = np.asarray(disordered_list).argsort(np.asarray(time))
ordered_time = np.asarray(time).sort()

Another option would be to leave it as a list:

ordered_arrays = disordered_list[np.argsort(np.asarray(time))]
TypeError: only integer scalar arrays can be converted to a scalar index

By iterating np.argsort(time) I could sort my disordered_list but I would like to know if there is a better option for this or which are the best.

Thanks

1
  • 1
    li.sort(key=lambda x:time_list.index(x)) Commented Jul 15, 2020 at 17:23

1 Answer 1

1

Create an index array and sort it based on time_list's value. Then use those indices to construct sorted versions of both arrays.

Code:

def sorted_lists(time_list, array_list):
    sorted_indices = sorted(range(len(time_list)), key=lambda i: time_list[i])
    sorted_time_list = [time_list[i] for i in sorted_indices]
    sorted_array_list = [array_list[i] for i in sorted_indices]
    return sorted_time_list, sorted_array_list
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.