> I'd like to do it in one line because what I am trying to do is, after
> all, a single, simple enough action. I find the suggested
> b = sorted(a.keys()) much more readable than breaking it up in two
> lines.
I think you have demonstrated that a single-line statements with
multiple functions a
>From ESR's "Why python?"
b = a.keys(); b.sort()
Two statements, one line. Doesn't create an incidental copy the way
sorted does but I'm not sure how much you like to jam on one line.
Cheers,
Aaron
--
http://mail.python.org/mailman/listinfo/python-list
[EMAIL PROTECTED] wrote:
> I'd like to do it in one line because what I am trying to do is,
> after all, a single, simple enough action.
No, it's two actions:
1. You extract a list of keys from the dict
2. you sort it
> I find the suggested b = sorted(a.keys()) much more readable than
> brea
On 21 srp, 02:31, Steven D'Aprano
<[EMAIL PROTECTED]> wrote:
> On Fri, 20 Jul 2007 15:27:51 -0700, montyphyton wrote:
> b = a.keys()
> b.sort()
> > [1, 2, 3]
>
> > Works fine, but I would really like it if I could somehow do it in one
> > line.
>
> Why? Is the Enter key on your keyboard b
On Fri, 20 Jul 2007 15:27:51 -0700, montyphyton wrote:
b = a.keys()
b.sort()
> [1, 2, 3]
>
> Works fine, but I would really like it if I could somehow do it in one
> line.
Why? Is the Enter key on your keyboard broken? Is there a global shortage
of newline characters that I haven't be
Miles <[EMAIL PROTECTED]> wrote in
news:[EMAIL PROTECTED]:
> On 7/20/07, Alex Popescu <[EMAIL PROTECTED]> wrote:
>> If you just want to iterate over your dict in an ordered manner than
>> all you have to do is:
>>
>> for k in my_dict.keys().sort():
>> # rest of the code
>
> I think you meant s
On 20 jul, 19:34, [EMAIL PROTECTED] wrote:
> copy.copy returns a new object:
>
> >>> copy.copy(a.keys())
>
> [1,2,3]
>
> Then why doesn't copy.copy(a.keys()).sort() work??
It works, but you don't notice it, because you don't save a reference
to the new list.
Try this:
c = copy.copy(a.keys())
On 21 srp, 00:47, Duncan Smith <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] wrote:
> > Consider the following:
>
> a = {1:2, 3:4, 2:5}
>
> > Say that i want to get the keys of a, sorted. First thing I tried:
>
> b = a.keys().sort()
> print b
>
> > None
>
> > Doesn't work. Probably be
On 20 jul, 18:50, Alex Popescu <[EMAIL PROTECTED]> wrote:
> If you just want to iterate over your dict in an ordered manner than all
> you have to do is:
>
> for k in my_dict.keys().sort():
> # rest of the code
sort() returns None, so this code won't work either.
--
Roberto Bonvallet
--
http:
On 7/20/07, Alex Popescu <[EMAIL PROTECTED]> wrote:
> If you just want to iterate over your dict in an ordered manner than all
> you have to do is:
>
> for k in my_dict.keys().sort():
> # rest of the code
I think you meant sorted(my_dict.keys()), since, as you just pointed
out, the sort() method
[EMAIL PROTECTED] wrote in news:1184970471.146819.86280
@r34g2000hsd.googlegroups.com:
I am not sure about your scenario, but as you discovered the sort() method
is modifying the in place list (and doesn't return a new one).
If you just want to iterate over your dict in an ordered manner than al
[EMAIL PROTECTED] wrote:
> Consider the following:
>
a = {1:2, 3:4, 2:5}
>
>
> Say that i want to get the keys of a, sorted. First thing I tried:
>
>
b = a.keys().sort()
print b
>
> None
>
> Doesn't work. Probably because I am actually trying to sort the keys
> of the dictionary
On Fri, Jul 20, 2007 at 03:27:51PM -0700, [EMAIL PROTECTED] wrote:
> Consider the following:
> >>> a = {1:2, 3:4, 2:5}
>
> Say that i want to get the keys of a, sorted. First thing I tried:
>
> >>> b = a.keys().sort()
> >>> print b
> None
list's sort() method sorts the list _in_place_:
>>>
On Fri, 20 Jul 2007 15:27:51 -0700, [EMAIL PROTECTED] wrote:
>Consider the following:
a = {1:2, 3:4, 2:5}
>
>Say that i want to get the keys of a, sorted. First thing I tried:
>
b = a.keys().sort()
print b
>None
>
>Doesn't work. Probably because I am actually trying to sort the keys
[EMAIL PROTECTED] wrote:
> Consider the following:
a = {1:2, 3:4, 2:5}
>
> Say that i want to get the keys of a, sorted. First thing I tried:
>
b = a.keys().sort()
print b
> None
>
> Doesn't work. Probably because I am actually trying to sort the keys
> of the dictionary without
Consider the following:
>>> a = {1:2, 3:4, 2:5}
Say that i want to get the keys of a, sorted. First thing I tried:
>>> b = a.keys().sort()
>>> print b
None
Doesn't work. Probably because I am actually trying to sort the keys
of the dictionary without copying them first. If that is the case,
fin
16 matches
Mail list logo