Python

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • Sorting a list of lists aka nested lists

    5 answers - 456 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    I have a python script that creates a list of lists like so:
    Quant.append( [ db_ticker, stock_close, MTD, 0, QTD, 0, YTD, 0, 0, 0 ] )
    After Quant is created, I want to sort it by MTD. If I use a simple
    Quant.sort(), I assume its going to sort by 'db_ticker' which is not
    what I want.
    I've been trying to figure out how to use the cmp() in a list sort
    method and I have failed to understand.
    Please help.
  • No.1 | | 644 bytes | |

    Quant.append( [ db_ticker, stock_close, MTD, 0, QTD, 0, YTD, 0,
    0, 0 ] )

    After Quant is created, I want to sort it by MTD. If I use a simple
    Quant.sort(), I assume its going to sort by 'db_ticker' which is not
    what I want.

    you need to write your own comparison function.
    Basically it will take two of your lists and return -1,0 or 1.

    you can use Python's own logic to help

    def cmplists(lst1,lst2):
    return cmp(lst1[2],lst2[2])

    Now you can sort Quant by passing your function into sort

    HTH,

    Alan G.

    Tutor maillist - Tutor (AT) python (DOT) org
  • No.2 | | 878 bytes | |

    Quoting Alan G <alan.gauld (AT) freenet (DOT) co.uk>:

    Quant.append( [ db_ticker, stock_close, MTD, 0, QTD, 0, YTD, 0,
    0, 0 ] )
    After Quant is created, I want to sort it by MTD. If I use a simple
    Quant.sort(), I assume its going to sort by 'db_ticker' which is not
    what I want.
    you need to write your own comparison function.
    Basically it will take two of your lists and return -1,0 or 1.

    you can use Python's own logic to help

    def cmplists(lst1,lst2):
    return cmp(lst1[2],lst2[2])

    Now you can sort Quant by passing your function into sort

    Note that in Python2.4+, you can use key= instead:

    def sortKey(lst):
    return lst[2]
    Quant.sort(key=sortKey)

    This is more effient than specifying a different comparison function, because
    the key function is only called once for each element.
  • No.3 | | 568 bytes | |

    jfouhy (AT) paradise (DOT) net.nz wrote:
    Note that in Python2.4+, you can use key= instead:

    def sortKey(lst):
    return lst[2]
    Quant.sort(key=sortKey)

    This is more effient than specifying a different comparison function, because
    the key function is only called once for each element.

    And instead of defining your own function you can use itertools.itemgetter() which is intended for this purpose:
    import itertools
    Quant.sort(key=itertools.itemgetter(2))

    Kent

    Tutor maillist - Tutor (AT) python (DOT) org
  • No.4 | | 745 bytes | |

    No-one's mentioned a Schwartzian Transform yet ; )

    8/15/05, Kent Johnson <kent37 (AT) tds (DOT) netwrote:
    jfouhy (AT) paradise (DOT) net.nz wrote:
    Note that in Python2.4+, you can use key= instead:

    def sortKey(lst):
    return lst[2]
    Quant.sort(key=sortKey)

    This is more effient than specifying a different comparison function, because
    the key function is only called once for each element.

    And instead of defining your own function you can use itertools.itemgetter() which is intended for this purpose:
    import itertools
    Quant.sort(key=itertools.itemgetter(2))

    Kent

    Tutor maillist - Tutor (AT) python (DOT) org

    Tutor maillist - Tutor (AT) python (DOT) org
  • No.5 | | 1114 bytes | |

    mailing list wrote:
    No-one's mentioned a Schwartzian Transform yet ; )

    because it's obsoleted by the key parameter to sort

    8/15/05, Kent Johnson <kent37 (AT) tds (DOT) netwrote:

    >>jfouhy (AT) paradise (DOT) net.nz wrote:
    >>

    Note that in Python2.4+, you can use key= instead:

    def sortKey(lst):
    return lst[2]
    Quant.sort(key=sortKey)

    This is more effient than specifying a different comparison function, because
    the key function is only called once for each element.
    >>
    >>And instead of defining your own function you can use itertools.itemgetter() which is intended for this purpose:
    >>import itertools
    >>Quant.sort(key=itertools.itemgetter(2))
    >>
    >>Kent
    >>
    >>
    >>Tutor maillist - Tutor (AT) python (DOT) org
    >>
    >>


    Tutor maillist - Tutor (AT) python (DOT) org

    Tutor maillist - Tutor (AT) python (DOT) org

Re: Sorting a list of lists aka nested lists


max 4000 letters.
Your nickname that display:
In order to stop the spam: 9 + 8 =
QUESTION ON "Python"

EMSDN.COM