简体   繁体   中英

Sort 2D list in Jython/Python

I'm using WLST (python/jython) to get the configuration of some WebLogic resources. I loop in the queues of a jms module and for each queue I recover the name and some other parameters.

With this info I build a 2D list that I want to order by queueName.

While I can successfully do it these two ways in the python console:

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:

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:

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. You can try using the cmp() version of .sort() :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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