Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #91891 > unrolled thread
| Started by | fl <rxjwg98@gmail.com> |
|---|---|
| First post | 2015-06-02 13:20 -0700 |
| Last post | 2015-06-02 14:00 -0700 |
| Articles | 7 — 5 participants |
Back to article view | Back to comp.lang.python
Please help on this sorted function fl <rxjwg98@gmail.com> - 2015-06-02 13:20 -0700
Re: Please help on this sorted function fl <rxjwg98@gmail.com> - 2015-06-02 13:25 -0700
Re: Please help on this sorted function Joonas Liik <liik.joonas@gmail.com> - 2015-06-02 23:42 +0300
Re: Please help on this sorted function Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-06-03 18:21 +1000
Re: Please help on this sorted function Chris Angelico <rosuav@gmail.com> - 2015-06-03 10:01 +1000
Re: Please help on this sorted function Joonas Liik <liik.joonas@gmail.com> - 2015-06-02 23:32 +0300
Re: Please help on this sorted function Gary Herron <gherron@digipen.edu> - 2015-06-02 14:00 -0700
| From | fl <rxjwg98@gmail.com> |
|---|---|
| Date | 2015-06-02 13:20 -0700 |
| Subject | Please help on this sorted function |
| Message-ID | <37d9de72-b719-45a0-976c-441fc5898741@googlegroups.com> |
Hi,
I try to learn sorted(). With the tutorial example:
>>> ff=sorted({1: 'D', 2: 'B', 3: 'B', 4: 'E', 5: 'A'})
>>> ff
[1, 2, 3, 4, 5]
I don't see what sorted does in this dictionary, i.e. the sequence of
1..5 is unchanged. Could you explain it to me?
Thanks,
[toc] | [next] | [standalone]
| From | fl <rxjwg98@gmail.com> |
|---|---|
| Date | 2015-06-02 13:25 -0700 |
| Message-ID | <70489d38-0848-44c4-9a4c-ba2e2e9aa027@googlegroups.com> |
| In reply to | #91891 |
On Tuesday, June 2, 2015 at 1:20:40 PM UTC-7, fl wrote:
> Hi,
>
> I try to learn sorted(). With the tutorial example:
>
>
>
>
> >>> ff=sorted({1: 'D', 2: 'B', 3: 'B', 4: 'E', 5: 'A'})
> >>> ff
> [1, 2, 3, 4, 5]
>
>
>
> I don't see what sorted does in this dictionary, i.e. the sequence of
> 1..5 is unchanged. Could you explain it to me?
>
>
> Thanks,
Excuse me. After a small modification, it can see the effect.
>>> ff=sorted({1: 'D', 2: 'B', 5: 'B', 4: 'E', 3: 'A'})
>>> ff
[1, 2, 3, 4, 5]
I am still new to Python. How to get the sorted dictionary output:
{1: 'D', 2: 'B', 3: 'A', 4: 'E', 5: 'B'}
[toc] | [prev] | [next] | [standalone]
| From | Joonas Liik <liik.joonas@gmail.com> |
|---|---|
| Date | 2015-06-02 23:42 +0300 |
| Message-ID | <mailman.80.1433277754.13271.python-list@python.org> |
| In reply to | #91892 |
[Multipart message — attachments visible in raw view] — view raw
my_dict = {1: 'D', 2: 'B', 3: 'A', 4: 'E', 5: 'B'}
# dict.items() returns an iterator that returns pairs of (key, value) pairs
# the key argument to sorted tells sorted what to sort by,
operator.itemgetter is a factory function , itemgetter(1)== lambda
iterable: iterable[1]
sorted_dict = sorted(my_dict.items(), key=itemgetter(1))
# at this moment sorted dict is a generator of key-value tuples in the
right order
sorted_dict = OrderedDict(sorted_dict) # turn the generator in to an actual
dict.
# notice: regular dicts are NOT ORDERED, you need a special type of dict to
preserve the order, hence OrderedDict
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2015-06-03 18:21 +1000 |
| Message-ID | <556eb8f6$0$13008$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #91894 |
On Wednesday 03 June 2015 06:42, Joonas Liik wrote:
> my_dict = {1: 'D', 2: 'B', 3: 'A', 4: 'E', 5: 'B'}
>
> # dict.items() returns an iterator that returns pairs of (key, value)
> # pairs the key argument to sorted tells sorted what to sort by,
> operator.itemgetter is a factory function , itemgetter(1)== lambda
> iterable: iterable[1]
> sorted_dict = sorted(my_dict.items(), key=itemgetter(1))
>
> # at this moment sorted dict is a generator of key-value tuples in the
> right order
> sorted_dict = OrderedDict(sorted_dict) # turn the generator in to an
> actual dict.
>
> # notice: regular dicts are NOT ORDERED, you need a special type of dict
> # to
> preserve the order, hence OrderedDict
OrderedDicts preserve the *insertion order*, they don't sort the keys.
Ordinary dicts are unordered. The order you see is arbitrary and
unpredictable:
py> d = {}
py> d['C'] = 1; d['A'] = 2; d['B'] = 3
py> d
{'A': 2, 'C': 1, 'B': 3}
Ordered dicts are ordered by insertion order:
py> from collections import OrderedDict
py> d = OrderedDict()
py> d['C'] = 1; d['A'] = 2; d['B'] = 3
py> d
OrderedDict([('C', 1), ('A', 2), ('B', 3)])
Python doesn't have a SortedDict, where the keys are kept in sorted order.
--
Steve
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-06-03 10:01 +1000 |
| Message-ID | <mailman.86.1433289704.13271.python-list@python.org> |
| In reply to | #91892 |
On Wed, Jun 3, 2015 at 6:25 AM, fl <rxjwg98@gmail.com> wrote:
> I am still new to Python. How to get the sorted dictionary output:
>
> {1: 'D', 2: 'B', 3: 'A', 4: 'E', 5: 'B'}
Since dictionaries don't actually have any sort of order to them, the
best thing to do is usually to simply display it in order. And there's
a very handy function for doing that: a pretty-printer.
>>> import pprint
>>> pprint.pprint({1: 'D', 2: 'B', 5: 'B', 4: 'E', 3: 'A'})
{1: 'D', 2: 'B', 3: 'A', 4: 'E', 5: 'B'}
This one comes with Python, so you can use it as easily as that above
example. Or you could do it this way:
>>> from pprint import pprint
>>> pprint({1: 'D', 2: 'B', 5: 'B', 4: 'E', 3: 'A'})
{1: 'D', 2: 'B', 3: 'A', 4: 'E', 5: 'B'}
For a lot of Python data structures, this will give you a tidy and
human-readable display.
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Joonas Liik <liik.joonas@gmail.com> |
|---|---|
| Date | 2015-06-02 23:32 +0300 |
| Message-ID | <mailman.79.1433277166.13271.python-list@python.org> |
| In reply to | #91891 |
[Multipart message — attachments visible in raw view] — view raw
>>> ff=sorted({1: 'D', 2: 'B', 3: 'B', 4: 'E', 5: 'A'})
>>> ff
[1, 2, 3, 4, 5]
sorted({1: 'D', 2: 'B', 3: 'B', 4: 'E', 5: 'A'}) is equivalent to
sorted(iter({1: 'D', 2: 'B', 3: 'B', 4: 'E', 5: 'A'}))
and iter(dict) iterates over the dict keys, so when you do
iter({1: 'D', 2: 'B', 3: 'B', 4: 'E', 5: 'A'}) you essentially get
[1,2,3,4,5]
and sorted([1,2,3,4,5]) returns [1,2,3,4,5]
[toc] | [prev] | [next] | [standalone]
| From | Gary Herron <gherron@digipen.edu> |
|---|---|
| Date | 2015-06-02 14:00 -0700 |
| Message-ID | <mailman.91.1433315375.13271.python-list@python.org> |
| In reply to | #91891 |
On 06/02/2015 01:20 PM, fl wrote:
> Hi,
>
> I try to learn sorted(). With the tutorial example:
>
>
>
>
>>>> ff=sorted({1: 'D', 2: 'B', 3: 'B', 4: 'E', 5: 'A'})
>>>> ff
> [1, 2, 3, 4, 5]
>
>
>
> I don't see what sorted does in this dictionary, i.e. the sequence of
> 1..5 is unchanged. Could you explain it to me?
>
>
> Thanks,
It's best to think of dictionaries as unordered collections of key/value
pairs. Dictionaries are not sequences, do not have any particular
ordering, and in full generality *can't* be sorted in any sensible way.
For instance, this slightly odd (but perfectly legal) dictionary
>>> d = {'a':123, 456:'b'}
can't be sorted
>>> sorted(d)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: int() < str()
because it doesn't make sense to order/compare the two keys 'a' and 456.
If your dictionary is a little better behaved, say
>>> d = {'a':123, 'b':456}
you may be able to sort the keys
>>> sorted(d)
['a', 'b']
or the values
>>> sorted(d.values())
[123, 456]
or the key/value tuples (called items)
>>> sorted(d.items())
[('a', 123), ('b', 456)]
but each of those attempts to sort could fail on a general dictionary if
the individual keys or values are not sortable.
There is also an implementation of a type of dictionary that remembers
the order in which the items are *inserted*. It's in the collections
module and called OrderedDict.
Gary Herron
--
Dr. Gary Herron
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web