Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #106394

Re: Sorting a list

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: Sorting a list
Date Sun, 03 Apr 2016 21:31:05 +0200
Organization None
Lines 64
Message-ID <mailman.408.1459711883.28225.python-list@python.org> (permalink)
References <ndrn97$bco$1@dont-email.me>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace news.uni-berlin.de X8+lMOq3L1z+vOCCCiWn7gVBF9Kfv0sCmcjjvR0XxAZw==
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; 'column': 0.07; 'items)': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'output': 0.13; 'alpha,': 0.16; 'descending': 0.16; 'dfs': 0.16; 'invocations': 0.16; 'itemgetter': 0.16; 'lambda': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'sort()': 0.16; 'sorted()': 0.16; 'x[1])': 0.16; 'wrote:': 0.16; 'nested': 0.18; '>>>': 0.20; 'skip:" 30': 0.20; 'import': 0.24; 'skip:- 40': 0.25; 'sort': 0.25; 'header:User-Agent:1': 0.26; 'subject:list': 0.26; '(which': 0.26; 'header:X-Complaints-To:1': 0.26; 'print': 0.30; 'putting': 0.30; 'skip:[ 10': 0.31; 'could': 0.35; 'replace': 0.35; 'item': 0.35; 'but': 0.36; 'instead': 0.36; 'there': 0.36; 'skip:{ 10': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'received:org': 0.37; 'to:addr:python.org': 0.40; 'received:de': 0.40
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57bd80fd.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:106394

Show key headers only | View raw


DFS wrote:

> cntText = 60
> cntBool = 20
> cntNbrs = 30
> cntDate = 20
> cntBins = 20
> 
> strText = "  text:     "
> strBool = "  boolean:  "
> strNbrs = "  numeric:  "
> strDate = "  date-time:"
> strBins = "  binary:   "
> 
> colCounts = [(cntText,strText) , (cntBool,strBool), (cntNbrs,strNbrs) ,
> (cntDate,strDate) , (cntBins,strBins)]
> 
> # sort by alpha, then by column type count descending
> colCounts.sort(key=lambda x: x[1])
> colCounts.sort(key=lambda x: x[0], reverse=True)
> for key in colCounts: print key[1], key[0]]
> 
> -------------------------------------------------
> 
> Output (which is exactly what I want):
> 
>    text:      60
>    numeric:   30
>    binary:    20
>    boolean:   20
>    date-time: 20
> 
> -------------------------------------------------
> 
> 
> But, is there a 1-line way to sort and print?

Yes, but I would not recommend it. You can replace the sort() method 
invocations with nested calls of sorted() and instead of

for item in items:
    print convert_to_str(item)

use

print "\n".join(convert_to_str(item) for item in items)

Putting it together:

>>> from operator import itemgetter as get
>>> print "\n".join("{1} {0}".format(*p) for p in sorted(
... sorted(colCounts, key=get(1)), key=get(0), reverse=True))
  text:      60
  numeric:   30
  binary:    20
  boolean:   20
  date-time: 20

You could also cheat and use

lambda v: (-v[0], v[1])

and a single sorted().

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Sorting a list DFS <nospam@dfs.com> - 2016-04-03 14:30 -0400
  Re: Sorting a list DFS <nospam@dfs.com> - 2016-04-03 14:34 -0400
  Re: Sorting a list Peter Otten <__peter__@web.de> - 2016-04-03 21:31 +0200
    Re: Sorting a list DFS <nospam@dfs.com> - 2016-04-03 16:08 -0400
      Re: Sorting a list Peter Otten <__peter__@web.de> - 2016-04-04 08:56 +0200
      Re: Sorting a list Random832 <random832@fastmail.com> - 2016-04-04 09:40 -0400
      Re: Sorting a list Peter Otten <__peter__@web.de> - 2016-04-04 16:12 +0200

csiph-web