Re: dict generator question

2008-09-22 Thread Miles
On Fri, Sep 19, 2008 at 9:51 PM, Steven D'Aprano [EMAIL PROTECTED] wrote: Extending len() to support iterables sounds like a good idea, except that it's not. Here are two iterables: def yes(): # like the Unix yes command while True: yield y def rand(total): Return random

Re: dict generator question

2008-09-22 Thread bearophileHUGS
Steven D'Aprano: Extending len() to support iterables sounds like a good idea, except that it's not. Python language lately has shifted toward more and more usage of lazy iterables (see range lazy by default, etc). So they are now quite common. So extending len() to make it act like leniter()

Re: dict generator question

2008-09-22 Thread Steven D'Aprano
On Mon, 22 Sep 2008 04:21:12 -0700, bearophileHUGS wrote: Steven D'Aprano: Extending len() to support iterables sounds like a good idea, except that it's not. Python language lately has shifted toward more and more usage of lazy iterables (see range lazy by default, etc). So they are now

Re: dict generator question

2008-09-22 Thread bearophileHUGS
Steven D'Aprano: I'm sorry, I don't recognise leniter(). Did I miss something? I have removed the docstring/doctests: def leniter(iterator): if hasattr(iterator, __len__): return len(iterator) nelements = 0 for _ in iterator: nelements += 1 return nelements it

Re: dict generator question

2008-09-19 Thread Paul Rubin
Simon Mullis [EMAIL PROTECTED] writes: l = [ 1.1.1.1, 1.2.2.2, 1.2.2.3, 1.3.1.2, 1.3.4.5] ... dict_of_counts = dict([(v[0:3], count) for v in l]) Untested: def major_version(version_string): convert '1.2.3.2' to '1.2' return '.'.join(version_string.split('.')[:2])

Re: dict generator question

2008-09-19 Thread Boris Borcic
Gerard flanagan wrote: George Sakkis wrote: .. Note that this works correctly only if the versions are already sorted by major version. Yes, I should have mentioned it. Here's a fuller example below. There's maybe better ways of sorting version numbers, but this is what I do. Indeed,

Re: dict generator question

2008-09-19 Thread Gerard flanagan
Boris Borcic wrote: Gerard flanagan wrote: George Sakkis wrote: .. Note that this works correctly only if the versions are already sorted by major version. Yes, I should have mentioned it. Here's a fuller example below. There's maybe better ways of sorting version numbers, but this is

Re: dict generator question

2008-09-19 Thread bearophileHUGS
Gerard flanagan: data.sort() datadict = \ dict((k, len(list(g))) for k,g in groupby(data, lambda s: '.'.join(s.split('.',2)[:2]))) That code may run correctly, but it's quite unreadable, while good Python programmers value high readability. So the right thing to do is to split that line

Re: dict generator question

2008-09-19 Thread MRAB
On Sep 19, 2:01 pm, [EMAIL PROTECTED] wrote: Gerard flanagan: data.sort() datadict = \ dict((k, len(list(g))) for k,g in groupby(data, lambda s:      '.'.join(s.split('.',2)[:2]))) That code may run correctly, but it's quite unreadable, while good Python programmers value high

Re: dict generator question

2008-09-19 Thread Steven D'Aprano
On Fri, 19 Sep 2008 17:00:56 -0700, MRAB wrote: Extending len() to support iterables sounds like a good idea, except that it could be misleading when: len(file(path)) returns the number of lines and /not/ the length in bytes as you might first think! Extending len() to support

Re: dict generator question

2008-09-19 Thread bearophileHUGS
MRAB: except that it could be misleading when: len(file(path)) returns the number of lines and /not/ the length in bytes as you might first think! :-) Well, file(...) returns an iterable of lines, so its len is the number of lines :-) I think I am able to always remember this fact.

dict generator question

2008-09-18 Thread Simon Mullis
Hi, Let's say I have an arbitrary list of minor software versions of an imaginary software product: l = [ 1.1.1.1, 1.2.2.2, 1.2.2.3, 1.3.1.2, 1.3.4.5] I'd like to create a dict with major_version : count. (So, in this case: dict_of_counts = { 1.1 : 1, 1.2 : 2,

Re: dict generator question

2008-09-18 Thread marek . rocki
Simon Mullis napisał(a): Something like: dict_of_counts = dict([(v[0:3], count) for v in l]) I can't seem to figure out how to get count, as I cannot do x += 1 or x++ as x may or may not yet exist, and I haven't found a way to create default values. It seems to me that the count you're

Re: dict generator question

2008-09-18 Thread pruebauno
On Sep 18, 10:54 am, Simon Mullis [EMAIL PROTECTED] wrote: Hi, Let's say I have an arbitrary list of minor software versions of an imaginary software product: l = [ 1.1.1.1, 1.2.2.2, 1.2.2.3, 1.3.1.2, 1.3.4.5] I'd like to create a dict with major_version : count. (So, in this case:

Re: dict generator question

2008-09-18 Thread George Sakkis
On Sep 18, 10:54 am, Simon Mullis [EMAIL PROTECTED] wrote: Hi, Let's say I have an arbitrary list of minor software versions of an imaginary software product: l = [ 1.1.1.1, 1.2.2.2, 1.2.2.3, 1.3.1.2, 1.3.4.5] I'd like to create a dict with major_version : count. (So, in this case:

Re: dict generator question

2008-09-18 Thread Gerard flanagan
Simon Mullis wrote: Hi, Let's say I have an arbitrary list of minor software versions of an imaginary software product: l = [ 1.1.1.1, 1.2.2.2, 1.2.2.3, 1.3.1.2, 1.3.4.5] I'd like to create a dict with major_version : count. (So, in this case: dict_of_counts = { 1.1 : 1,

Re: dict generator question

2008-09-18 Thread pruebauno
On Sep 18, 10:54 am, Simon Mullis [EMAIL PROTECTED] wrote: Hi, Let's say I have an arbitrary list of minor software versions of an imaginary software product: l = [ 1.1.1.1, 1.2.2.2, 1.2.2.3, 1.3.1.2, 1.3.4.5] I'd like to create a dict with major_version : count. (So, in this case:

Re: dict generator question

2008-09-18 Thread George Sakkis
On Sep 18, 11:43 am, Gerard flanagan [EMAIL PROTECTED] wrote: Simon Mullis wrote: Hi, Let's say I have an arbitrary list of minor software versions of an imaginary software product: l = [ 1.1.1.1, 1.2.2.2, 1.2.2.3, 1.3.1.2, 1.3.4.5] I'd like to create a dict with major_version :

Re: dict generator question

2008-09-18 Thread Simon Mullis
Haha! Thanks for all of the suggestions... (I love this list!) SM 2008/9/18 [EMAIL PROTECTED]: On Sep 18, 10:54 am, Simon Mullis [EMAIL PROTECTED] wrote: Hi, Let's say I have an arbitrary list of minor software versions of an imaginary software product: l = [ 1.1.1.1, 1.2.2.2, 1.2.2.3,

Re: dict generator question

2008-09-18 Thread Gerard flanagan
George Sakkis wrote: On Sep 18, 11:43 am, Gerard flanagan [EMAIL PROTECTED] wrote: Simon Mullis wrote: Hi, Let's say I have an arbitrary list of minor software versions of an imaginary software product: l = [ 1.1.1.1, 1.2.2.2, 1.2.2.3, 1.3.1.2, 1.3.4.5] I'd like to create a dict with