Re: Please help on this sorted function

2015-06-03 Thread Steven D'Aprano
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 , itemg

Re: Please help on this sorted function

2015-06-03 Thread Gary Herron
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,

Re: Please help on this sorted function

2015-06-02 Thread Chris Angelico
On Wed, Jun 3, 2015 at 6:25 AM, fl 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

Re: Please help on this sorted function

2015-06-02 Thread Joonas Liik
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

Re: Please help on this sorted function

2015-06-02 Thread Joonas Liik
>>> 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',

Re: Please help on this sorted function

2015-06-02 Thread fl
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 >

Please help on this sorted function

2015-06-02 Thread fl
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, -- https://mail.python.org/