[Python-ideas] dict.fromkeys() better as dict().setkeys() ? (and other suggestions)

Steve Howell showell30 at yahoo.com
Wed May 30 04:53:03 CEST 2007


--- Ron Adam <rrr at ronadam.com> wrote:

> Steve Howell wrote:
> > --- Ron Adam <rrr at ronadam.com> wrote:
> >> # Tests
> >>
> >> def a_g(s):
> >>     return s[0].lower() in "abcdefg"
> >>
> >> def h_m(s):
> >>     return s[0].lower() in "hijklm"
> >>
> >> def n_z(s):
> >>     return s[0].lower() in "nopqrstuvwxyz"
> >>
> >> decmps = [a_g, h_m, n_z]
> >> ag, hm, nz, other = decomp(src, *decmps)
> >>
> >> print 'ag =', ag
> >> print 'hm =', hm
> >> print 'nz =', nz
> >> print 'other =', other
> >>
> >>
> >> -------------------
> >>
> >> ag = ['c8WQe60G6J', 'EMY7O8qzTg']
> >> hm = ['lDunyeOM98', 'LJuPg8ncZd']
> >> nz = ['uhhuhd9YdO', 'qAuQvfTc6N', 'vpJz47pkP5',
> >> 'YOq6m4IXBn']
> >> other = ['8JE6PuXxBz', '4ttyMdpuQY']
> >>
> > 
> > Am I misunderstanding (de-comprehensing) something
> > here?  How does the code above return those result
> > sets?  Or, more specifically, why does ag include
> 'T'
> > in its results set?
> 
> The data in this case simulates 10 digit partnumbers
> which can include a-z, 
> A-Z, and 0-9.
> 
> It doesn't alter the data, it just sorts it into
> smaller groups according 
> to some predefined tests.  In this case.. it's only
> testing the first 
> letter of each item.
> 
> What is tested is entirely up to you.  You could
> have lists of records as 
> your data and test fields and divide the data
> according to that.
> 

Ok, apologies for quoting away the parts of your code
that probably answer my own question.

But to your bigger question--I think you can set up a
list comprehension that does partitioning by having
the list comprension or generator expression simply
return a list of tuples where the first element in the
tuple is a value that suggest where it fits in the
partition, then feed that tuple to dict() or whatever.

But I don't have a specific code example to prove it.

For simple binary partitions, there is the bool
function.





       
____________________________________________________________________________________Choose the right car based on your needs.  Check out Yahoo! Autos new Car Finder tool.
http://autos.yahoo.com/carfinder/



More information about the Python-ideas mailing list