Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #106402
| Path | csiph.com!news.swapon.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | DFS <nospam@dfs.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: Sorting a list |
| Date | Sun, 3 Apr 2016 16:08:42 -0400 |
| Organization | A noiseless patient Spider |
| Lines | 77 |
| Message-ID | <ndrt29$36l$1@dont-email.me> (permalink) |
| References | <ndrn97$bco$1@dont-email.me> <mailman.408.1459711883.28225.python-list@python.org> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=windows-1252; format=flowed |
| Content-Transfer-Encoding | 7bit |
| Injection-Date | Sun, 3 Apr 2016 20:05:29 -0000 (UTC) |
| Injection-Info | mx02.eternal-september.org; posting-host="5399b1c4ae39fc7dd2f40cbc5f70036e"; logging-data="3285"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+2xwfDh23bNmlMlWzWdh5r" |
| User-Agent | Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.0 |
| In-Reply-To | <mailman.408.1459711883.28225.python-list@python.org> |
| Cancel-Lock | sha1:vFnVE0X1qE9ReNAEdYcMAVVIqdU= |
| Xref | csiph.com comp.lang.python:106402 |
Show key headers only | View raw
On 4/3/2016 3:31 PM, Peter Otten wrote:
> 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))
Kind of clunky looking. Is that why don't you recommend it?
> 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().
That works well. Why is it 'cheating'?
Thanks for the reply.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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