简体   繁体   English

如何在python中对二维数组进行排序

[英]How to sort two-dimensional array in python

Tool = [[0 for x in xrange(3)] for y in xrange(len(xTool)-1)]
for l in xrange((len(xTool) - 1)):
  Tool[l][0] = yTool[l]; Tool[l][1] = xTool[l]; Tool[l][2] = zTool[l]  

I am starting with points coordinates, which are in 3 lists (xTool, yTool, zTool), representing respectively the x, y and z coordinates of all my points. 我从点坐标开始,这是3个列表(xTool,yTool,zTool),分别代表我所有点的x,y和z坐标。

The aim here is to create a matrix of 3 columns and many rows (over 10,000), where each row represent the point's 3 coordinates. 此处的目的是创建一个由3列和许多行(超过10,000个)组成的矩阵,其中每一行代表该点的3个坐标。 The next step I do is a vector transformation like so: ( This has minor importance, only if you really want to understand what i'm doing 我要做的下一步是这样的向量转换:( 仅当您真的想了解我在做什么时,这才具有次要的重要性

rTool = numpy.zeros_like(Tool)   
for rt in xrange((len(xTool) - 1)):
  rTool[rt][0] = (Tool[rt][0] * cos(angle)) - (Tool[rt][1] * sin(angle))
  rTool[rt][1] = (Tool[rt][0] * sin(angle)) + (Tool[rt][1] * cos(angle))
  rTool[rt][2] = Tool[rt][2]

Finally, what I'm trying to do is order my rTool in regards to my 2nd column ([1]). 最后,我想做的是根据第二列([1])订购我的rTool。 For instance, I printed 5 entries of my rTool. 例如,我打印了5个rTool条目。 By sorting them according to the 2nd column, the last row should be the first. 通过根据第二列对它们进行排序,最后一行应该是第一行。 I am really struggling to do this, and I suspect it's because I have tuples instead of a real 3 column mathematical matrix. 我确实很难做到这一点,我怀疑这是因为我有元组而不是真正的3列数学矩阵。

[[ -584.89837646 -3648.6472168    402.177948  ]
 [ -542.8659668  -3663.34545898   405.76959229]
 [ -500.831604   -3678.04785156   409.32122803]
 [ -458.79336548 -3692.75854492   412.7930603 ]
 [ -416.74984741 -3637.48022461   416.15090942]]

Don't hesitate to ask for clarification and I hope you will be able to help me! 不要犹豫,要求澄清,希望您能为我提供帮助! Thanks. 谢谢。

First and foremost, learn numpy. 首先,学习numpy。 Doing this kind of thing in plain python goes against everything pythonic. 在普通的python中做这种事情与所有pythonic背道而驰。

Once you've done that: 完成此操作后:

sorted_rtool = rTool[np.argsort(rTool[:,1])]

To drive the importance of numpy home: 推动numpy home的重要性:

rTool = np.dot(Tool, R)

Is not only a lot cleaner, its also orders of magnitude faster. 它不仅干净很多,而且速度也快了几个数量级。

You could use the 'key' parameter of 'sort' or 'sorted', see https://wiki.python.org/moin/HowTo/Sorting#Key_Functions 您可以使用'sort'或'sorted'的'key'参数,请参见https://wiki.python.org/moin/HowTo/Sorting#Key_Functions

In your case: 在您的情况下:

rTool.sort(key=lambda x: x[1])

or 要么

rToolSorted = sorted(rTool, key=lambda x: x[1])

You're looking for the key keyword of list.sort() (or the builtin sorted() ) Look at the Key Functions section here . 您正在寻找list.sort()key关键字(或内置的sorted() )。在此处查看Key Functions部分。

Often people just use a lambda function for the sort key, as it's concise, and you often don't use the sort function more than once. 简而言之,人们通常只是将lambda函数用作排序键,而您通常不会多次使用sort函数。 All it is, is a function which returns an object key , on which each element of the list is sorted. 它是一个返回对象key的函数,在该key上对列表的每个元素进行排序。

So you could do: 因此,您可以执行以下操作:

def keyFunc(element):
  return element[1]

rTool.sort(key=keyFunc)

or: 要么:

rTool.sort(key=lambda x: x[1])

In both cases, you could use rTool = sorted(rTool, key=...) instead, the difference being that list.sort() does an inplace sort, and is more efficient if you don't need the original array. 在这两种情况下,都可以改用rTool = sorted(rTool, key=...) ,区别在于list.sort()进行就地排序,如果不需要原始数组,效率更高。

The key function can really be anything you want, provided it returns something else that can be sorted, so if your points were objects with x, y, z attributes, then you could do rTool.sort(key x: xy) key函数实际上可以是您想要的任何东西,只要它返回可以排序的其他内容,那么如果您的点是具有x, y, z属性的对象,则可以执行rTool.sort(key x: xy)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM