python - 在 Jython/Python 中对二维列表进行排序

标签 python list sorting jython wlst

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

根据此信息,我构建了一个二维列表,我想按 queueName 对其进行排序。

虽然我可以在 python 控制台中通过这两种方式成功地做到这一点:

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

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

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

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

我得到错误:

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

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

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

有人有什么建议吗?

编辑:

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...',

问候, 黛博拉

最佳答案

听起来您正在运行 2.4 之前的 Python 版本,即 .sort(key=...)被介绍。您可以尝试使用 cmp() .sort() 的版本:

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

关于python - 在 Jython/Python 中对二维列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26739026/

相关文章:

python - Django ValidationError 日期格式异常

python - App Engine,两个实体之间的交叉引用

javascript - 如何在应用程序中使用 Ext.list.Tree 组件?

python - 从列表python的单个列表中删除子列表

将数组除以自己的列时出现 Python ValueError

python - 使用python分割数组中的元素

Python:如何修复我的代码以便追加将参数添加到列表中?

python - Pandas Dataframe - 排序日期时出现问题

Java - Google App Engine - 不使用 Collections.sort() 对 ArrayList<Object> 进行排序

arrays - 力扣代码 : sort an array of n objects with k different colors