在 Jython/Python 中对 2D 列表进行排序

deb*_*deb 1 python sorting jython list wlst

我正在使用 WLST (python/jython) 来获取一些 WebLogic 资源的配置。我在 jms 模块的队列中循环,并为每个队列恢复名称和其他一些参数。

有了这些信息,我构建了一个二维列表,我想按队列名称排序。

虽然我可以在 python 控制台中通过以下两种方式成功完成此操作:

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

或者

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

当我使用 .py 脚本时,我的 WL 服务器(版本 10.3.5)中的 python/jython 版本(我真的不知道使用了什么)不喜欢这样:

list2d.sort(key=lambda x: x[0])
Run Code Online (Sandbox Code Playgroud)

我收到错误:

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
Run Code Online (Sandbox Code Playgroud)

如果我尝试使用 itemgetter 也好不到哪儿去,因为我收到以下错误:

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

有人有什么建议吗?

编辑:

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...',
Run Code Online (Sandbox Code Playgroud)

问候,黛博拉

use*_*450 5

听起来您正在运行 2.4 之前的 Python 版本(即 2.4.sort(key=...)推出时的版本)。您可以尝试使用cmp()以下版本.sort()

list2d.sort(lambda left, right: cmp(left[0], right[0]))
Run Code Online (Sandbox Code Playgroud)