简体   繁体   English

在Jython / Python中排序2D列表

[英]Sort 2D list in Jython/Python

I'm using WLST (python/jython) to get the configuration of some WebLogic resources. 我正在使用WLST(python / jython)来获取一些WebLogic资源的配置。 I loop in the queues of a jms module and for each queue I recover the name and some other parameters. 我循环进入jms模块的队列,并为每个队列恢复名称和其他一些参数。

With this info I build a 2D list that I want to order by queueName. 通过此信息,我建立了要按queueName排序的2D列表。

While I can successfully do it these two ways in the python console: 虽然我可以在python控制台中成功完成以下两种操作:

from operator import itemgetter
L=[["queueName1", 1, 2], ["queueName2", 2, 3], ["queueName3", 4, 1]]
sorted(L, key=itemgetter(0))

or 要么

L=[["queueName1", 1, 2], ["queueName2", 2, 3], ["queueName3", 4, 1]]
sorted(L, key=lambda x: x[0])

The version of python/jython (I don't really know what is used) in my WL server (version 10.3.5) doesn't like this when I use a .py script: 当我使用.py脚本时,WL服务器(版本10.3.5)中的python / jython版本(我真的不知道使用了什么)不喜欢这样:

list2d.sort(key=lambda x: x[0])

I get the error: 我得到错误:

Problem invoking WLST - Traceback (innermost last):
  File "/home/user/scripts/pythonscripts/get_jms_config.py", line 98, in ?
  File "/home/user/scripts/pythonscripts/get_jms_config.py", line 69, in getInfo
TypeError: sort() takes no keyword arguments

If I try using itemgetter it's no better, as I get the following error: 如果尝试使用itemgetter并没有更好的效果,因为出现以下错误:

Problem invoking WLST - Traceback (innermost last):
  File "/home/user/scripts/pythonscripts/get_jms_config.py", line 5, in ?
ImportError: cannot import name itemgetter

Does anybody have any suggestion? 有人有什么建议吗?

Edited: 编辑:

def getQueueInformation():
    try:
        list2d = []
        j = 1
        jmsSystemResources = cmo.getJMSSystemResources();
        for jmsSystemResource in jmsSystemResources:
            queues = jmsSystemResource.getJMSResource().getQueues();
            for queue in queues:
                # print some information
                row = []
                row.append(queue.getName())
                row.append(str(queue.getDeliveryParamsOverrides().getRedeliveryDelay()))
                row.append(str(queue.getDeliveryFailureParams().getRedeliveryLimit()))

                list2d.append(row)
                j += 1 
        return list2d
    except WLSTException:
        print 'an error occurred...',

Regards, Deborah 问候,黛博拉

It sounds like you're running a version of Python before 2.4 which is when .sort(key=...) was introduced. 听起来好像您正在运行2.4之前的Python版本,当时是引入.sort(key=...)时候。 You can try using the cmp() version of .sort() : 您可以尝试使用.sort()cmp()版本:

list2d.sort(lambda left, right: cmp(left[0], right[0]))

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

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