|
|
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,
|
|
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'}
|
|
>>> 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]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: < http://mail.python.org/pipermail/python-list/attachments/20150602/5662edde/attachment.html>
|
|
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: < http://mail.python.org/pipermail/python-list/attachments/20150602/993e0e7e/attachment.html>
|
|
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
|
|
On Wed, Jun 3, 2015 at 6:25 AM, fl <rxjwg98 at 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
|
|
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
|
|