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

Steve Howell showell30 at yahoo.com
Wed May 30 05:31:49 CEST 2007


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

> However, I do think there could be very useful uses
> for a standard sorting 
> structure of some sort.  That's the sorting as in
> mail sorters, or category 
> sorters, that produce several streams of output
> instead of just one.
> 
> Would that be called a de-comprehension?
> 

Here is some category-sorting code, FWIW, where every
employee, Fred or not, gets a 50% raise, and employees
are partitioned according to their Fredness.

It doesn't use a general iterator, so maybe I'm
missing your point.


def partitions(lst):
    dct = {}
    for k, value in lst:
        dct.setdefault(k, []).append(value)
    return dct.items()

def is_fred(emp):
    return 'Fred' in emp[0]


emps = [
    ('Fred Smith', 50),
    ('Fred Jones', 40),
    ('Joe Blow', 30),
    ]

def pay_increase(salary): return salary * 0.5

emp_groups = partitions([(is_fred(emp),
        (emp[0], pay_increase(emp[1]))) for
        emp in emps])

for fredness, emps in emp_groups:
    print
    print 'is Fred?', fredness
    for name, pay_increase in emps:
        print name, pay_increase

----

is Fred? False
Joe Blow 15.0

is Fred? True
Fred Smith 25.0
Fred Jones 20.0



      ____________________________________________________________________________________
Park yourself in front of a world of choices in alternative vehicles. Visit the Yahoo! Auto Green Center.
http://autos.yahoo.com/green_center/ 



More information about the Python-ideas mailing list