开发者

Two Dimensional Python Array: Sort second by First

开发者 https://www.devze.com 2022-12-29 15:48 出处:网络
I have开发者_如何学Python a Multidimensional array in Python. How do I go about sorting the second array, by the first - all the while keeping it in the same order?I\'m not sure from your answer if t

I have开发者_如何学Python a Multidimensional array in Python.

How do I go about sorting the second array, by the first - all the while keeping it in the same order?


I'm not sure from your answer if this is what you want, but take a look and see. If I have a multidimensional array x:

>>> x = [[100,50,39,69,22,23,19,80,94,72],range(10)]
>>> print x
[[100, 50, 39, 69, 22, 23, 19, 80, 94, 72], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]

and I want to sort the second subarray by the first subarray, I could do the following:

>>> x[1].sort(key = x[0].__getitem__)
>>> print x
[[100, 50, 39, 69, 22, 23, 19, 80, 94, 72], [6, 4, 5, 2, 1, 3, 9, 7, 8, 0]]

Is that what you're looking for?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号